-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray.go
100 lines (87 loc) · 1.69 KB
/
array.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
90
91
92
93
94
95
96
97
98
99
100
package humanize
import (
"fmt"
"go/ast"
"strconv"
)
// ArrayType is the base array
type ArrayType struct {
pkg *Package
Slice bool
Len int
Type Type
}
// EllipsisType is slice type but with ...type definition
type EllipsisType struct {
*ArrayType
}
// Equal check array type equality
func (a *ArrayType) Equal(t Type) bool {
if !a.pkg.Equal(t.Package()) {
return false
}
v, ok := t.(*ArrayType)
if !ok {
return false
}
if a.Slice != v.Slice {
return false
}
if a.Len != v.Len {
return false
}
return a.Type.Equal(v.Type)
}
// String represent array in string
func (a *ArrayType) String() string {
if a.Slice {
return "[]" + a.Type.String()
}
return fmt.Sprintf("[%d]%s", a.Len, a.Type.String())
}
// Package return the array package
func (a *ArrayType) Package() *Package {
return a.pkg
}
func (a *ArrayType) lateBind() error {
return lateBind(a.Type)
}
// String represent ellipsis array in string
func (e *EllipsisType) String() string {
return fmt.Sprintf("[...]%s{}", e.Type.String())
}
// Equal if two ellipsis array are equal
func (e EllipsisType) Equal(t Type) bool {
if v, ok := t.(*EllipsisType); ok {
return e.ArrayType.Equal(v.ArrayType)
}
return false
}
func getArray(p *Package, f *File, t *ast.ArrayType) Type {
slice := t.Len == nil
ellipsis := false
l := 0
if !slice {
var (
ls string
)
switch t.Len.(type) {
case *ast.BasicLit:
ls = t.Len.(*ast.BasicLit).Value
case *ast.Ellipsis:
ls = "0"
ellipsis = true
}
l, _ = strconv.Atoi(ls)
}
var at Type = &ArrayType{
pkg: p,
Slice: t.Len == nil,
Len: l,
Type: newType(p, f, t.Elt),
}
if ellipsis {
at = &EllipsisType{ArrayType: at.(*ArrayType)}
}
return at
}