-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_code_model.go.tpl
157 lines (148 loc) · 6.38 KB
/
generate_code_model.go.tpl
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{{ reserveImport "fmt" }}
{{ reserveImport "time" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
{{ reserveImport "github.com/mitchellh/mapstructure" }}
{{- $root := .}}
{{- $input2TypeName := "MergeToType"}}
// GetInputStruct returns struct filled from map obj defined by name
// Example useage struct validation with github.com/go-playground/validator by directive:
// func ValidateDirective(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
// field := graphql.GetPathContext(ctx)
// if data, ok := obj.(map[string]interface{}); ok {
// for _, v := range field.ParentField.Field.Arguments {
// name := v.Value.ExpectedType.Name()
// model, err := model.GetInputStruct(name, data)
// if err != nil {
// //handle not found error
// }
// if err := validate.Struct(model); err != nil {
// //handle error
// }
// }
// }
// return next(ctx)
// }
func GetInputStruct(name string, obj map[string]interface{}) (interface{}, error) {
switch name {
{{- range $objectName, $object := .Handler.List.Objects }}
{{- if $object.HasSqlDirective}}
case "{{$objectName}}Input":
return {{$objectName}}InputFromMap(obj)
{{- end}}
{{- end}}
}
return nil, fmt.Errorf("%s not found", name)
}
{{- range $objectName, $object := .Handler.List.Enums }}
// {{$input2TypeName}} for enum value {{$objectName}}
func (d *{{$objectName}}) {{$input2TypeName}}() {{$objectName}} {
return *d
}
{{- end}}
{{- range $objectName, $object := .Handler.List.Objects }}
{{- if $object.HasSqlDirective}}
{{$objectName := $object.Name}}
// {{$objectName}}InputFromMap return a {{$objectName}}Input from data map
// use github.com/mitchellh/mapstructure with reflaction
func {{$objectName}}InputFromMap(data map[string]interface{}) ({{$objectName}}Input, error) {
model := {{$objectName}}Input{}
err := mapstructure.Decode(data, &model);
return model, err
}
// {{$input2TypeName}} returns a map with all values set to {{$objectName}}Patch
func (d *{{$objectName}}Patch) {{$input2TypeName}}() map[string]interface{} {
res := make(map[string]interface{})
{{- range $entityKey, $entity := $object.PatchEntities }}
{{- $entityGoName := $root.GetGoFieldName $objectName $entity}}
{{- if $entity.IsPrimitive}}
if d.{{$entityGoName}} != nil {
{{- if $entity.IsArray}}
var tmp{{$entityGoName}} {{$root.GetGoFieldType $objectName $entity false}}
for _, v := range d.{{$entityGoName}}{
tmp := v
tmp{{$entityGoName}} = append(tmp{{$entityGoName}}, {{$root.GenPointerStrIfNeeded $objectName $entity false}}tmp)
}
res["{{$entity.DatabaseFieldName}}"] = tmp{{$entityGoName}}
{{- else}}
res["{{$entity.DatabaseFieldName}}"] = {{$root.PointerStrIfNeeded $objectName $entity true}}d.{{$entityGoName}}
{{- end}}
}
{{- else}}
if d.{{$entityGoName}} != nil {
{{- if $entity.IsArray}}
tmp{{$entityGoName}} := make([]map[string]interface{},len(d.{{$entityGoName}}))
for _, v := range d.{{$entityGoName}}{
tmp := v.{{$input2TypeName}}()
tmp{{$entityGoName}} = append(tmp{{$entityGoName}}, tmp)
}
res["{{$entity.DatabaseFieldName}}"] = tmp{{$entityGoName}}
{{- else}}
{{- if $entity.IsScalar}}
res["{{$entity.DatabaseFieldName}}"] = {{if not $entity.Required}}*{{end}} d.{{$entityGoName}}
{{- else}}
res["{{$entity.DatabaseFieldName}}"] = d.{{$entityGoName}}.{{$input2TypeName}}()
{{- end}}
{{- end}}
}
{{- end}}
{{- end}}
return res
}
// {{$input2TypeName}} retuns a {{$objectName}} filled from {{$objectName}}Input
func (d *{{$objectName}}Input) {{$input2TypeName}}() {{$objectName}} {
{{- range $entityKey, $entity := $object.InputEntities }}
{{- $entityGoName := $root.GetGoFieldName $objectName $entity}}
{{- if not $entity.IsPrimitive}}
{{$pointer := "*"}}
{{- if $entity.Required}}
{{$pointer := ""}}
{{- end}}
{{$entityType := $root.GetGoFieldType $objectName $entity true}}
var tmp{{$entityGoName}} {{ if $entity.IsArray}} []{{$pointer}}{{$entityType }} {{ else }} {{$entityType }} {{ end }}
{{- if not $entity.Required}}
if d.{{$entityGoName}} != nil {
{{- end}}
{{- if $entity.IsArray}}
tmp{{$entityGoName}} = make([]*{{$entityType }},len(d.{{$entityGoName}}))
for _, v := range d.{{$entityGoName}}{
tmp := v.{{$input2TypeName}}()
tmp{{$entityGoName}} = append(tmp{{$entityGoName}}, &tmp)
}
{{- else}}
{{- if $entity.IsScalar}}
tmp{{$entityGoName}} = {{if not $entity.Required}}*{{end}}d.{{$entityGoName}}
{{- else}}
tmp{{$entityGoName}} = d.{{$entityGoName}}.{{$input2TypeName}}()
{{- end}}
{{- end}}
{{- if not $entity.Required}}
}
{{- end}}
{{- else}}
{{$entityType := $root.GetGoFieldType $objectName $entity false}}
{{- if $entity.Required}}
{{- if and $entity.IsArray (not $entity.IsArrayElementRequired)}}
tmp{{$entityGoName}} := make({{$entityType }}, len(d.{{$entityGoName}}))
for _, v := range d.{{$entityGoName}}{
tmp{{$entityGoName}} = append(tmp{{$entityGoName}}, &v)
}
{{- else}}
tmp{{$entityGoName}} := d.{{$entityGoName}}
{{- end}}
{{- else}}
var tmp{{$entityGoName}} {{$entityType }}
if d.{{$entityGoName}} != nil {
tmp{{$entityGoName}} = d.{{$entityGoName}}
}
{{- end}}
{{- end}}
{{- end}}
return {{$objectName}}{
{{- range $entityKey, $entity := $object.InputEntities }}
{{- $entityGoName := $root.GetGoFieldName $objectName $entity}}
{{$entityGoName}}: {{$root.GetPointerSymbol $objectName $entity}}tmp{{$entityGoName}},
{{- end}}
}
}
{{- end}}
{{- end}}