Commit 4bb2f097 authored by Daniel Martí's avatar Daniel Martí

schema/gen/go: apply gofmt automatically

Now that we buffer the output, using go/format is trivial.

This makes the default behavior better, and means not having to use an
extra gofmt go:generate step everywhere.
parent 2359e698
......@@ -14,6 +14,5 @@
// The code generation is triggered by `go:generate` comments in the `doc.go` file.
//go:generate go run gen.go
//go:generate gofmt -w .
package gendemo
//go:generate go run gen.go
//go:generate gofmt -w .
package schemadmt
......@@ -3,6 +3,7 @@ package gengo
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"path/filepath"
......@@ -150,7 +151,16 @@ func withFile(filename string, fn func(io.Writer)) {
// more atomicity via the single write.
buf := new(bytes.Buffer)
fn(buf)
if err := ioutil.WriteFile(filename, buf.Bytes(), 0666); err != nil {
src := buf.Bytes()
// Format the source before writing, just like gofmt would.
// This also prevents us from writing invalid syntax to disk.
src, err := format.Source(src)
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(filename, src, 0666); err != nil {
panic(err)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment