Files
ent/schema/field/gen/gen.go
Ariel Mashraki f519a5465e schema/field: add builder per numeric type
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/21

Reviewed By: alexsn

Differential Revision: D16936906

fbshipit-source-id: 06f5721ed0088107cf42382731f8b7282f22eef7
2019-08-21 08:00:01 -07:00

53 lines
1.1 KiB
Go

// gen is a codegen cmd for generating numeric build types from template.
package main
import (
"bytes"
"go/format"
"io/ioutil"
"log"
"strings"
"text/template"
"github.com/facebookincubator/ent/schema/field"
)
func main() {
buf, err := ioutil.ReadFile("gen/numeric.tmpl")
if err != nil {
log.Fatal("reading template file:", err)
}
intTmpl := template.Must(template.New("numeric").
Funcs(template.FuncMap{"title": strings.Title, "hasPrefix": strings.HasPrefix}).
Parse(string(buf)))
b := &bytes.Buffer{}
if err := intTmpl.Execute(b, struct {
Ints, Floats []field.Type
}{
Ints: []field.Type{
field.TypeInt,
field.TypeUint,
field.TypeInt8,
field.TypeInt16,
field.TypeInt32,
field.TypeInt64,
field.TypeUint8,
field.TypeUint16,
field.TypeUint32,
field.TypeUint64,
},
Floats: []field.Type{
field.TypeFloat64,
field.TypeFloat32,
},
}); err != nil {
log.Fatal("executing template:", err)
}
if buf, err = format.Source(b.Bytes()); err != nil {
log.Fatal("formatting output:", err)
}
if err := ioutil.WriteFile("numeric.go", buf, 0644); err != nil {
log.Fatal("writing go file:", err)
}
}