forked from fenos/dqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
135 lines (106 loc) · 2.83 KB
/
generator.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package dqlx
import (
"bytes"
_ "embed"
"fmt"
"go/format"
"html/template"
"io/fs"
"io/ioutil"
"regexp"
"strings"
)
//go:embed templates/types.tmpl
var typesTmpl string
type templateTypesVariables struct {
PackageName string
Types []templateDGraphType
Imports map[string]bool
}
type templateDGraphType struct {
Name string
Predicates []templateDGraphPredicate
}
type templateDGraphPredicate struct {
Name string
JsonName string
GoType string
IsEdge bool
}
// GeneratorOption options for the generator
type GeneratorOption struct {
Path string
PackageName string
}
// GenerateTypes given a schema it generates Go structs definitions
func GenerateTypes(schema *SchemaBuilder, options GeneratorOption) error {
tmpl, err := template.New("types").Parse(typesTmpl)
if err != nil {
return err
}
out := bytes.Buffer{}
typeDefinitions, imports := getTypeDefinition(schema)
err = tmpl.Execute(&out, templateTypesVariables{
PackageName: options.PackageName,
Types: typeDefinitions,
Imports: imports,
})
if err != nil {
return err
}
formattedCode, err := format.Source(out.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile(options.Path, formattedCode, fs.ModePerm)
}
func getTypeDefinition(schema *SchemaBuilder) ([]templateDGraphType, map[string]bool) {
types := make([]templateDGraphType, len(schema.Types))
imports := map[string]bool{}
for index, dType := range schema.Types {
templateType := templateDGraphType{
Name: dType.name,
Predicates: nil,
}
// Add fields
templateType.Predicates = append(templateType.Predicates, templateDGraphPredicate{
Name: "Uid",
JsonName: "uid",
GoType: "string",
})
for _, predicate := range dType.predicates {
if predicate.ScalarType == ScalarDateTime {
imports["time"] = true
}
predicateType := dqlTypeToGoType(predicate.ScalarType)
if predicate.List {
predicateType = fmt.Sprintf("[]%s", predicateType)
}
fieldName := predicate.Name
if strings.Contains(fieldName, ".") {
parts := strings.Split(fieldName, ".")
fieldName = parts[len(parts)-1]
}
templateType.Predicates = append(templateType.Predicates, templateDGraphPredicate{
Name: toCamelCase(fieldName),
JsonName: predicate.Name,
GoType: predicateType,
IsEdge: !isKnownScalarType(predicate.ScalarType),
})
}
// Default DType field
templateType.Predicates = append(templateType.Predicates, templateDGraphPredicate{
Name: "DType",
JsonName: "dgraph.type",
GoType: "[]string",
})
types[index] = templateType
}
return types, imports
}
var link = regexp.MustCompile("(^[A-Za-z])|_([A-Za-z])")
func toCamelCase(str string) string {
return link.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})
}