saving some bytes uint8 vs int
joeybloggs
7 years ago
152 | 152 | ctag = new(cTag) |
153 | 153 | } |
154 | 154 | |
155 | cs.fields[i] = &cField{idx: i, name: fld.Name, altName: customName, cTags: ctag, namesEqual: fld.Name == customName} | |
155 | cs.fields[i] = &cField{ | |
156 | idx: i, | |
157 | name: fld.Name, | |
158 | altName: customName, | |
159 | cTags: ctag, | |
160 | namesEqual: fld.Name == customName, | |
161 | } | |
156 | 162 | } |
157 | 163 | |
158 | 164 | v.structCache.Set(typ, cs) |
131 | 131 | actualTag string |
132 | 132 | ns string |
133 | 133 | structNs string |
134 | fieldLen int | |
135 | structfieldLen int | |
136 | // field string | |
137 | // structField string | |
138 | value interface{} | |
139 | param string | |
140 | kind reflect.Kind | |
141 | typ reflect.Type | |
134 | fieldLen uint8 | |
135 | structfieldLen uint8 | |
136 | value interface{} | |
137 | param string | |
138 | kind reflect.Kind | |
139 | typ reflect.Type | |
142 | 140 | } |
143 | 141 | |
144 | 142 | // Tag returns the validation tag that failed. |
168 | 166 | // fields actual name. |
169 | 167 | func (fe *fieldError) Field() string { |
170 | 168 | // return fe.field |
171 | return fe.ns[len(fe.ns)-fe.fieldLen:] | |
169 | return fe.ns[len(fe.ns)-int(fe.fieldLen):] | |
172 | 170 | } |
173 | 171 | |
174 | 172 | // returns the fields actual name from the struct, when able to determine. |
175 | 173 | func (fe *fieldError) StructField() string { |
176 | 174 | // return fe.structField |
177 | return fe.structNs[len(fe.structNs)-fe.structfieldLen:] | |
175 | return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):] | |
178 | 176 | } |
179 | 177 | |
180 | 178 | // Value returns the actual fields value in case needed for creating the error |
120 | 120 | actualTag: tag, |
121 | 121 | ns: v.str1, |
122 | 122 | structNs: v.str2, |
123 | fieldLen: len(fieldName), | |
124 | structfieldLen: len(structFieldName), | |
123 | fieldLen: uint8(len(fieldName)), | |
124 | structfieldLen: uint8(len(structFieldName)), | |
125 | 125 | param: param, |
126 | 126 | kind: kind, |
127 | 127 | }, |
135 | 135 | actualTag: tag, |
136 | 136 | ns: v.str1, |
137 | 137 | structNs: v.str2, |
138 | fieldLen: len(fieldName), | |
139 | structfieldLen: len(structFieldName), | |
138 | fieldLen: uint8(len(fieldName)), | |
139 | structfieldLen: uint8(len(structFieldName)), | |
140 | 140 | value: fv.Interface(), |
141 | 141 | param: param, |
142 | 142 | kind: kind, |
116 | 116 | actualTag: ct.tag, |
117 | 117 | ns: v.str1, |
118 | 118 | structNs: v.str2, |
119 | fieldLen: len(cf.altName), | |
120 | structfieldLen: len(cf.name), | |
119 | fieldLen: uint8(len(cf.altName)), | |
120 | structfieldLen: uint8(len(cf.name)), | |
121 | 121 | param: ct.param, |
122 | 122 | kind: kind, |
123 | 123 | }, |
132 | 132 | actualTag: ct.tag, |
133 | 133 | ns: v.str1, |
134 | 134 | structNs: v.str2, |
135 | fieldLen: len(cf.altName), | |
136 | structfieldLen: len(cf.name), | |
135 | fieldLen: uint8(len(cf.altName)), | |
136 | structfieldLen: uint8(len(cf.name)), | |
137 | 137 | value: current.Interface(), |
138 | 138 | param: ct.param, |
139 | 139 | kind: kind, |
326 | 326 | actualTag: ct.actualAliasTag, |
327 | 327 | ns: v.str1, |
328 | 328 | structNs: v.str2, |
329 | fieldLen: len(cf.altName), | |
330 | structfieldLen: len(cf.name), | |
329 | fieldLen: uint8(len(cf.altName)), | |
330 | structfieldLen: uint8(len(cf.name)), | |
331 | 331 | value: current.Interface(), |
332 | 332 | param: ct.param, |
333 | 333 | kind: kind, |
345 | 345 | actualTag: tVal, |
346 | 346 | ns: v.str1, |
347 | 347 | structNs: v.str2, |
348 | fieldLen: len(cf.altName), | |
349 | structfieldLen: len(cf.name), | |
348 | fieldLen: uint8(len(cf.altName)), | |
349 | structfieldLen: uint8(len(cf.name)), | |
350 | 350 | value: current.Interface(), |
351 | 351 | param: ct.param, |
352 | 352 | kind: kind, |
384 | 384 | actualTag: ct.tag, |
385 | 385 | ns: v.str1, |
386 | 386 | structNs: v.str2, |
387 | fieldLen: len(cf.altName), | |
388 | structfieldLen: len(cf.name), | |
387 | fieldLen: uint8(len(cf.altName)), | |
388 | structfieldLen: uint8(len(cf.name)), | |
389 | 389 | value: current.Interface(), |
390 | 390 | param: ct.param, |
391 | 391 | kind: kind, |