-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvaluetype.go
63 lines (50 loc) · 1.19 KB
/
valuetype.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
package astrav
import (
"go/ast"
"go/types"
)
// ValueTyper provides an interface for nodes with a value type.
type ValueTyper interface {
ValueType() types.Type
}
func (s *baseNode) getType(node ast.Node) types.Type {
if node == nil {
return nil
}
ident, ok := node.(*ast.Ident)
if !ok {
return nil
}
obj := s.Info().ObjectOf(ident)
if obj == nil {
return nil
}
return s.Info().ObjectOf(ident).Type()
}
// ValueType returns the value type of the node.
func (s *Field) ValueType() types.Type {
return s.getType(s.Type)
}
// ValueType returns the value type of the node.
func (s *CompositeLit) ValueType() types.Type {
return s.getType(s.Type)
}
// ValueType returns the value type of the node.
func (s *TypeAssertExpr) ValueType() types.Type {
return s.getType(s.Type)
}
// ValueType returns the value type of the node.
func (s *ValueSpec) ValueType() types.Type {
return s.getType(s.Type)
}
// ValueType returns the value type of the node.
func (s *TypeSpec) ValueType() types.Type {
return s.getType(s.Type)
}
// ValueType returns the value type of the node.
func (s *Ident) ValueType() types.Type {
if t := s.Info().TypeOf(s); t != nil {
return t
}
return s.getType(s.node)
}