-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_type.go
98 lines (80 loc) · 2.01 KB
/
helper_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package codegen
import "strings"
type TypeDecl struct {
name *nameHelper
}
// Type creates a new type for a function
func Type(name string) *TypeDecl {
return &TypeDecl{name: newNameHelper("", name)}
}
// QualType creates a new type with an alias of an imported package
func QualType(alias, name string) *TypeDecl {
return &TypeDecl{name: newNameHelper(alias, name)}
}
// ReturnTypeError create a new type of type `error`
func ReturnTypeError() *TypeDecl {
return Type("error")
}
// GetTypeName gets a type name of the return declaration
func (t *TypeDecl) GetTypeName() string {
return t.name.identifier
}
// GetTypeAlias gets a type alias (if any) of the return declaration
func (t *TypeDecl) GetTypeAlias() string {
return t.name.alias
}
// GetIsPointer gets a flag whether or not the type is a pointer
func (t *TypeDecl) GetIsPointer() bool {
return t.name.isPointer
}
// Pointer turns the type into a pointer value
func (t *TypeDecl) Pointer() *TypeDecl {
t.SetIsPointer(true)
return t
}
// SetIsPointer sets whether or not the type is a pointer
func (t *TypeDecl) SetIsPointer(isPointer bool) *TypeDecl {
t.name.setIsPointer(isPointer)
return t
}
// Array converts the type to the array type
func (t *TypeDecl) Array() *TypeDecl {
return t.SetIsArray(true)
}
// SetIsArray sets whether or not the type is an array
func (t *TypeDecl) SetIsArray(isArray bool) *TypeDecl {
t.name.setIsArray(isArray)
return t
}
func (r *TypeDecl) wr(sb *strings.Builder) {
r.name.wr(sb)
}
func (r *TypeDecl) isValid() bool {
return r.name.isValid()
}
func writeReturnTypes(sb *strings.Builder, returnTypes []*TypeDecl) {
if count := len(returnTypes); count == 0 {
return
} else if count == 1 {
if returnTypes[0].isValid() {
returnTypes[0].wr(sb)
}
} else {
validCounter := 0
for _, r := range returnTypes {
if !r.isValid() {
continue
}
if validCounter != 0 {
sb.WriteByte(',')
} else {
sb.WriteByte('(')
}
r.wr(sb)
validCounter++
}
if validCounter != 0 {
sb.WriteByte(')')
}
}
}