-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_identifier.go
151 lines (122 loc) · 4.26 KB
/
value_identifier.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package codegen
import (
"fmt"
"strings"
)
type identifierValue struct {
declaration *nameHelper
isAddress bool
}
// Identifier creates a new identifier (variable, value, etc.)
func Identifier(name string) *identifierValue {
return &identifierValue{
declaration: newNameHelper("", name),
}
}
// Identifier creates a new identifier with a package alias (variable, value, etc.)
func QualIdentifier(alias, name string) *identifierValue {
return &identifierValue{
declaration: newNameHelper(alias, name),
}
}
// Pointer dereferences the identifier
func (i *identifierValue) Pointer() *identifierValue {
i.SetIsPointer(true)
return i
}
// SetIsPointer sets whether or not an identifier is a pointer
func (i *identifierValue) SetIsPointer(isPointer bool) *identifierValue {
i.declaration.setIsPointer(isPointer)
return i
}
// Address turns the identifier into an address type (pointer to the identifier)
func (i *identifierValue) Address() *identifierValue {
i.isAddress = true
return i
}
// Err creates a new identifier named `err`
func Err() *identifierValue {
return Identifier("err")
}
// Nil creates a new identifier named `nil`
func Nil() *identifierValue {
return Identifier("nil")
}
// String creates a new stiring value identifier
func String(strValue string) *identifierValue {
return Identifier(fmt.Sprintf("\"%s\"", strValue))
}
// Int creates a new integer (int32) value identifier
func Int(intVal int) *identifierValue {
return Identifier(fmt.Sprintf("%d", intVal))
}
// Field appends a new field getter after the identifier
func (i *identifierValue) Field(fieldName string) *fieldValue {
return newField(i, fieldName)
}
// Call appends a new function call after the identifier
func (i *identifierValue) Call(funcName string) *callValue {
return newCallValue(i, funcName)
}
// Cast casts the identifier to the specified type
func (i *identifierValue) Cast(typeName string) *castValue {
return newCastValue(i, "", typeName, false)
}
// CastPointer casts the identifier to a pointer of the specified type
func (i *identifierValue) CastPointer(typeName string) *castValue {
return newCastValue(i, "", typeName, true)
}
// CastQual casts the identifier to the specified type with an alias
func (i *identifierValue) CastQual(alias, typeName string) *castValue {
return newCastValue(i, alias, typeName, false)
}
// CastPointer casts the identifier to a pointer of the specified type with an alias
func (i *identifierValue) CastQualPointer(alias, typeName string) *castValue {
return newCastValue(i, alias, typeName, true)
}
// Assign assigns a value to the identifier
func (i *identifierValue) Assign(val Value) *assignStmt {
return newAssignment(i, val)
}
// Equals compares a value of the identifier for equality
func (i *identifierValue) Equals(value Value) *comparisonValue {
return newEquals(i, value, cmpType_Equals)
}
// NotEquals compares a value of the identifier for not being equal
func (i *identifierValue) NotEquals(value Value) *comparisonValue {
return newEquals(i, value, cmpType_NotEquals)
}
// LessThan compares a value of the identifier for being less
func (i *identifierValue) LessThan(value Value) *comparisonValue {
return newEquals(i, value, cmpType_LessThan)
}
// IsNil checks whether or not the identifier is nil
func (i *identifierValue) IsNil() *comparisonValue {
return newEquals(i, Nil(), cmpType_Equals)
}
// IsNil checks whether or not the identifier is not nil
func (i *identifierValue) IsNotNil() *comparisonValue {
return newEquals(i, Nil(), cmpType_NotEquals)
}
// IsNotEmpty checks whether or not the identifier's length is empty
func (i *identifierValue) IsNotEmpty() *comparisonValue {
return Len(i).NotEquals(Int(0))
}
// Increment increments the identifier (++ syntax)
func (i *identifierValue) Increment() *incrementStmt {
return newIncrement(i)
}
// AtIndex access a value at the specified index
func (i *identifierValue) AtIndex(value Value) *squireBracketsValue {
return newSquireBrackets(i, value)
}
// Append creates an append block for the identifier
func (i *identifierValue) Append(value Value) *assignStmt {
return i.Assign(Append(i, value))
}
func (i *identifierValue) writeValue(sb *strings.Builder) {
writeAddressValueAccess(sb, i.declaration.wr, i.isAddress)
}
func (i *identifierValue) isPointer() bool {
return i.declaration.isPointer
}