/
type.go
49 lines (39 loc) · 1.05 KB
/
type.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
package loader
import (
"go/types"
)
// StructField for the example below.
//type Foo struct {
// Name sql.NullString `json:"name"
//}
type StructField struct {
// Name of the struct field.
Name string `example:"Name"`
// Useful when the output directory doesn't match the existing ones.
PkgPath string `example:"github.com/alextanhongpin/go-codegen/test"`
PkgName string `example:"test"`
Exported bool `example:"true"`
// Stores the original position of the field in the struct.
Ordinal int
Tag string `example:"get:'Renamed'"` // To ignore getter.
Type types.Type
}
func NewStructFields(structType *types.Struct) (map[string]StructField, error) {
fields := make(map[string]StructField)
for i := 0; i < structType.NumFields(); i++ {
field := structType.Field(i)
tag := structType.Tag(i)
if skipField(tag) {
continue
}
fields[field.Name()] = StructField{
Name: field.Name(),
PkgPath: field.Pkg().Path(),
Exported: field.Exported(),
Type: field.Type(),
Tag: tag,
Ordinal: i,
}
}
return fields, nil
}