| 1 |
package gen |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"reflect" |
| 9 |
"strings" |
| 10 |
"unicode" |
| 11 |
|
| 12 |
"github.com/mailru/easyjson" |
| 13 |
) |
| 14 |
|
| 15 |
// Target this byte size for initial slice allocation to reduce garbage collection. |
| 16 |
const minSliceBytes = 64 |
| 17 |
|
| 18 |
func (g *Generator) getDecoderName(t reflect.Type) string { |
| 19 |
return g.functionName("decode", t) |
| 20 |
} |
| 21 |
|
| 22 |
var primitiveDecoders = map[reflect.Kind]string{ |
| 23 |
reflect.String: "in.String()", |
| 24 |
reflect.Bool: "in.Bool()", |
| 25 |
reflect.Int: "in.Int()", |
| 26 |
reflect.Int8: "in.Int8()", |
| 27 |
reflect.Int16: "in.Int16()", |
| 28 |
reflect.Int32: "in.Int32()", |
| 29 |
reflect.Int64: "in.Int64()", |
| 30 |
reflect.Uint: "in.Uint()", |
| 31 |
reflect.Uint8: "in.Uint8()", |
| 32 |
reflect.Uint16: "in.Uint16()", |
| 33 |
reflect.Uint32: "in.Uint32()", |
| 34 |
reflect.Uint64: "in.Uint64()", |
| 35 |
reflect.Float32: "in.Float32()", |
| 36 |
reflect.Float64: "in.Float64()", |
| 37 |
} |
| 38 |
|
| 39 |
var primitiveStringDecoders = map[reflect.Kind]string{ |
| 40 |
reflect.String: "in.String()", |
| 41 |
reflect.Int: "in.IntStr()", |
| 42 |
reflect.Int8: "in.Int8Str()", |
| 43 |
reflect.Int16: "in.Int16Str()", |
| 44 |
reflect.Int32: "in.Int32Str()", |
| 45 |
reflect.Int64: "in.Int64Str()", |
| 46 |
reflect.Uint: "in.UintStr()", |
| 47 |
reflect.Uint8: "in.Uint8Str()", |
| 48 |
reflect.Uint16: "in.Uint16Str()", |
| 49 |
reflect.Uint32: "in.Uint32Str()", |
| 50 |
reflect.Uint64: "in.Uint64Str()", |
| 51 |
reflect.Uintptr: "in.UintptrStr()", |
| 52 |
reflect.Float32: "in.Float32Str()", |
| 53 |
reflect.Float64: "in.Float64Str()", |
| 54 |
} |
| 55 |
|
| 56 |
var customDecoders = map[string]string{ |
| 57 |
"json.Number": "in.JsonNumber()", |
| 58 |
} |
| 59 |
|
| 60 |
// genTypeDecoder generates decoding code for the type t, but uses unmarshaler interface if implemented by t. |
| 61 |
func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, indent int) error { |
| 62 |
ws := strings.Repeat(" ", indent) |
| 63 |
|
| 64 |
unmarshalerIface := reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem() |
| 65 |
if reflect.PtrTo(t).Implements(unmarshalerIface) { |
| 66 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 67 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 68 |
fmt.Fprintln(g.out, ws+"} else {") |
| 69 |
fmt.Fprintln(g.out, ws+" ("+out+").UnmarshalEasyJSON(in)") |
| 70 |
fmt.Fprintln(g.out, ws+"}") |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
unmarshalerIface = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem() |
| 75 |
if reflect.PtrTo(t).Implements(unmarshalerIface) { |
| 76 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 77 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 78 |
fmt.Fprintln(g.out, ws+"} else {") |
| 79 |
fmt.Fprintln(g.out, ws+" if data := in.Raw(); in.Ok() {") |
| 80 |
fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalJSON(data) )") |
| 81 |
fmt.Fprintln(g.out, ws+" }") |
| 82 |
fmt.Fprintln(g.out, ws+"}") |
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
unmarshalerIface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() |
| 87 |
if reflect.PtrTo(t).Implements(unmarshalerIface) { |
| 88 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 89 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 90 |
fmt.Fprintln(g.out, ws+"} else {") |
| 91 |
fmt.Fprintln(g.out, ws+" if data := in.UnsafeBytes(); in.Ok() {") |
| 92 |
fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") |
| 93 |
fmt.Fprintln(g.out, ws+" }") |
| 94 |
fmt.Fprintln(g.out, ws+"}") |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
err := g.genTypeDecoderNoCheck(t, out, tags, indent) |
| 99 |
return err |
| 100 |
} |
| 101 |
|
| 102 |
// returns true if the type t implements one of the custom unmarshaler interfaces |
| 103 |
func hasCustomUnmarshaler(t reflect.Type) bool { |
| 104 |
t = reflect.PtrTo(t) |
| 105 |
return t.Implements(reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem()) || |
| 106 |
t.Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) || |
| 107 |
t.Implements(reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()) |
| 108 |
} |
| 109 |
|
| 110 |
func hasUnknownsUnmarshaler(t reflect.Type) bool { |
| 111 |
t = reflect.PtrTo(t) |
| 112 |
return t.Implements(reflect.TypeOf((*easyjson.UnknownsUnmarshaler)(nil)).Elem()) |
| 113 |
} |
| 114 |
|
| 115 |
func hasUnknownsMarshaler(t reflect.Type) bool { |
| 116 |
t = reflect.PtrTo(t) |
| 117 |
return t.Implements(reflect.TypeOf((*easyjson.UnknownsMarshaler)(nil)).Elem()) |
| 118 |
} |
| 119 |
|
| 120 |
// genTypeDecoderNoCheck generates decoding code for the type t. |
| 121 |
func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags fieldTags, indent int) error { |
| 122 |
ws := strings.Repeat(" ", indent) |
| 123 |
// Check whether type is primitive, needs to be done after interface check. |
| 124 |
if dec := customDecoders[t.String()]; dec != "" { |
| 125 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 126 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 127 |
fmt.Fprintln(g.out, ws+"} else {") |
| 128 |
fmt.Fprintln(g.out, ws+" "+out+" = "+dec) |
| 129 |
fmt.Fprintln(g.out, ws+"}") |
| 130 |
return nil |
| 131 |
} else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString { |
| 132 |
if tags.intern && t.Kind() == reflect.String { |
| 133 |
dec = "in.StringIntern()" |
| 134 |
} |
| 135 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 136 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 137 |
fmt.Fprintln(g.out, ws+"} else {") |
| 138 |
fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"("+dec+")") |
| 139 |
fmt.Fprintln(g.out, ws+"}") |
| 140 |
return nil |
| 141 |
} else if dec := primitiveDecoders[t.Kind()]; dec != "" { |
| 142 |
if tags.intern && t.Kind() == reflect.String { |
| 143 |
dec = "in.StringIntern()" |
| 144 |
} |
| 145 |
if tags.noCopy && t.Kind() == reflect.String { |
| 146 |
dec = "in.UnsafeString()" |
| 147 |
} |
| 148 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 149 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 150 |
fmt.Fprintln(g.out, ws+"} else {") |
| 151 |
fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"("+dec+")") |
| 152 |
fmt.Fprintln(g.out, ws+"}") |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
switch t.Kind() { |
| 157 |
case reflect.Slice: |
| 158 |
tmpVar := g.uniqueVarName() |
| 159 |
elem := t.Elem() |
| 160 |
|
| 161 |
if elem.Kind() == reflect.Uint8 && elem.Name() == "uint8" { |
| 162 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 163 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 164 |
fmt.Fprintln(g.out, ws+" "+out+" = nil") |
| 165 |
fmt.Fprintln(g.out, ws+"} else {") |
| 166 |
if g.simpleBytes { |
| 167 |
fmt.Fprintln(g.out, ws+" "+out+" = []byte(in.String())") |
| 168 |
} else { |
| 169 |
fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()") |
| 170 |
} |
| 171 |
|
| 172 |
fmt.Fprintln(g.out, ws+"}") |
| 173 |
|
| 174 |
} else { |
| 175 |
|
| 176 |
capacity := 1 |
| 177 |
if elem.Size() > 0 { |
| 178 |
capacity = minSliceBytes / int(elem.Size()) |
| 179 |
} |
| 180 |
|
| 181 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 182 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 183 |
fmt.Fprintln(g.out, ws+" "+out+" = nil") |
| 184 |
fmt.Fprintln(g.out, ws+"} else {") |
| 185 |
fmt.Fprintln(g.out, ws+" in.Delim('[')") |
| 186 |
fmt.Fprintln(g.out, ws+" if "+out+" == nil {") |
| 187 |
fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {") |
| 188 |
fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")") |
| 189 |
fmt.Fprintln(g.out, ws+" } else {") |
| 190 |
fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}") |
| 191 |
fmt.Fprintln(g.out, ws+" }") |
| 192 |
fmt.Fprintln(g.out, ws+" } else { ") |
| 193 |
fmt.Fprintln(g.out, ws+" "+out+" = ("+out+")[:0]") |
| 194 |
fmt.Fprintln(g.out, ws+" }") |
| 195 |
fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") |
| 196 |
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) |
| 197 |
|
| 198 |
if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
|
| 202 |
fmt.Fprintln(g.out, ws+" "+out+" = append("+out+", "+tmpVar+")") |
| 203 |
fmt.Fprintln(g.out, ws+" in.WantComma()") |
| 204 |
fmt.Fprintln(g.out, ws+" }") |
| 205 |
fmt.Fprintln(g.out, ws+" in.Delim(']')") |
| 206 |
fmt.Fprintln(g.out, ws+"}") |
| 207 |
} |
| 208 |
|
| 209 |
case reflect.Array: |
| 210 |
iterVar := g.uniqueVarName() |
| 211 |
elem := t.Elem() |
| 212 |
|
| 213 |
if elem.Kind() == reflect.Uint8 && elem.Name() == "uint8" { |
| 214 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 215 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 216 |
fmt.Fprintln(g.out, ws+"} else {") |
| 217 |
fmt.Fprintln(g.out, ws+" copy("+out+"[:], in.Bytes())") |
| 218 |
fmt.Fprintln(g.out, ws+"}") |
| 219 |
|
| 220 |
} else { |
| 221 |
|
| 222 |
length := t.Len() |
| 223 |
|
| 224 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 225 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 226 |
fmt.Fprintln(g.out, ws+"} else {") |
| 227 |
fmt.Fprintln(g.out, ws+" in.Delim('[')") |
| 228 |
fmt.Fprintln(g.out, ws+" "+iterVar+" := 0") |
| 229 |
fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") |
| 230 |
fmt.Fprintln(g.out, ws+" if "+iterVar+" < "+fmt.Sprint(length)+" {") |
| 231 |
|
| 232 |
if err := g.genTypeDecoder(elem, "("+out+")["+iterVar+"]", tags, indent+3); err != nil { |
| 233 |
return err |
| 234 |
} |
| 235 |
|
| 236 |
fmt.Fprintln(g.out, ws+" "+iterVar+"++") |
| 237 |
fmt.Fprintln(g.out, ws+" } else {") |
| 238 |
fmt.Fprintln(g.out, ws+" in.SkipRecursive()") |
| 239 |
fmt.Fprintln(g.out, ws+" }") |
| 240 |
fmt.Fprintln(g.out, ws+" in.WantComma()") |
| 241 |
fmt.Fprintln(g.out, ws+" }") |
| 242 |
fmt.Fprintln(g.out, ws+" in.Delim(']')") |
| 243 |
fmt.Fprintln(g.out, ws+"}") |
| 244 |
} |
| 245 |
|
| 246 |
case reflect.Struct: |
| 247 |
dec := g.getDecoderName(t) |
| 248 |
g.addType(t) |
| 249 |
|
| 250 |
if len(out) > 0 && out[0] == '*' { |
| 251 |
// NOTE: In order to remove an extra reference to a pointer |
| 252 |
fmt.Fprintln(g.out, ws+dec+"(in, "+out[1:]+")") |
| 253 |
} else { |
| 254 |
fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")") |
| 255 |
} |
| 256 |
|
| 257 |
case reflect.Ptr: |
| 258 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 259 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 260 |
fmt.Fprintln(g.out, ws+" "+out+" = nil") |
| 261 |
fmt.Fprintln(g.out, ws+"} else {") |
| 262 |
fmt.Fprintln(g.out, ws+" if "+out+" == nil {") |
| 263 |
fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")") |
| 264 |
fmt.Fprintln(g.out, ws+" }") |
| 265 |
|
| 266 |
if err := g.genTypeDecoder(t.Elem(), "*"+out, tags, indent+1); err != nil { |
| 267 |
return err |
| 268 |
} |
| 269 |
|
| 270 |
fmt.Fprintln(g.out, ws+"}") |
| 271 |
|
| 272 |
case reflect.Map: |
| 273 |
key := t.Key() |
| 274 |
keyDec, ok := primitiveStringDecoders[key.Kind()] |
| 275 |
if !ok && !hasCustomUnmarshaler(key) { |
| 276 |
return fmt.Errorf("map type %v not supported: only string and integer keys and types implementing json.Unmarshaler are allowed", key) |
| 277 |
} // else assume the caller knows what they are doing and that the custom unmarshaler performs the translation from string or integer keys to the key type |
| 278 |
elem := t.Elem() |
| 279 |
tmpVar := g.uniqueVarName() |
| 280 |
keepEmpty := tags.required || tags.noOmitEmpty || (!g.omitEmpty && !tags.omitEmpty) |
| 281 |
|
| 282 |
fmt.Fprintln(g.out, ws+"if in.IsNull() {") |
| 283 |
fmt.Fprintln(g.out, ws+" in.Skip()") |
| 284 |
fmt.Fprintln(g.out, ws+"} else {") |
| 285 |
fmt.Fprintln(g.out, ws+" in.Delim('{')") |
| 286 |
if !keepEmpty { |
| 287 |
fmt.Fprintln(g.out, ws+" if !in.IsDelim('}') {") |
| 288 |
} |
| 289 |
fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+")") |
| 290 |
if !keepEmpty { |
| 291 |
fmt.Fprintln(g.out, ws+" } else {") |
| 292 |
fmt.Fprintln(g.out, ws+" "+out+" = nil") |
| 293 |
fmt.Fprintln(g.out, ws+" }") |
| 294 |
} |
| 295 |
|
| 296 |
fmt.Fprintln(g.out, ws+" for !in.IsDelim('}') {") |
| 297 |
// NOTE: extra check for TextUnmarshaler. It overrides default methods. |
| 298 |
if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()) { |
| 299 |
fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) |
| 300 |
fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") |
| 301 |
fmt.Fprintln(g.out, ws+" in.AddError(key.UnmarshalText(data) )") |
| 302 |
fmt.Fprintln(g.out, ws+"}") |
| 303 |
} else if keyDec != "" { |
| 304 |
fmt.Fprintln(g.out, ws+" key := "+g.getType(key)+"("+keyDec+")") |
| 305 |
} else { |
| 306 |
fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) |
| 307 |
if err := g.genTypeDecoder(key, "key", tags, indent+2); err != nil { |
| 308 |
return err |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
fmt.Fprintln(g.out, ws+" in.WantColon()") |
| 313 |
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) |
| 314 |
|
| 315 |
if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { |
| 316 |
return err |
| 317 |
} |
| 318 |
|
| 319 |
fmt.Fprintln(g.out, ws+" ("+out+")[key] = "+tmpVar) |
| 320 |
fmt.Fprintln(g.out, ws+" in.WantComma()") |
| 321 |
fmt.Fprintln(g.out, ws+" }") |
| 322 |
fmt.Fprintln(g.out, ws+" in.Delim('}')") |
| 323 |
fmt.Fprintln(g.out, ws+"}") |
| 324 |
|
| 325 |
case reflect.Interface: |
| 326 |
if t.NumMethod() != 0 { |
| 327 |
if g.interfaceIsEasyjsonUnmarshaller(t) { |
| 328 |
fmt.Fprintln(g.out, ws+out+".UnmarshalEasyJSON(in)") |
| 329 |
} else if g.interfaceIsJsonUnmarshaller(t) { |
| 330 |
fmt.Fprintln(g.out, ws+out+".UnmarshalJSON(in.Raw())") |
| 331 |
} else { |
| 332 |
return fmt.Errorf("interface type %v not supported: only interface{} and easyjson/json Unmarshaler are allowed", t) |
| 333 |
} |
| 334 |
} else { |
| 335 |
fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {") |
| 336 |
fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)") |
| 337 |
fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {") |
| 338 |
fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())") |
| 339 |
fmt.Fprintln(g.out, ws+"} else {") |
| 340 |
fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()") |
| 341 |
fmt.Fprintln(g.out, ws+"}") |
| 342 |
} |
| 343 |
default: |
| 344 |
return fmt.Errorf("don't know how to decode %v", t) |
| 345 |
} |
| 346 |
return nil |
| 347 |
} |
| 348 |
|
| 349 |
func (g *Generator) interfaceIsEasyjsonUnmarshaller(t reflect.Type) bool { |
| 350 |
return t.Implements(reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem()) |
| 351 |
} |
| 352 |
|
| 353 |
func (g *Generator) interfaceIsJsonUnmarshaller(t reflect.Type) bool { |
| 354 |
return t.Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) |
| 355 |
} |
| 356 |
|
| 357 |
func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) error { |
| 358 |
jsonName := g.fieldNamer.GetJSONFieldName(t, f) |
| 359 |
tags := parseFieldTags(f) |
| 360 |
|
| 361 |
if tags.omit { |
| 362 |
return nil |
| 363 |
} |
| 364 |
if tags.intern && tags.noCopy { |
| 365 |
return errors.New("Mutually exclusive tags are specified: 'intern' and 'nocopy'") |
| 366 |
} |
| 367 |
|
| 368 |
fmt.Fprintf(g.out, " case %q:\n", jsonName) |
| 369 |
if err := g.genTypeDecoder(f.Type, "out."+f.Name, tags, 3); err != nil { |
| 370 |
return err |
| 371 |
} |
| 372 |
|
| 373 |
if tags.required { |
| 374 |
fmt.Fprintf(g.out, "%sSet = true\n", f.Name) |
| 375 |
} |
| 376 |
|
| 377 |
return nil |
| 378 |
} |
| 379 |
|
| 380 |
func (g *Generator) genRequiredFieldSet(t reflect.Type, f reflect.StructField) { |
| 381 |
tags := parseFieldTags(f) |
| 382 |
|
| 383 |
if !tags.required { |
| 384 |
return |
| 385 |
} |
| 386 |
|
| 387 |
fmt.Fprintf(g.out, "var %sSet bool\n", f.Name) |
| 388 |
} |
| 389 |
|
| 390 |
func (g *Generator) genRequiredFieldCheck(t reflect.Type, f reflect.StructField) { |
| 391 |
jsonName := g.fieldNamer.GetJSONFieldName(t, f) |
| 392 |
tags := parseFieldTags(f) |
| 393 |
|
| 394 |
if !tags.required { |
| 395 |
return |
| 396 |
} |
| 397 |
|
| 398 |
g.imports["fmt"] = "fmt" |
| 399 |
|
| 400 |
fmt.Fprintf(g.out, "if !%sSet {\n", f.Name) |
| 401 |
fmt.Fprintf(g.out, " in.AddError(fmt.Errorf(\"key '%s' is required\"))\n", jsonName) |
| 402 |
fmt.Fprintf(g.out, "}\n") |
| 403 |
} |
| 404 |
|
| 405 |
func mergeStructFields(fields1, fields2 []reflect.StructField) (fields []reflect.StructField) { |
| 406 |
used := map[string]bool{} |
| 407 |
for _, f := range fields2 { |
| 408 |
used[f.Name] = true |
| 409 |
fields = append(fields, f) |
| 410 |
} |
| 411 |
|
| 412 |
for _, f := range fields1 { |
| 413 |
if !used[f.Name] { |
| 414 |
fields = append(fields, f) |
| 415 |
} |
| 416 |
} |
| 417 |
return |
| 418 |
} |
| 419 |
|
| 420 |
func getStructFields(t reflect.Type) ([]reflect.StructField, error) { |
| 421 |
if t.Kind() != reflect.Struct { |
| 422 |
return nil, fmt.Errorf("got %v; expected a struct", t) |
| 423 |
} |
| 424 |
|
| 425 |
var efields []reflect.StructField |
| 426 |
var fields []reflect.StructField |
| 427 |
for i := 0; i < t.NumField(); i++ { |
| 428 |
f := t.Field(i) |
| 429 |
tags := parseFieldTags(f) |
| 430 |
if !f.Anonymous || tags.name != "" { |
| 431 |
continue |
| 432 |
} |
| 433 |
|
| 434 |
t1 := f.Type |
| 435 |
if t1.Kind() == reflect.Ptr { |
| 436 |
t1 = t1.Elem() |
| 437 |
} |
| 438 |
|
| 439 |
if t1.Kind() == reflect.Struct { |
| 440 |
fs, err := getStructFields(t1) |
| 441 |
if err != nil { |
| 442 |
return nil, fmt.Errorf("error processing embedded field: %v", err) |
| 443 |
} |
| 444 |
efields = mergeStructFields(efields, fs) |
| 445 |
} else if (t1.Kind() >= reflect.Bool && t1.Kind() < reflect.Complex128) || t1.Kind() == reflect.String { |
| 446 |
if strings.Contains(f.Name, ".") || unicode.IsUpper([]rune(f.Name)[0]) { |
| 447 |
fields = append(fields, f) |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
for i := 0; i < t.NumField(); i++ { |
| 453 |
f := t.Field(i) |
| 454 |
tags := parseFieldTags(f) |
| 455 |
if f.Anonymous && tags.name == "" { |
| 456 |
continue |
| 457 |
} |
| 458 |
|
| 459 |
c := []rune(f.Name)[0] |
| 460 |
if unicode.IsUpper(c) { |
| 461 |
fields = append(fields, f) |
| 462 |
} |
| 463 |
} |
| 464 |
return mergeStructFields(efields, fields), nil |
| 465 |
} |
| 466 |
|
| 467 |
func (g *Generator) genDecoder(t reflect.Type) error { |
| 468 |
switch t.Kind() { |
| 469 |
case reflect.Slice, reflect.Array, reflect.Map: |
| 470 |
return g.genSliceArrayDecoder(t) |
| 471 |
default: |
| 472 |
return g.genStructDecoder(t) |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
func (g *Generator) genSliceArrayDecoder(t reflect.Type) error { |
| 477 |
switch t.Kind() { |
| 478 |
case reflect.Slice, reflect.Array, reflect.Map: |
| 479 |
default: |
| 480 |
return fmt.Errorf("cannot generate encoder/decoder for %v, not a slice/array/map type", t) |
| 481 |
} |
| 482 |
|
| 483 |
fname := g.getDecoderName(t) |
| 484 |
typ := g.getType(t) |
| 485 |
|
| 486 |
fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") |
| 487 |
fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") |
| 488 |
err := g.genTypeDecoderNoCheck(t, "*out", fieldTags{}, 1) |
| 489 |
if err != nil { |
| 490 |
return err |
| 491 |
} |
| 492 |
fmt.Fprintln(g.out, " if isTopLevel {") |
| 493 |
fmt.Fprintln(g.out, " in.Consumed()") |
| 494 |
fmt.Fprintln(g.out, " }") |
| 495 |
fmt.Fprintln(g.out, "}") |
| 496 |
|
| 497 |
return nil |
| 498 |
} |
| 499 |
|
| 500 |
func (g *Generator) genStructDecoder(t reflect.Type) error { |
| 501 |
if t.Kind() != reflect.Struct { |
| 502 |
return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct type", t) |
| 503 |
} |
| 504 |
|
| 505 |
fname := g.getDecoderName(t) |
| 506 |
typ := g.getType(t) |
| 507 |
|
| 508 |
fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") |
| 509 |
fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") |
| 510 |
fmt.Fprintln(g.out, " if in.IsNull() {") |
| 511 |
fmt.Fprintln(g.out, " if isTopLevel {") |
| 512 |
fmt.Fprintln(g.out, " in.Consumed()") |
| 513 |
fmt.Fprintln(g.out, " }") |
| 514 |
fmt.Fprintln(g.out, " in.Skip()") |
| 515 |
fmt.Fprintln(g.out, " return") |
| 516 |
fmt.Fprintln(g.out, " }") |
| 517 |
|
| 518 |
// Init embedded pointer fields. |
| 519 |
for i := 0; i < t.NumField(); i++ { |
| 520 |
f := t.Field(i) |
| 521 |
if !f.Anonymous || f.Type.Kind() != reflect.Ptr { |
| 522 |
continue |
| 523 |
} |
| 524 |
fmt.Fprintln(g.out, " out."+f.Name+" = new("+g.getType(f.Type.Elem())+")") |
| 525 |
} |
| 526 |
|
| 527 |
fs, err := getStructFields(t) |
| 528 |
if err != nil { |
| 529 |
return fmt.Errorf("cannot generate decoder for %v: %v", t, err) |
| 530 |
} |
| 531 |
|
| 532 |
for _, f := range fs { |
| 533 |
g.genRequiredFieldSet(t, f) |
| 534 |
} |
| 535 |
|
| 536 |
fmt.Fprintln(g.out, " in.Delim('{')") |
| 537 |
fmt.Fprintln(g.out, " for !in.IsDelim('}') {") |
| 538 |
fmt.Fprintf(g.out, " key := in.UnsafeFieldName(%v)\n", g.skipMemberNameUnescaping) |
| 539 |
fmt.Fprintln(g.out, " in.WantColon()") |
| 540 |
|
| 541 |
fmt.Fprintln(g.out, " switch key {") |
| 542 |
for _, f := range fs { |
| 543 |
if err := g.genStructFieldDecoder(t, f); err != nil { |
| 544 |
return err |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
fmt.Fprintln(g.out, " default:") |
| 549 |
if g.disallowUnknownFields { |
| 550 |
fmt.Fprintln(g.out, ` in.AddError(&jlexer.LexerError{ |
| 551 |
Offset: in.GetPos(), |
| 552 |
Reason: "unknown field", |
| 553 |
Data: key, |
| 554 |
})`) |
| 555 |
} else if hasUnknownsUnmarshaler(t) { |
| 556 |
fmt.Fprintln(g.out, " out.UnmarshalUnknown(in, key)") |
| 557 |
} else { |
| 558 |
fmt.Fprintln(g.out, " in.SkipRecursive()") |
| 559 |
} |
| 560 |
fmt.Fprintln(g.out, " }") |
| 561 |
fmt.Fprintln(g.out, " in.WantComma()") |
| 562 |
fmt.Fprintln(g.out, " }") |
| 563 |
fmt.Fprintln(g.out, " in.Delim('}')") |
| 564 |
fmt.Fprintln(g.out, " if isTopLevel {") |
| 565 |
fmt.Fprintln(g.out, " in.Consumed()") |
| 566 |
fmt.Fprintln(g.out, " }") |
| 567 |
|
| 568 |
for _, f := range fs { |
| 569 |
g.genRequiredFieldCheck(t, f) |
| 570 |
} |
| 571 |
|
| 572 |
fmt.Fprintln(g.out, "}") |
| 573 |
|
| 574 |
return nil |
| 575 |
} |
| 576 |
|
| 577 |
func (g *Generator) genStructUnmarshaler(t reflect.Type) error { |
| 578 |
switch t.Kind() { |
| 579 |
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct: |
| 580 |
default: |
| 581 |
return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct/slice/array/map type", t) |
| 582 |
} |
| 583 |
|
| 584 |
fname := g.getDecoderName(t) |
| 585 |
typ := g.getType(t) |
| 586 |
|
| 587 |
if !g.noStdMarshalers { |
| 588 |
fmt.Fprintln(g.out, "// UnmarshalJSON supports json.Unmarshaler interface") |
| 589 |
fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalJSON(data []byte) error {") |
| 590 |
fmt.Fprintln(g.out, " r := jlexer.Lexer{Data: data}") |
| 591 |
fmt.Fprintln(g.out, " "+fname+"(&r, v)") |
| 592 |
fmt.Fprintln(g.out, " return r.Error()") |
| 593 |
fmt.Fprintln(g.out, "}") |
| 594 |
} |
| 595 |
|
| 596 |
fmt.Fprintln(g.out, "// UnmarshalEasyJSON supports easyjson.Unmarshaler interface") |
| 597 |
fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalEasyJSON(l *jlexer.Lexer) {") |
| 598 |
fmt.Fprintln(g.out, " "+fname+"(l, v)") |
| 599 |
fmt.Fprintln(g.out, "}") |
| 600 |
|
| 601 |
return nil |
| 602 |
} |