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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package format
// field stores the minimum necessary
// data for iterating and formatting
// each field in a given struct.
type field struct {
format FormatFunc
name string
offset uintptr
}
// iterStructType returns a FormatFunc capable of iterating
// and formatting the given struct type currently in typenode{}.
// note this will fetch sub-FormatFuncs for each struct field.
func (fmt *Formatter) iterStructType(t typenode) FormatFunc {
// Number of struct fields.
n := t.rtype.NumField()
// Gather format functions.
fields := make([]field, n)
for i := 0; i < n; i++ {
// Get struct field at index.
sfield := t.rtype.Field(i)
rtype := sfield.Type
// Get nested field typenode with appropriate flags.
flags := reflect_struct_field_flags(t.flags, rtype)
ft := t.next(sfield.Type, flags)
// Get field format func.
fn := fmt.loadOrGet(ft)
if fn == nil {
panic("unreachable")
}
// Set field info.
fields[i] = field{
format: fn,
name: sfield.Name,
offset: sfield.Offset,
}
}
// Handle no. fields.
switch len(fields) {
case 0:
return emptyStructType(t)
case 1:
return iterSingleFieldStructType(t, fields[0])
default:
return iterMultiFieldStructType(t, fields)
}
}
func emptyStructType(t typenode) FormatFunc {
if !t.needs_typestr() {
return func(s *State) {
// Append empty object.
s.B = append(s.B, "{}"...)
}
}
// Struct type string with refs.
typestr := t.typestr_with_refs()
// Append empty object
// with type information.
return func(s *State) {
if s.A.WithType() {
s.B = append(s.B, typestr...)
}
s.B = append(s.B, "{}"...)
}
}
func iterSingleFieldStructType(t typenode, field field) FormatFunc {
if field.format == nil {
panic("nil func")
}
if !t.needs_typestr() {
return func(s *State) {
// Wrap 'fn' with braces + field name.
s.B = append(s.B, "{"+field.name+"="...)
field.format(s)
s.B = append(s.B, "}"...)
}
}
// Struct type string with refs.
typestr := t.typestr_with_refs()
return func(s *State) {
// Include type info.
if s.A.WithType() {
s.B = append(s.B, typestr...)
}
// Wrap 'fn' with braces + field name.
s.B = append(s.B, "{"+field.name+"="...)
field.format(s)
s.B = append(s.B, "}"...)
}
}
func iterMultiFieldStructType(t typenode, fields []field) FormatFunc {
for _, field := range fields {
if field.format == nil {
panic("nil func")
}
}
if !t.needs_typestr() {
return func(s *State) {
ptr := s.P
// Prepend object brace.
s.B = append(s.B, '{')
for i := 0; i < len(fields); i++ {
// Get struct field ptr via offset.
s.P = add(ptr, fields[i].offset)
// Append field name and value separator.
s.B = append(s.B, fields[i].name+"="...)
// Format i'th field.
fields[i].format(s)
s.B = append(s.B, ',', ' ')
}
// Drop final ", ".
s.B = s.B[:len(s.B)-2]
// Append object brace.
s.B = append(s.B, '}')
}
}
// Struct type string with refs.
typestr := t.typestr_with_refs()
return func(s *State) {
ptr := s.P
// Include type info.
if s.A.WithType() {
s.B = append(s.B, typestr...)
}
// Prepend object brace.
s.B = append(s.B, '{')
for i := 0; i < len(fields); i++ {
// Get struct field ptr via offset.
s.P = add(ptr, fields[i].offset)
// Append field name and value separator.
s.B = append(s.B, fields[i].name+"="...)
// Format i'th field.
fields[i].format(s)
s.B = append(s.B, ',', ' ')
}
// Drop final ", ".
s.B = s.B[:len(s.B)-2]
// Append object brace.
s.B = append(s.B, '}')
}
}
|