1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// A codegen cmd for generating builder types from template.
package main
import (
"bytes"
"go/format"
"io/ioutil"
"log"
"strings"
"text/template"
"github.com/facebook/ent/schema/field"
)
func main() {
buf, err := ioutil.ReadFile("internal/types.tmpl")
if err != nil {
log.Fatal("reading template file:", err)
}
tmpl := template.Must(template.New("types").
Funcs(template.FuncMap{
"ops": ops,
"title": strings.Title,
"ident": ident,
"type": typ,
}).
Parse(string(buf)))
b := &bytes.Buffer{}
if err := tmpl.Execute(b, struct {
Types []field.Type
}{
Types: []field.Type{
field.TypeBool,
field.TypeBytes,
field.TypeTime,
field.TypeUint,
field.TypeUint8,
field.TypeUint16,
field.TypeUint32,
field.TypeUint64,
field.TypeInt,
field.TypeInt8,
field.TypeInt16,
field.TypeInt32,
field.TypeInt64,
field.TypeFloat32,
field.TypeFloat64,
field.TypeString,
field.TypeUUID,
},
}); 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("types.go", buf, 0644); err != nil {
log.Fatal("writing go file:", err)
}
}
func ops(t field.Type) []string {
switch t {
case field.TypeBool, field.TypeBytes, field.TypeUUID:
return []string{"EQ", "NEQ"}
default:
return []string{"EQ", "NEQ", "LT", "LTE", "GT", "GTE"}
}
}
func ident(t field.Type) string {
switch t {
case field.TypeBytes:
return "bytes"
case field.TypeTime:
return "time"
case field.TypeUUID:
return "value"
default:
return t.String()
}
}
func typ(t field.Type) string {
if t == field.TypeUUID {
return "driver.Valuer"
}
return t.String()
}
|