-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_type.go
60 lines (58 loc) · 1.25 KB
/
build_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
50
51
52
53
54
55
56
57
58
59
60
package golang
import (
"go/ast"
)
func BuildType(currPkg string, importMap ImportMap, t ast.Expr) (*Type, error) {
typ := &Type{
Methods: map[string]*ast.FuncDecl{},
}
switch t := t.(type) {
case *ast.Ident:
typ.IsIdent = true
typ.Ident = &Ident{
Name: t.Name,
From: importMap[t.Name],
}
case *ast.StarExpr:
baseType, err := BuildType(currPkg, importMap, t.X)
if err != nil {
return nil, err
}
typ.IsPointer = true
typ.BaseType = baseType
case *ast.ArrayType:
baseType, err := BuildType(currPkg, importMap, t.Elt)
if err != nil {
return nil, err
}
typ.IsArray = true
typ.BaseType = baseType
case *ast.MapType:
keyType, err := BuildType(currPkg, importMap, t.Key)
if err != nil {
return nil, err
}
valueType, err := BuildType(currPkg, importMap, t.Value)
if err != nil {
return nil, err
}
typ.IsMap = true
typ.KeyType = keyType
typ.ValueType = valueType
case *ast.StructType:
fields := []Field{}
for _, field := range t.Fields.List {
fieldType, err := BuildType(currPkg, importMap, field.Type)
if err != nil {
return nil, err
}
fields = append(fields, Field{
Name: field.Names[0].Name,
Type: fieldType,
})
}
typ.IsStruct = true
typ.Fields = fields
}
return typ, nil
}