-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_field.go
85 lines (68 loc) · 2.19 KB
/
value_field.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
package codegen
import "strings"
type fieldValue struct {
val Value
name string
isAddress bool
}
// Field appends a new field getter after the field
func (f *fieldValue) Field(fieldName string) *fieldValue {
return newField(f, fieldName)
}
// Call appends a new function call after the field
func (f *fieldValue) Call(name string) *callValue {
return newCallValue(f, name)
}
// Cast casts the field to the specified type
func (f *fieldValue) Cast(typeName string) *castValue {
return newCastValue(f, "", typeName, false)
}
// CastPointer casts the field to a pointer of the specified type
func (f *fieldValue) CastPointer(typeName string) *castValue {
return newCastValue(f, "", typeName, true)
}
// CastQual casts the field to the specified type with an alias
func (f *fieldValue) CastQual(alias, typeName string) *castValue {
return newCastValue(f, alias, typeName, false)
}
// CastQualPointer casts the field to a pointer of the specified type with an alias
func (f *fieldValue) CastQualPointer(alias, typeName string) *castValue {
return newCastValue(f, alias, typeName, true)
}
// Assign assigns a value to the field
func (f *fieldValue) Assign(val Value) *assignStmt {
return newAssignment(f, val)
}
// Equals compares a value of the identifier for equality
func (f *fieldValue) Equals(value Value) *comparisonValue {
return newEquals(f, value, cmpType_Equals)
}
// Equals compares a value of the identifier for not being equal
func (f *fieldValue) NotEquals(value Value) *comparisonValue {
return newEquals(f, value, cmpType_NotEquals)
}
// Address turns the field into an address type (pointer to the field)
func (f *fieldValue) Address() *fieldValue {
f.isAddress = true
return f
}
// Append creates an append block for the field
func (f *fieldValue) Append(value Value) *assignStmt {
return f.Assign(Append(f, value))
}
func newField(val Value, name string) *fieldValue {
return &fieldValue{
val: val,
name: name,
}
}
func (f *fieldValue) writeValue(sb *strings.Builder) {
write := func(b *strings.Builder) {
writePointerValueAccess(b, f.val)
writeF(b, ".%s", f.name)
}
writeAddressValueAccess(sb, write, f.isAddress)
}
func (f *fieldValue) isPointer() bool {
return false
}