/
type.go
81 lines (67 loc) · 1.54 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
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
package golang
import (
"github.com/zyra/gots/pkg/parser/reader"
"go/ast"
"go/token"
"strings"
)
// Package import options
type Import struct {
// Package path
//
// This might not be populated immediately
// since we need access to the file imports to find the path
Package string `json:"package"`
// Name of import
//
// Might be alias or real package name
Name string `json:"alias"`
}
// Returns a type with the token's string representation as the name
func TypeFromToken(t token.Token) *reader.Type {
return &reader.Type{
Name: strings.ToLower(t.String()),
}
}
// Returns a Type with the given name
func TypeFromName(n string) *reader.Type {
return &reader.Type{Name: n}
}
// Parses ast.Expr and returns a Type
func TypeFromExpr(t ast.Expr) *reader.Type {
switch v := t.(type) {
case *ast.Ident:
if v.Name == "byte" {
return &reader.Type{Generic: true}
}
return TypeFromName(v.Name)
case *ast.StarExpr:
t := TypeFromExpr(v.X)
t.Pointer = true
return t
case *ast.MapType:
k, vv := TypeFromExpr(v.Key), TypeFromExpr(v.Value)
return &reader.Type{
Map: true,
MapKey: k,
MapValue: vv,
}
case *ast.ArrayType:
if v, ok := v.Elt.(*ast.Ident); ok && v.Name == "byte" {
return &reader.Type{Generic: true}
}
return &reader.Type{
Name: TypeFromExpr(v.Elt).Name,
Array: true,
}
case *ast.SelectorExpr:
return &reader.Type{
Name: v.Sel.Name,
From: v.X.(*ast.Ident).Name,
}
case *ast.InterfaceType:
return &reader.Type{Generic: true}
default:
return &reader.Type{Generic: true}
}
}