-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
89 lines (76 loc) · 1.96 KB
/
field.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
package main
import (
"fmt"
"go/ast"
"go/types"
"strings"
)
// tag name
const (
constructorTag = "constructor"
getterTag = "getter"
setterTag = "setter"
)
// field elements required to create a constructor
type field struct {
FieldName string
FieldType string
ConstructorIgnore bool
GetterIgnore bool
SetterIgnore bool
}
// searchFiled extract the elements necessary to create a constructor from ast.StructType.
func searchFiled(structType *ast.StructType) ([]field, error) {
var fields []field
for _, list := range structType.Fields.List {
f := field{}
if list.Tag == nil {
f.FieldName = list.Names[0].Name
f.FieldType = types.ExprString(list.Type)
f.ConstructorIgnore = false
f.GetterIgnore = false
f.SetterIgnore = false
} else {
value := strings.Trim(list.Tag.Value, "`")
// if the string "goor" is not included
if !strings.Contains(value, cliName) {
continue
}
// by getting the position where the tag key name "goor" starts and adding +5 to it, the
// then you can get "constructor:-" of "goor: "constructor:-".
tag := value[strings.Index(value, cliName)+5:]
tag = strings.Trim(tag, "\"")
var tags []string
if strings.Contains(tag, ";") {
tags = strings.Split(tag, ";")
} else {
tags = []string{tag}
}
for _, t := range tags {
switch {
case strings.Contains(t, constructorTag):
if t[len(constructorTag)+1:] == "-" {
f.ConstructorIgnore = true
}
case strings.Contains(t, getterTag):
if t[len(getterTag)+1:] == "-" {
f.GetterIgnore = true
}
case strings.Contains(t, setterTag):
if t[len(setterTag)+1:] == "-" {
f.SetterIgnore = true
}
default:
// don't do anything.
}
}
f.FieldName = list.Names[0].Name
f.FieldType = types.ExprString(list.Type)
}
fields = append(fields, f)
}
if len(fields) == 0 {
return fields, fmt.Errorf("fields is not found")
}
return fields, nil
}