-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfield.go.tmpl
65 lines (53 loc) · 2.14 KB
/
field.go.tmpl
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
{{- define "field" -}}
{{- $type := .Type -}}
{{- $name := (coalesce .Name "") -}}
{{- $fieldName := $name | firstLetterToUpper -}}
{{- $typePrefix := .TypePrefix -}}
{{- $typeMeta := .TypeMeta -}}
{{- $typeMap := .TypeMap -}}
{{- $optional := .Optional -}}
{{- $printName := .PrintName -}}
{{- $includeJsonTags := .JsonTags -}}
{{- $includeStructTags := .StructTags -}}
{{- $customType := "" -}}
{{- $jsonTag := printf "json:%q" $name }}
{{- $structTags := array -}}
{{- range $meta := $typeMeta -}}
{{- if exists $meta "json" -}}
{{- $jsonTag = printf "json:%q" (get $meta "json") -}}
{{- end -}}
{{- if exists $meta "go.field.name" -}}
{{- $fieldName = get $meta "go.field.name" -}}
{{- end -}}
{{- if exists $meta "go.field.type" -}}
{{- $customType = get $meta "go.field.type" -}}
{{- end -}}
{{- if exists $meta "go.tag.json" -}}
{{- $jsonTag = printf "json:%q" (get $meta "go.tag.json") -}}
{{- end -}}
{{- range $metaKey, $metaValue := $meta -}}
{{- if and (hasPrefix $metaKey "go.tag.") (ne $metaKey "go.tag.json") -}}
{{- $structTags = append $structTags (printf "%s:%q" (trimPrefix $metaKey "go.tag.") $metaValue) -}}
{{- end -}}
{{- end -}}
{{- end }}
{{- if $printName}}{{"\t"}}{{$fieldName}} {{end -}}
{{- if (ne $customType "") -}}
{{$customType}}
{{- else if isMapType $type -}}
map[{{mapKeyType $type}}]{{template "field" dict "Name" $name "Type" (mapValueType $type) "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $typeMeta}}
{{- else if isListType $type -}}
[]{{template "field" dict "Name" $name "Type" (listElemType $type) "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $typeMeta}}
{{- else if isCoreType $type -}}
{{if $optional}}*{{end}}{{ get $typeMap $type }}
{{- else -}}{{- /* structs */ -}}
{{- if isEnumType $type -}}
{{if $optional}}*{{end}}{{$typePrefix}}{{$type}}
{{- else -}}
*{{$typePrefix}}{{$type}}
{{- end -}}
{{- end -}}
{{- if $includeJsonTags -}}
{{" "}}`{{$jsonTag}}{{if and $includeStructTags (len $structTags)}} {{join (sort $structTags) " "}}{{end}}`
{{- end -}}
{{- end -}}