...

Source file src/github.com/json-iterator/go/reflect_struct_decoder.go

Documentation: github.com/json-iterator/go

     1  package jsoniter
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  	"unsafe"
     8  
     9  	"github.com/modern-go/reflect2"
    10  )
    11  
    12  func decoderOfStruct(ctx *ctx, typ reflect2.Type) ValDecoder {
    13  	bindings := map[string]*Binding{}
    14  	structDescriptor := describeStruct(ctx, typ)
    15  	for _, binding := range structDescriptor.Fields {
    16  		for _, fromName := range binding.FromNames {
    17  			old := bindings[fromName]
    18  			if old == nil {
    19  				bindings[fromName] = binding
    20  				continue
    21  			}
    22  			ignoreOld, ignoreNew := resolveConflictBinding(ctx.frozenConfig, old, binding)
    23  			if ignoreOld {
    24  				delete(bindings, fromName)
    25  			}
    26  			if !ignoreNew {
    27  				bindings[fromName] = binding
    28  			}
    29  		}
    30  	}
    31  	fields := map[string]*structFieldDecoder{}
    32  	for k, binding := range bindings {
    33  		fields[k] = binding.Decoder.(*structFieldDecoder)
    34  	}
    35  
    36  	if !ctx.caseSensitive() {
    37  		for k, binding := range bindings {
    38  			if _, found := fields[strings.ToLower(k)]; !found {
    39  				fields[strings.ToLower(k)] = binding.Decoder.(*structFieldDecoder)
    40  			}
    41  		}
    42  	}
    43  
    44  	return createStructDecoder(ctx, typ, fields)
    45  }
    46  
    47  func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structFieldDecoder) ValDecoder {
    48  	if ctx.disallowUnknownFields {
    49  		return &generalStructDecoder{typ: typ, fields: fields, disallowUnknownFields: true}
    50  	}
    51  	knownHash := map[int64]struct{}{
    52  		0: {},
    53  	}
    54  
    55  	switch len(fields) {
    56  	case 0:
    57  		return &skipObjectDecoder{typ}
    58  	case 1:
    59  		for fieldName, fieldDecoder := range fields {
    60  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
    61  			_, known := knownHash[fieldHash]
    62  			if known {
    63  				return &generalStructDecoder{typ, fields, false}
    64  			}
    65  			knownHash[fieldHash] = struct{}{}
    66  			return &oneFieldStructDecoder{typ, fieldHash, fieldDecoder}
    67  		}
    68  	case 2:
    69  		var fieldHash1 int64
    70  		var fieldHash2 int64
    71  		var fieldDecoder1 *structFieldDecoder
    72  		var fieldDecoder2 *structFieldDecoder
    73  		for fieldName, fieldDecoder := range fields {
    74  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
    75  			_, known := knownHash[fieldHash]
    76  			if known {
    77  				return &generalStructDecoder{typ, fields, false}
    78  			}
    79  			knownHash[fieldHash] = struct{}{}
    80  			if fieldHash1 == 0 {
    81  				fieldHash1 = fieldHash
    82  				fieldDecoder1 = fieldDecoder
    83  			} else {
    84  				fieldHash2 = fieldHash
    85  				fieldDecoder2 = fieldDecoder
    86  			}
    87  		}
    88  		return &twoFieldsStructDecoder{typ, fieldHash1, fieldDecoder1, fieldHash2, fieldDecoder2}
    89  	case 3:
    90  		var fieldName1 int64
    91  		var fieldName2 int64
    92  		var fieldName3 int64
    93  		var fieldDecoder1 *structFieldDecoder
    94  		var fieldDecoder2 *structFieldDecoder
    95  		var fieldDecoder3 *structFieldDecoder
    96  		for fieldName, fieldDecoder := range fields {
    97  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
    98  			_, known := knownHash[fieldHash]
    99  			if known {
   100  				return &generalStructDecoder{typ, fields, false}
   101  			}
   102  			knownHash[fieldHash] = struct{}{}
   103  			if fieldName1 == 0 {
   104  				fieldName1 = fieldHash
   105  				fieldDecoder1 = fieldDecoder
   106  			} else if fieldName2 == 0 {
   107  				fieldName2 = fieldHash
   108  				fieldDecoder2 = fieldDecoder
   109  			} else {
   110  				fieldName3 = fieldHash
   111  				fieldDecoder3 = fieldDecoder
   112  			}
   113  		}
   114  		return &threeFieldsStructDecoder{typ,
   115  			fieldName1, fieldDecoder1,
   116  			fieldName2, fieldDecoder2,
   117  			fieldName3, fieldDecoder3}
   118  	case 4:
   119  		var fieldName1 int64
   120  		var fieldName2 int64
   121  		var fieldName3 int64
   122  		var fieldName4 int64
   123  		var fieldDecoder1 *structFieldDecoder
   124  		var fieldDecoder2 *structFieldDecoder
   125  		var fieldDecoder3 *structFieldDecoder
   126  		var fieldDecoder4 *structFieldDecoder
   127  		for fieldName, fieldDecoder := range fields {
   128  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   129  			_, known := knownHash[fieldHash]
   130  			if known {
   131  				return &generalStructDecoder{typ, fields, false}
   132  			}
   133  			knownHash[fieldHash] = struct{}{}
   134  			if fieldName1 == 0 {
   135  				fieldName1 = fieldHash
   136  				fieldDecoder1 = fieldDecoder
   137  			} else if fieldName2 == 0 {
   138  				fieldName2 = fieldHash
   139  				fieldDecoder2 = fieldDecoder
   140  			} else if fieldName3 == 0 {
   141  				fieldName3 = fieldHash
   142  				fieldDecoder3 = fieldDecoder
   143  			} else {
   144  				fieldName4 = fieldHash
   145  				fieldDecoder4 = fieldDecoder
   146  			}
   147  		}
   148  		return &fourFieldsStructDecoder{typ,
   149  			fieldName1, fieldDecoder1,
   150  			fieldName2, fieldDecoder2,
   151  			fieldName3, fieldDecoder3,
   152  			fieldName4, fieldDecoder4}
   153  	case 5:
   154  		var fieldName1 int64
   155  		var fieldName2 int64
   156  		var fieldName3 int64
   157  		var fieldName4 int64
   158  		var fieldName5 int64
   159  		var fieldDecoder1 *structFieldDecoder
   160  		var fieldDecoder2 *structFieldDecoder
   161  		var fieldDecoder3 *structFieldDecoder
   162  		var fieldDecoder4 *structFieldDecoder
   163  		var fieldDecoder5 *structFieldDecoder
   164  		for fieldName, fieldDecoder := range fields {
   165  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   166  			_, known := knownHash[fieldHash]
   167  			if known {
   168  				return &generalStructDecoder{typ, fields, false}
   169  			}
   170  			knownHash[fieldHash] = struct{}{}
   171  			if fieldName1 == 0 {
   172  				fieldName1 = fieldHash
   173  				fieldDecoder1 = fieldDecoder
   174  			} else if fieldName2 == 0 {
   175  				fieldName2 = fieldHash
   176  				fieldDecoder2 = fieldDecoder
   177  			} else if fieldName3 == 0 {
   178  				fieldName3 = fieldHash
   179  				fieldDecoder3 = fieldDecoder
   180  			} else if fieldName4 == 0 {
   181  				fieldName4 = fieldHash
   182  				fieldDecoder4 = fieldDecoder
   183  			} else {
   184  				fieldName5 = fieldHash
   185  				fieldDecoder5 = fieldDecoder
   186  			}
   187  		}
   188  		return &fiveFieldsStructDecoder{typ,
   189  			fieldName1, fieldDecoder1,
   190  			fieldName2, fieldDecoder2,
   191  			fieldName3, fieldDecoder3,
   192  			fieldName4, fieldDecoder4,
   193  			fieldName5, fieldDecoder5}
   194  	case 6:
   195  		var fieldName1 int64
   196  		var fieldName2 int64
   197  		var fieldName3 int64
   198  		var fieldName4 int64
   199  		var fieldName5 int64
   200  		var fieldName6 int64
   201  		var fieldDecoder1 *structFieldDecoder
   202  		var fieldDecoder2 *structFieldDecoder
   203  		var fieldDecoder3 *structFieldDecoder
   204  		var fieldDecoder4 *structFieldDecoder
   205  		var fieldDecoder5 *structFieldDecoder
   206  		var fieldDecoder6 *structFieldDecoder
   207  		for fieldName, fieldDecoder := range fields {
   208  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   209  			_, known := knownHash[fieldHash]
   210  			if known {
   211  				return &generalStructDecoder{typ, fields, false}
   212  			}
   213  			knownHash[fieldHash] = struct{}{}
   214  			if fieldName1 == 0 {
   215  				fieldName1 = fieldHash
   216  				fieldDecoder1 = fieldDecoder
   217  			} else if fieldName2 == 0 {
   218  				fieldName2 = fieldHash
   219  				fieldDecoder2 = fieldDecoder
   220  			} else if fieldName3 == 0 {
   221  				fieldName3 = fieldHash
   222  				fieldDecoder3 = fieldDecoder
   223  			} else if fieldName4 == 0 {
   224  				fieldName4 = fieldHash
   225  				fieldDecoder4 = fieldDecoder
   226  			} else if fieldName5 == 0 {
   227  				fieldName5 = fieldHash
   228  				fieldDecoder5 = fieldDecoder
   229  			} else {
   230  				fieldName6 = fieldHash
   231  				fieldDecoder6 = fieldDecoder
   232  			}
   233  		}
   234  		return &sixFieldsStructDecoder{typ,
   235  			fieldName1, fieldDecoder1,
   236  			fieldName2, fieldDecoder2,
   237  			fieldName3, fieldDecoder3,
   238  			fieldName4, fieldDecoder4,
   239  			fieldName5, fieldDecoder5,
   240  			fieldName6, fieldDecoder6}
   241  	case 7:
   242  		var fieldName1 int64
   243  		var fieldName2 int64
   244  		var fieldName3 int64
   245  		var fieldName4 int64
   246  		var fieldName5 int64
   247  		var fieldName6 int64
   248  		var fieldName7 int64
   249  		var fieldDecoder1 *structFieldDecoder
   250  		var fieldDecoder2 *structFieldDecoder
   251  		var fieldDecoder3 *structFieldDecoder
   252  		var fieldDecoder4 *structFieldDecoder
   253  		var fieldDecoder5 *structFieldDecoder
   254  		var fieldDecoder6 *structFieldDecoder
   255  		var fieldDecoder7 *structFieldDecoder
   256  		for fieldName, fieldDecoder := range fields {
   257  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   258  			_, known := knownHash[fieldHash]
   259  			if known {
   260  				return &generalStructDecoder{typ, fields, false}
   261  			}
   262  			knownHash[fieldHash] = struct{}{}
   263  			if fieldName1 == 0 {
   264  				fieldName1 = fieldHash
   265  				fieldDecoder1 = fieldDecoder
   266  			} else if fieldName2 == 0 {
   267  				fieldName2 = fieldHash
   268  				fieldDecoder2 = fieldDecoder
   269  			} else if fieldName3 == 0 {
   270  				fieldName3 = fieldHash
   271  				fieldDecoder3 = fieldDecoder
   272  			} else if fieldName4 == 0 {
   273  				fieldName4 = fieldHash
   274  				fieldDecoder4 = fieldDecoder
   275  			} else if fieldName5 == 0 {
   276  				fieldName5 = fieldHash
   277  				fieldDecoder5 = fieldDecoder
   278  			} else if fieldName6 == 0 {
   279  				fieldName6 = fieldHash
   280  				fieldDecoder6 = fieldDecoder
   281  			} else {
   282  				fieldName7 = fieldHash
   283  				fieldDecoder7 = fieldDecoder
   284  			}
   285  		}
   286  		return &sevenFieldsStructDecoder{typ,
   287  			fieldName1, fieldDecoder1,
   288  			fieldName2, fieldDecoder2,
   289  			fieldName3, fieldDecoder3,
   290  			fieldName4, fieldDecoder4,
   291  			fieldName5, fieldDecoder5,
   292  			fieldName6, fieldDecoder6,
   293  			fieldName7, fieldDecoder7}
   294  	case 8:
   295  		var fieldName1 int64
   296  		var fieldName2 int64
   297  		var fieldName3 int64
   298  		var fieldName4 int64
   299  		var fieldName5 int64
   300  		var fieldName6 int64
   301  		var fieldName7 int64
   302  		var fieldName8 int64
   303  		var fieldDecoder1 *structFieldDecoder
   304  		var fieldDecoder2 *structFieldDecoder
   305  		var fieldDecoder3 *structFieldDecoder
   306  		var fieldDecoder4 *structFieldDecoder
   307  		var fieldDecoder5 *structFieldDecoder
   308  		var fieldDecoder6 *structFieldDecoder
   309  		var fieldDecoder7 *structFieldDecoder
   310  		var fieldDecoder8 *structFieldDecoder
   311  		for fieldName, fieldDecoder := range fields {
   312  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   313  			_, known := knownHash[fieldHash]
   314  			if known {
   315  				return &generalStructDecoder{typ, fields, false}
   316  			}
   317  			knownHash[fieldHash] = struct{}{}
   318  			if fieldName1 == 0 {
   319  				fieldName1 = fieldHash
   320  				fieldDecoder1 = fieldDecoder
   321  			} else if fieldName2 == 0 {
   322  				fieldName2 = fieldHash
   323  				fieldDecoder2 = fieldDecoder
   324  			} else if fieldName3 == 0 {
   325  				fieldName3 = fieldHash
   326  				fieldDecoder3 = fieldDecoder
   327  			} else if fieldName4 == 0 {
   328  				fieldName4 = fieldHash
   329  				fieldDecoder4 = fieldDecoder
   330  			} else if fieldName5 == 0 {
   331  				fieldName5 = fieldHash
   332  				fieldDecoder5 = fieldDecoder
   333  			} else if fieldName6 == 0 {
   334  				fieldName6 = fieldHash
   335  				fieldDecoder6 = fieldDecoder
   336  			} else if fieldName7 == 0 {
   337  				fieldName7 = fieldHash
   338  				fieldDecoder7 = fieldDecoder
   339  			} else {
   340  				fieldName8 = fieldHash
   341  				fieldDecoder8 = fieldDecoder
   342  			}
   343  		}
   344  		return &eightFieldsStructDecoder{typ,
   345  			fieldName1, fieldDecoder1,
   346  			fieldName2, fieldDecoder2,
   347  			fieldName3, fieldDecoder3,
   348  			fieldName4, fieldDecoder4,
   349  			fieldName5, fieldDecoder5,
   350  			fieldName6, fieldDecoder6,
   351  			fieldName7, fieldDecoder7,
   352  			fieldName8, fieldDecoder8}
   353  	case 9:
   354  		var fieldName1 int64
   355  		var fieldName2 int64
   356  		var fieldName3 int64
   357  		var fieldName4 int64
   358  		var fieldName5 int64
   359  		var fieldName6 int64
   360  		var fieldName7 int64
   361  		var fieldName8 int64
   362  		var fieldName9 int64
   363  		var fieldDecoder1 *structFieldDecoder
   364  		var fieldDecoder2 *structFieldDecoder
   365  		var fieldDecoder3 *structFieldDecoder
   366  		var fieldDecoder4 *structFieldDecoder
   367  		var fieldDecoder5 *structFieldDecoder
   368  		var fieldDecoder6 *structFieldDecoder
   369  		var fieldDecoder7 *structFieldDecoder
   370  		var fieldDecoder8 *structFieldDecoder
   371  		var fieldDecoder9 *structFieldDecoder
   372  		for fieldName, fieldDecoder := range fields {
   373  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   374  			_, known := knownHash[fieldHash]
   375  			if known {
   376  				return &generalStructDecoder{typ, fields, false}
   377  			}
   378  			knownHash[fieldHash] = struct{}{}
   379  			if fieldName1 == 0 {
   380  				fieldName1 = fieldHash
   381  				fieldDecoder1 = fieldDecoder
   382  			} else if fieldName2 == 0 {
   383  				fieldName2 = fieldHash
   384  				fieldDecoder2 = fieldDecoder
   385  			} else if fieldName3 == 0 {
   386  				fieldName3 = fieldHash
   387  				fieldDecoder3 = fieldDecoder
   388  			} else if fieldName4 == 0 {
   389  				fieldName4 = fieldHash
   390  				fieldDecoder4 = fieldDecoder
   391  			} else if fieldName5 == 0 {
   392  				fieldName5 = fieldHash
   393  				fieldDecoder5 = fieldDecoder
   394  			} else if fieldName6 == 0 {
   395  				fieldName6 = fieldHash
   396  				fieldDecoder6 = fieldDecoder
   397  			} else if fieldName7 == 0 {
   398  				fieldName7 = fieldHash
   399  				fieldDecoder7 = fieldDecoder
   400  			} else if fieldName8 == 0 {
   401  				fieldName8 = fieldHash
   402  				fieldDecoder8 = fieldDecoder
   403  			} else {
   404  				fieldName9 = fieldHash
   405  				fieldDecoder9 = fieldDecoder
   406  			}
   407  		}
   408  		return &nineFieldsStructDecoder{typ,
   409  			fieldName1, fieldDecoder1,
   410  			fieldName2, fieldDecoder2,
   411  			fieldName3, fieldDecoder3,
   412  			fieldName4, fieldDecoder4,
   413  			fieldName5, fieldDecoder5,
   414  			fieldName6, fieldDecoder6,
   415  			fieldName7, fieldDecoder7,
   416  			fieldName8, fieldDecoder8,
   417  			fieldName9, fieldDecoder9}
   418  	case 10:
   419  		var fieldName1 int64
   420  		var fieldName2 int64
   421  		var fieldName3 int64
   422  		var fieldName4 int64
   423  		var fieldName5 int64
   424  		var fieldName6 int64
   425  		var fieldName7 int64
   426  		var fieldName8 int64
   427  		var fieldName9 int64
   428  		var fieldName10 int64
   429  		var fieldDecoder1 *structFieldDecoder
   430  		var fieldDecoder2 *structFieldDecoder
   431  		var fieldDecoder3 *structFieldDecoder
   432  		var fieldDecoder4 *structFieldDecoder
   433  		var fieldDecoder5 *structFieldDecoder
   434  		var fieldDecoder6 *structFieldDecoder
   435  		var fieldDecoder7 *structFieldDecoder
   436  		var fieldDecoder8 *structFieldDecoder
   437  		var fieldDecoder9 *structFieldDecoder
   438  		var fieldDecoder10 *structFieldDecoder
   439  		for fieldName, fieldDecoder := range fields {
   440  			fieldHash := calcHash(fieldName, ctx.caseSensitive())
   441  			_, known := knownHash[fieldHash]
   442  			if known {
   443  				return &generalStructDecoder{typ, fields, false}
   444  			}
   445  			knownHash[fieldHash] = struct{}{}
   446  			if fieldName1 == 0 {
   447  				fieldName1 = fieldHash
   448  				fieldDecoder1 = fieldDecoder
   449  			} else if fieldName2 == 0 {
   450  				fieldName2 = fieldHash
   451  				fieldDecoder2 = fieldDecoder
   452  			} else if fieldName3 == 0 {
   453  				fieldName3 = fieldHash
   454  				fieldDecoder3 = fieldDecoder
   455  			} else if fieldName4 == 0 {
   456  				fieldName4 = fieldHash
   457  				fieldDecoder4 = fieldDecoder
   458  			} else if fieldName5 == 0 {
   459  				fieldName5 = fieldHash
   460  				fieldDecoder5 = fieldDecoder
   461  			} else if fieldName6 == 0 {
   462  				fieldName6 = fieldHash
   463  				fieldDecoder6 = fieldDecoder
   464  			} else if fieldName7 == 0 {
   465  				fieldName7 = fieldHash
   466  				fieldDecoder7 = fieldDecoder
   467  			} else if fieldName8 == 0 {
   468  				fieldName8 = fieldHash
   469  				fieldDecoder8 = fieldDecoder
   470  			} else if fieldName9 == 0 {
   471  				fieldName9 = fieldHash
   472  				fieldDecoder9 = fieldDecoder
   473  			} else {
   474  				fieldName10 = fieldHash
   475  				fieldDecoder10 = fieldDecoder
   476  			}
   477  		}
   478  		return &tenFieldsStructDecoder{typ,
   479  			fieldName1, fieldDecoder1,
   480  			fieldName2, fieldDecoder2,
   481  			fieldName3, fieldDecoder3,
   482  			fieldName4, fieldDecoder4,
   483  			fieldName5, fieldDecoder5,
   484  			fieldName6, fieldDecoder6,
   485  			fieldName7, fieldDecoder7,
   486  			fieldName8, fieldDecoder8,
   487  			fieldName9, fieldDecoder9,
   488  			fieldName10, fieldDecoder10}
   489  	}
   490  	return &generalStructDecoder{typ, fields, false}
   491  }
   492  
   493  type generalStructDecoder struct {
   494  	typ                   reflect2.Type
   495  	fields                map[string]*structFieldDecoder
   496  	disallowUnknownFields bool
   497  }
   498  
   499  func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   500  	if !iter.readObjectStart() {
   501  		return
   502  	}
   503  	if !iter.incrementDepth() {
   504  		return
   505  	}
   506  	var c byte
   507  	for c = ','; c == ','; c = iter.nextToken() {
   508  		decoder.decodeOneField(ptr, iter)
   509  	}
   510  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   511  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   512  	}
   513  	if c != '}' {
   514  		iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c}))
   515  	}
   516  	iter.decrementDepth()
   517  }
   518  
   519  func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
   520  	var field string
   521  	var fieldDecoder *structFieldDecoder
   522  	if iter.cfg.objectFieldMustBeSimpleString {
   523  		fieldBytes := iter.ReadStringAsSlice()
   524  		field = *(*string)(unsafe.Pointer(&fieldBytes))
   525  		fieldDecoder = decoder.fields[field]
   526  		if fieldDecoder == nil && !iter.cfg.caseSensitive {
   527  			fieldDecoder = decoder.fields[strings.ToLower(field)]
   528  		}
   529  	} else {
   530  		field = iter.ReadString()
   531  		fieldDecoder = decoder.fields[field]
   532  		if fieldDecoder == nil && !iter.cfg.caseSensitive {
   533  			fieldDecoder = decoder.fields[strings.ToLower(field)]
   534  		}
   535  	}
   536  	if fieldDecoder == nil {
   537  		if decoder.disallowUnknownFields {
   538  			msg := "found unknown field: " + field
   539  			iter.ReportError("ReadObject", msg)
   540  		}
   541  		c := iter.nextToken()
   542  		if c != ':' {
   543  			iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
   544  		}
   545  		iter.Skip()
   546  		return
   547  	}
   548  	c := iter.nextToken()
   549  	if c != ':' {
   550  		iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
   551  	}
   552  	fieldDecoder.Decode(ptr, iter)
   553  }
   554  
   555  type skipObjectDecoder struct {
   556  	typ reflect2.Type
   557  }
   558  
   559  func (decoder *skipObjectDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   560  	valueType := iter.WhatIsNext()
   561  	if valueType != ObjectValue && valueType != NilValue {
   562  		iter.ReportError("skipObjectDecoder", "expect object or null")
   563  		return
   564  	}
   565  	iter.Skip()
   566  }
   567  
   568  type oneFieldStructDecoder struct {
   569  	typ          reflect2.Type
   570  	fieldHash    int64
   571  	fieldDecoder *structFieldDecoder
   572  }
   573  
   574  func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   575  	if !iter.readObjectStart() {
   576  		return
   577  	}
   578  	if !iter.incrementDepth() {
   579  		return
   580  	}
   581  	for {
   582  		if iter.readFieldHash() == decoder.fieldHash {
   583  			decoder.fieldDecoder.Decode(ptr, iter)
   584  		} else {
   585  			iter.Skip()
   586  		}
   587  		if iter.isObjectEnd() {
   588  			break
   589  		}
   590  	}
   591  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   592  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   593  	}
   594  	iter.decrementDepth()
   595  }
   596  
   597  type twoFieldsStructDecoder struct {
   598  	typ           reflect2.Type
   599  	fieldHash1    int64
   600  	fieldDecoder1 *structFieldDecoder
   601  	fieldHash2    int64
   602  	fieldDecoder2 *structFieldDecoder
   603  }
   604  
   605  func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   606  	if !iter.readObjectStart() {
   607  		return
   608  	}
   609  	if !iter.incrementDepth() {
   610  		return
   611  	}
   612  	for {
   613  		switch iter.readFieldHash() {
   614  		case decoder.fieldHash1:
   615  			decoder.fieldDecoder1.Decode(ptr, iter)
   616  		case decoder.fieldHash2:
   617  			decoder.fieldDecoder2.Decode(ptr, iter)
   618  		default:
   619  			iter.Skip()
   620  		}
   621  		if iter.isObjectEnd() {
   622  			break
   623  		}
   624  	}
   625  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   626  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   627  	}
   628  	iter.decrementDepth()
   629  }
   630  
   631  type threeFieldsStructDecoder struct {
   632  	typ           reflect2.Type
   633  	fieldHash1    int64
   634  	fieldDecoder1 *structFieldDecoder
   635  	fieldHash2    int64
   636  	fieldDecoder2 *structFieldDecoder
   637  	fieldHash3    int64
   638  	fieldDecoder3 *structFieldDecoder
   639  }
   640  
   641  func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   642  	if !iter.readObjectStart() {
   643  		return
   644  	}
   645  	if !iter.incrementDepth() {
   646  		return
   647  	}
   648  	for {
   649  		switch iter.readFieldHash() {
   650  		case decoder.fieldHash1:
   651  			decoder.fieldDecoder1.Decode(ptr, iter)
   652  		case decoder.fieldHash2:
   653  			decoder.fieldDecoder2.Decode(ptr, iter)
   654  		case decoder.fieldHash3:
   655  			decoder.fieldDecoder3.Decode(ptr, iter)
   656  		default:
   657  			iter.Skip()
   658  		}
   659  		if iter.isObjectEnd() {
   660  			break
   661  		}
   662  	}
   663  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   664  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   665  	}
   666  	iter.decrementDepth()
   667  }
   668  
   669  type fourFieldsStructDecoder struct {
   670  	typ           reflect2.Type
   671  	fieldHash1    int64
   672  	fieldDecoder1 *structFieldDecoder
   673  	fieldHash2    int64
   674  	fieldDecoder2 *structFieldDecoder
   675  	fieldHash3    int64
   676  	fieldDecoder3 *structFieldDecoder
   677  	fieldHash4    int64
   678  	fieldDecoder4 *structFieldDecoder
   679  }
   680  
   681  func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   682  	if !iter.readObjectStart() {
   683  		return
   684  	}
   685  	if !iter.incrementDepth() {
   686  		return
   687  	}
   688  	for {
   689  		switch iter.readFieldHash() {
   690  		case decoder.fieldHash1:
   691  			decoder.fieldDecoder1.Decode(ptr, iter)
   692  		case decoder.fieldHash2:
   693  			decoder.fieldDecoder2.Decode(ptr, iter)
   694  		case decoder.fieldHash3:
   695  			decoder.fieldDecoder3.Decode(ptr, iter)
   696  		case decoder.fieldHash4:
   697  			decoder.fieldDecoder4.Decode(ptr, iter)
   698  		default:
   699  			iter.Skip()
   700  		}
   701  		if iter.isObjectEnd() {
   702  			break
   703  		}
   704  	}
   705  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   706  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   707  	}
   708  	iter.decrementDepth()
   709  }
   710  
   711  type fiveFieldsStructDecoder struct {
   712  	typ           reflect2.Type
   713  	fieldHash1    int64
   714  	fieldDecoder1 *structFieldDecoder
   715  	fieldHash2    int64
   716  	fieldDecoder2 *structFieldDecoder
   717  	fieldHash3    int64
   718  	fieldDecoder3 *structFieldDecoder
   719  	fieldHash4    int64
   720  	fieldDecoder4 *structFieldDecoder
   721  	fieldHash5    int64
   722  	fieldDecoder5 *structFieldDecoder
   723  }
   724  
   725  func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   726  	if !iter.readObjectStart() {
   727  		return
   728  	}
   729  	if !iter.incrementDepth() {
   730  		return
   731  	}
   732  	for {
   733  		switch iter.readFieldHash() {
   734  		case decoder.fieldHash1:
   735  			decoder.fieldDecoder1.Decode(ptr, iter)
   736  		case decoder.fieldHash2:
   737  			decoder.fieldDecoder2.Decode(ptr, iter)
   738  		case decoder.fieldHash3:
   739  			decoder.fieldDecoder3.Decode(ptr, iter)
   740  		case decoder.fieldHash4:
   741  			decoder.fieldDecoder4.Decode(ptr, iter)
   742  		case decoder.fieldHash5:
   743  			decoder.fieldDecoder5.Decode(ptr, iter)
   744  		default:
   745  			iter.Skip()
   746  		}
   747  		if iter.isObjectEnd() {
   748  			break
   749  		}
   750  	}
   751  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   752  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   753  	}
   754  	iter.decrementDepth()
   755  }
   756  
   757  type sixFieldsStructDecoder struct {
   758  	typ           reflect2.Type
   759  	fieldHash1    int64
   760  	fieldDecoder1 *structFieldDecoder
   761  	fieldHash2    int64
   762  	fieldDecoder2 *structFieldDecoder
   763  	fieldHash3    int64
   764  	fieldDecoder3 *structFieldDecoder
   765  	fieldHash4    int64
   766  	fieldDecoder4 *structFieldDecoder
   767  	fieldHash5    int64
   768  	fieldDecoder5 *structFieldDecoder
   769  	fieldHash6    int64
   770  	fieldDecoder6 *structFieldDecoder
   771  }
   772  
   773  func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   774  	if !iter.readObjectStart() {
   775  		return
   776  	}
   777  	if !iter.incrementDepth() {
   778  		return
   779  	}
   780  	for {
   781  		switch iter.readFieldHash() {
   782  		case decoder.fieldHash1:
   783  			decoder.fieldDecoder1.Decode(ptr, iter)
   784  		case decoder.fieldHash2:
   785  			decoder.fieldDecoder2.Decode(ptr, iter)
   786  		case decoder.fieldHash3:
   787  			decoder.fieldDecoder3.Decode(ptr, iter)
   788  		case decoder.fieldHash4:
   789  			decoder.fieldDecoder4.Decode(ptr, iter)
   790  		case decoder.fieldHash5:
   791  			decoder.fieldDecoder5.Decode(ptr, iter)
   792  		case decoder.fieldHash6:
   793  			decoder.fieldDecoder6.Decode(ptr, iter)
   794  		default:
   795  			iter.Skip()
   796  		}
   797  		if iter.isObjectEnd() {
   798  			break
   799  		}
   800  	}
   801  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   802  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   803  	}
   804  	iter.decrementDepth()
   805  }
   806  
   807  type sevenFieldsStructDecoder struct {
   808  	typ           reflect2.Type
   809  	fieldHash1    int64
   810  	fieldDecoder1 *structFieldDecoder
   811  	fieldHash2    int64
   812  	fieldDecoder2 *structFieldDecoder
   813  	fieldHash3    int64
   814  	fieldDecoder3 *structFieldDecoder
   815  	fieldHash4    int64
   816  	fieldDecoder4 *structFieldDecoder
   817  	fieldHash5    int64
   818  	fieldDecoder5 *structFieldDecoder
   819  	fieldHash6    int64
   820  	fieldDecoder6 *structFieldDecoder
   821  	fieldHash7    int64
   822  	fieldDecoder7 *structFieldDecoder
   823  }
   824  
   825  func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   826  	if !iter.readObjectStart() {
   827  		return
   828  	}
   829  	if !iter.incrementDepth() {
   830  		return
   831  	}
   832  	for {
   833  		switch iter.readFieldHash() {
   834  		case decoder.fieldHash1:
   835  			decoder.fieldDecoder1.Decode(ptr, iter)
   836  		case decoder.fieldHash2:
   837  			decoder.fieldDecoder2.Decode(ptr, iter)
   838  		case decoder.fieldHash3:
   839  			decoder.fieldDecoder3.Decode(ptr, iter)
   840  		case decoder.fieldHash4:
   841  			decoder.fieldDecoder4.Decode(ptr, iter)
   842  		case decoder.fieldHash5:
   843  			decoder.fieldDecoder5.Decode(ptr, iter)
   844  		case decoder.fieldHash6:
   845  			decoder.fieldDecoder6.Decode(ptr, iter)
   846  		case decoder.fieldHash7:
   847  			decoder.fieldDecoder7.Decode(ptr, iter)
   848  		default:
   849  			iter.Skip()
   850  		}
   851  		if iter.isObjectEnd() {
   852  			break
   853  		}
   854  	}
   855  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   856  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   857  	}
   858  	iter.decrementDepth()
   859  }
   860  
   861  type eightFieldsStructDecoder struct {
   862  	typ           reflect2.Type
   863  	fieldHash1    int64
   864  	fieldDecoder1 *structFieldDecoder
   865  	fieldHash2    int64
   866  	fieldDecoder2 *structFieldDecoder
   867  	fieldHash3    int64
   868  	fieldDecoder3 *structFieldDecoder
   869  	fieldHash4    int64
   870  	fieldDecoder4 *structFieldDecoder
   871  	fieldHash5    int64
   872  	fieldDecoder5 *structFieldDecoder
   873  	fieldHash6    int64
   874  	fieldDecoder6 *structFieldDecoder
   875  	fieldHash7    int64
   876  	fieldDecoder7 *structFieldDecoder
   877  	fieldHash8    int64
   878  	fieldDecoder8 *structFieldDecoder
   879  }
   880  
   881  func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   882  	if !iter.readObjectStart() {
   883  		return
   884  	}
   885  	if !iter.incrementDepth() {
   886  		return
   887  	}
   888  	for {
   889  		switch iter.readFieldHash() {
   890  		case decoder.fieldHash1:
   891  			decoder.fieldDecoder1.Decode(ptr, iter)
   892  		case decoder.fieldHash2:
   893  			decoder.fieldDecoder2.Decode(ptr, iter)
   894  		case decoder.fieldHash3:
   895  			decoder.fieldDecoder3.Decode(ptr, iter)
   896  		case decoder.fieldHash4:
   897  			decoder.fieldDecoder4.Decode(ptr, iter)
   898  		case decoder.fieldHash5:
   899  			decoder.fieldDecoder5.Decode(ptr, iter)
   900  		case decoder.fieldHash6:
   901  			decoder.fieldDecoder6.Decode(ptr, iter)
   902  		case decoder.fieldHash7:
   903  			decoder.fieldDecoder7.Decode(ptr, iter)
   904  		case decoder.fieldHash8:
   905  			decoder.fieldDecoder8.Decode(ptr, iter)
   906  		default:
   907  			iter.Skip()
   908  		}
   909  		if iter.isObjectEnd() {
   910  			break
   911  		}
   912  	}
   913  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   914  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   915  	}
   916  	iter.decrementDepth()
   917  }
   918  
   919  type nineFieldsStructDecoder struct {
   920  	typ           reflect2.Type
   921  	fieldHash1    int64
   922  	fieldDecoder1 *structFieldDecoder
   923  	fieldHash2    int64
   924  	fieldDecoder2 *structFieldDecoder
   925  	fieldHash3    int64
   926  	fieldDecoder3 *structFieldDecoder
   927  	fieldHash4    int64
   928  	fieldDecoder4 *structFieldDecoder
   929  	fieldHash5    int64
   930  	fieldDecoder5 *structFieldDecoder
   931  	fieldHash6    int64
   932  	fieldDecoder6 *structFieldDecoder
   933  	fieldHash7    int64
   934  	fieldDecoder7 *structFieldDecoder
   935  	fieldHash8    int64
   936  	fieldDecoder8 *structFieldDecoder
   937  	fieldHash9    int64
   938  	fieldDecoder9 *structFieldDecoder
   939  }
   940  
   941  func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
   942  	if !iter.readObjectStart() {
   943  		return
   944  	}
   945  	if !iter.incrementDepth() {
   946  		return
   947  	}
   948  	for {
   949  		switch iter.readFieldHash() {
   950  		case decoder.fieldHash1:
   951  			decoder.fieldDecoder1.Decode(ptr, iter)
   952  		case decoder.fieldHash2:
   953  			decoder.fieldDecoder2.Decode(ptr, iter)
   954  		case decoder.fieldHash3:
   955  			decoder.fieldDecoder3.Decode(ptr, iter)
   956  		case decoder.fieldHash4:
   957  			decoder.fieldDecoder4.Decode(ptr, iter)
   958  		case decoder.fieldHash5:
   959  			decoder.fieldDecoder5.Decode(ptr, iter)
   960  		case decoder.fieldHash6:
   961  			decoder.fieldDecoder6.Decode(ptr, iter)
   962  		case decoder.fieldHash7:
   963  			decoder.fieldDecoder7.Decode(ptr, iter)
   964  		case decoder.fieldHash8:
   965  			decoder.fieldDecoder8.Decode(ptr, iter)
   966  		case decoder.fieldHash9:
   967  			decoder.fieldDecoder9.Decode(ptr, iter)
   968  		default:
   969  			iter.Skip()
   970  		}
   971  		if iter.isObjectEnd() {
   972  			break
   973  		}
   974  	}
   975  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
   976  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
   977  	}
   978  	iter.decrementDepth()
   979  }
   980  
   981  type tenFieldsStructDecoder struct {
   982  	typ            reflect2.Type
   983  	fieldHash1     int64
   984  	fieldDecoder1  *structFieldDecoder
   985  	fieldHash2     int64
   986  	fieldDecoder2  *structFieldDecoder
   987  	fieldHash3     int64
   988  	fieldDecoder3  *structFieldDecoder
   989  	fieldHash4     int64
   990  	fieldDecoder4  *structFieldDecoder
   991  	fieldHash5     int64
   992  	fieldDecoder5  *structFieldDecoder
   993  	fieldHash6     int64
   994  	fieldDecoder6  *structFieldDecoder
   995  	fieldHash7     int64
   996  	fieldDecoder7  *structFieldDecoder
   997  	fieldHash8     int64
   998  	fieldDecoder8  *structFieldDecoder
   999  	fieldHash9     int64
  1000  	fieldDecoder9  *structFieldDecoder
  1001  	fieldHash10    int64
  1002  	fieldDecoder10 *structFieldDecoder
  1003  }
  1004  
  1005  func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1006  	if !iter.readObjectStart() {
  1007  		return
  1008  	}
  1009  	if !iter.incrementDepth() {
  1010  		return
  1011  	}
  1012  	for {
  1013  		switch iter.readFieldHash() {
  1014  		case decoder.fieldHash1:
  1015  			decoder.fieldDecoder1.Decode(ptr, iter)
  1016  		case decoder.fieldHash2:
  1017  			decoder.fieldDecoder2.Decode(ptr, iter)
  1018  		case decoder.fieldHash3:
  1019  			decoder.fieldDecoder3.Decode(ptr, iter)
  1020  		case decoder.fieldHash4:
  1021  			decoder.fieldDecoder4.Decode(ptr, iter)
  1022  		case decoder.fieldHash5:
  1023  			decoder.fieldDecoder5.Decode(ptr, iter)
  1024  		case decoder.fieldHash6:
  1025  			decoder.fieldDecoder6.Decode(ptr, iter)
  1026  		case decoder.fieldHash7:
  1027  			decoder.fieldDecoder7.Decode(ptr, iter)
  1028  		case decoder.fieldHash8:
  1029  			decoder.fieldDecoder8.Decode(ptr, iter)
  1030  		case decoder.fieldHash9:
  1031  			decoder.fieldDecoder9.Decode(ptr, iter)
  1032  		case decoder.fieldHash10:
  1033  			decoder.fieldDecoder10.Decode(ptr, iter)
  1034  		default:
  1035  			iter.Skip()
  1036  		}
  1037  		if iter.isObjectEnd() {
  1038  			break
  1039  		}
  1040  	}
  1041  	if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  1042  		iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  1043  	}
  1044  	iter.decrementDepth()
  1045  }
  1046  
  1047  type structFieldDecoder struct {
  1048  	field        reflect2.StructField
  1049  	fieldDecoder ValDecoder
  1050  }
  1051  
  1052  func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1053  	fieldPtr := decoder.field.UnsafeGet(ptr)
  1054  	decoder.fieldDecoder.Decode(fieldPtr, iter)
  1055  	if iter.Error != nil && iter.Error != io.EOF {
  1056  		iter.Error = fmt.Errorf("%s: %s", decoder.field.Name(), iter.Error.Error())
  1057  	}
  1058  }
  1059  
  1060  type stringModeStringDecoder struct {
  1061  	elemDecoder ValDecoder
  1062  	cfg         *frozenConfig
  1063  }
  1064  
  1065  func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1066  	decoder.elemDecoder.Decode(ptr, iter)
  1067  	str := *((*string)(ptr))
  1068  	tempIter := decoder.cfg.BorrowIterator([]byte(str))
  1069  	defer decoder.cfg.ReturnIterator(tempIter)
  1070  	*((*string)(ptr)) = tempIter.ReadString()
  1071  }
  1072  
  1073  type stringModeNumberDecoder struct {
  1074  	elemDecoder ValDecoder
  1075  }
  1076  
  1077  func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1078  	if iter.WhatIsNext() == NilValue {
  1079  		decoder.elemDecoder.Decode(ptr, iter)
  1080  		return
  1081  	}
  1082  
  1083  	c := iter.nextToken()
  1084  	if c != '"' {
  1085  		iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  1086  		return
  1087  	}
  1088  	decoder.elemDecoder.Decode(ptr, iter)
  1089  	if iter.Error != nil {
  1090  		return
  1091  	}
  1092  	c = iter.readByte()
  1093  	if c != '"' {
  1094  		iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  1095  		return
  1096  	}
  1097  }
  1098  

View as plain text