-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_tmpl.go
85 lines (71 loc) · 2.51 KB
/
model_tmpl.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
package main
const modelTmpl = `// Code generated by go-entity, DO NOT EDIT.
{{ $TableName := .BigCamelName }}
{{ $FirstLetter := .FirstLetter }}
package model
{{ .Imports }}
// {{ $TableName }} {{ .Comment }}
type {{ $TableName }} struct {
{{ range $index, $field := .Fields }}{{ $field.BigCamelName }} {{ $field.DataType }} {{ $field.Tag }} // {{ $field.Comment }}
{{ end }}}
func ({{ $FirstLetter }} *{{ $TableName }}) TableName() string {
{{ if .IsSplit }} return fmt.Sprintf("%s_%v", "{{ .Name }}", {{ $FirstLetter }}.{{ .PriField.BigCamelName }}%{{ .MOD }}) {{ else }} return "{{ .Name }}" {{ end }}
}
{{ $SliceStruct := printf "%sSlice" $TableName }}
type {{ $SliceStruct }} []*{{ $TableName }}
{{ range $index, $field := .Fields }}
{{ if eq $field.ColKey "PRI" }}
func ({{ $FirstLetter }} *{{ $SliceStruct }}) {{ $field.BigCamelName }}Map() map[{{ $field.DataType }}]*{{ $TableName }} {
uni := make(map[{{ $field.DataType }}]*{{ $TableName }})
for _, item := range *{{ $FirstLetter }} {
uni[item.{{ $field.BigCamelName }}] = item
}
return uni
}
{{ else }}
func ({{ $FirstLetter }} *{{ $SliceStruct }}) GroupBy{{ $field.BigCamelName }}() map[{{ $field.DataType }}]{{ $SliceStruct }} {
res := make(map[{{ $field.DataType }}]{{ $SliceStruct }})
for _, item := range *{{ $FirstLetter }} {
res[item.{{ $field.BigCamelName }}] = append(res[item.{{ $field.BigCamelName }}], item)
}
return res
}
{{ end }}
{{ end }}
{{ range $index, $field := .Fields }}
func ({{ $FirstLetter }} *{{ $SliceStruct }}) Pluck{{ $field.BigCamelName }}() []{{ $field.DataType }} {
res := make([]{{ $field.DataType }}, 0, len(*{{ $FirstLetter }}))
for _, item := range *{{ $FirstLetter }} {
res = append(res, item.{{ $field.BigCamelName }})
}
return res
}
{{ end }}
{{ range $index, $field := .Fields }}
func ({{ $FirstLetter }} *{{ $SliceStruct }}) Unique{{ $field.BigCamelName }}() []{{ $field.DataType }} {
uni := make(map[{{ $field.DataType }}]struct{})
res := make([]{{ $field.DataType }}, 0)
for _, item := range *{{ $FirstLetter }} {
uni[item.{{ $field.BigCamelName }}] = struct{}{}
}
for key := range uni {
res = append(res, key)
}
return res
}
{{ end }}
`
const modelFieldTmpl = `// Code generated by go-entity, DO NOT EDIT.
package {{ .PackageName }}
const (
{{ range $index, $field := .Fields }}{{ $field.BigCamelName }} = "{{ $field.Name }}"
{{ end }}
)
func Columns() []string {
return []string{
{{ range $index, $field := .Fields }}{{ $field.BigCamelName }},
{{ end }}
}
}
const TableName = "{{ .Name }}"
`