github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/encode.go (about) 1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved. 2 // Use of this source code is governed by a MIT license found in the LICENSE file. 3 4 package codec 5 6 import ( 7 "encoding" 8 "errors" 9 "io" 10 "reflect" 11 "sort" 12 "strconv" 13 "time" 14 ) 15 16 // defEncByteBufSize is the default size of []byte used 17 // for bufio buffer or []byte (when nil passed) 18 const defEncByteBufSize = 1 << 10 // 4:16, 6:64, 8:256, 10:1024 19 20 var errEncoderNotInitialized = errors.New("Encoder not initialized") 21 22 // encDriver abstracts the actual codec (binc vs msgpack, etc) 23 type encDriver interface { 24 EncodeNil() 25 EncodeInt(i int64) 26 EncodeUint(i uint64) 27 EncodeBool(b bool) 28 EncodeFloat32(f float32) 29 EncodeFloat64(f float64) 30 EncodeRawExt(re *RawExt) 31 EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext) 32 // EncodeString using cUTF8, honor'ing StringToRaw flag 33 EncodeString(v string) 34 EncodeStringBytesRaw(v []byte) 35 EncodeTime(time.Time) 36 WriteArrayStart(length int) 37 WriteArrayEnd() 38 WriteMapStart(length int) 39 WriteMapEnd() 40 41 // reset will reset current encoding runtime state, and cached information from the handle 42 reset() 43 44 encoder() *Encoder 45 46 driverStateManager 47 } 48 49 type encDriverContainerTracker interface { 50 WriteArrayElem() 51 WriteMapElemKey() 52 WriteMapElemValue() 53 } 54 55 type encDriverNoState struct{} 56 57 func (encDriverNoState) captureState() interface{} { return nil } 58 func (encDriverNoState) reset() {} 59 func (encDriverNoState) resetState() {} 60 func (encDriverNoState) restoreState(v interface{}) {} 61 62 type encDriverNoopContainerWriter struct{} 63 64 func (encDriverNoopContainerWriter) WriteArrayStart(length int) {} 65 func (encDriverNoopContainerWriter) WriteArrayEnd() {} 66 func (encDriverNoopContainerWriter) WriteMapStart(length int) {} 67 func (encDriverNoopContainerWriter) WriteMapEnd() {} 68 69 // encStructFieldObj[Slice] is used for sorting when there are missing fields and canonical flag is set 70 type encStructFieldObj struct { 71 key string 72 rv reflect.Value 73 intf interface{} 74 ascii bool 75 isRv bool 76 } 77 78 type encStructFieldObjSlice []encStructFieldObj 79 80 func (p encStructFieldObjSlice) Len() int { return len(p) } 81 func (p encStructFieldObjSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 82 func (p encStructFieldObjSlice) Less(i, j int) bool { 83 return p[uint(i)].key < p[uint(j)].key 84 } 85 86 // EncodeOptions captures configuration options during encode. 87 type EncodeOptions struct { 88 // WriterBufferSize is the size of the buffer used when writing. 89 // 90 // if > 0, we use a smart buffer internally for performance purposes. 91 WriterBufferSize int 92 93 // ChanRecvTimeout is the timeout used when selecting from a chan. 94 // 95 // Configuring this controls how we receive from a chan during the encoding process. 96 // - If ==0, we only consume the elements currently available in the chan. 97 // - if <0, we consume until the chan is closed. 98 // - If >0, we consume until this timeout. 99 ChanRecvTimeout time.Duration 100 101 // StructToArray specifies to encode a struct as an array, and not as a map 102 StructToArray bool 103 104 // Canonical representation means that encoding a value will always result in the same 105 // sequence of bytes. 106 // 107 // This only affects maps, as the iteration order for maps is random. 108 // 109 // The implementation MAY use the natural sort order for the map keys if possible: 110 // 111 // - If there is a natural sort order (ie for number, bool, string or []byte keys), 112 // then the map keys are first sorted in natural order and then written 113 // with corresponding map values to the strema. 114 // - If there is no natural sort order, then the map keys will first be 115 // encoded into []byte, and then sorted, 116 // before writing the sorted keys and the corresponding map values to the stream. 117 // 118 Canonical bool 119 120 // CheckCircularRef controls whether we check for circular references 121 // and error fast during an encode. 122 // 123 // If enabled, an error is received if a pointer to a struct 124 // references itself either directly or through one of its fields (iteratively). 125 // 126 // This is opt-in, as there may be a performance hit to checking circular references. 127 CheckCircularRef bool 128 129 // RecursiveEmptyCheck controls how we determine whether a value is empty. 130 // 131 // If true, we descend into interfaces and pointers to reursively check if value is empty. 132 // 133 // We *might* check struct fields one by one to see if empty 134 // (if we cannot directly check if a struct value is equal to its zero value). 135 // If so, we honor IsZero, Comparable, IsCodecEmpty(), etc. 136 // Note: This *may* make OmitEmpty more expensive due to the large number of reflect calls. 137 // 138 // If false, we check if the value is equal to its zero value (newly allocated state). 139 RecursiveEmptyCheck bool 140 141 // Raw controls whether we encode Raw values. 142 // This is a "dangerous" option and must be explicitly set. 143 // If set, we blindly encode Raw values as-is, without checking 144 // if they are a correct representation of a value in that format. 145 // If unset, we error out. 146 Raw bool 147 148 // StringToRaw controls how strings are encoded. 149 // 150 // As a go string is just an (immutable) sequence of bytes, 151 // it can be encoded either as raw bytes or as a UTF string. 152 // 153 // By default, strings are encoded as UTF-8. 154 // but can be treated as []byte during an encode. 155 // 156 // Note that things which we know (by definition) to be UTF-8 157 // are ALWAYS encoded as UTF-8 strings. 158 // These include encoding.TextMarshaler, time.Format calls, struct field names, etc. 159 StringToRaw bool 160 161 // OptimumSize controls whether we optimize for the smallest size. 162 // 163 // Some formats will use this flag to determine whether to encode 164 // in the smallest size possible, even if it takes slightly longer. 165 // 166 // For example, some formats that support half-floats might check if it is possible 167 // to store a float64 as a half float. Doing this check has a small performance cost, 168 // but the benefit is that the encoded message will be smaller. 169 OptimumSize bool 170 171 // NoAddressableReadonly controls whether we try to force a non-addressable value 172 // to be addressable so we can call a pointer method on it e.g. for types 173 // that support Selfer, json.Marshaler, etc. 174 // 175 // Use it in the very rare occurrence that your types modify a pointer value when calling 176 // an encode callback function e.g. JsonMarshal, TextMarshal, BinaryMarshal or CodecEncodeSelf. 177 NoAddressableReadonly bool 178 } 179 180 // --------------------------------------------- 181 182 func (e *Encoder) rawExt(f *codecFnInfo, rv reflect.Value) { 183 e.e.EncodeRawExt(rv2i(rv).(*RawExt)) 184 } 185 186 func (e *Encoder) ext(f *codecFnInfo, rv reflect.Value) { 187 e.e.EncodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn) 188 } 189 190 func (e *Encoder) selferMarshal(f *codecFnInfo, rv reflect.Value) { 191 rv2i(rv).(Selfer).CodecEncodeSelf(e) 192 } 193 194 func (e *Encoder) binaryMarshal(f *codecFnInfo, rv reflect.Value) { 195 bs, fnerr := rv2i(rv).(encoding.BinaryMarshaler).MarshalBinary() 196 e.marshalRaw(bs, fnerr) 197 } 198 199 func (e *Encoder) textMarshal(f *codecFnInfo, rv reflect.Value) { 200 bs, fnerr := rv2i(rv).(encoding.TextMarshaler).MarshalText() 201 e.marshalUtf8(bs, fnerr) 202 } 203 204 func (e *Encoder) jsonMarshal(f *codecFnInfo, rv reflect.Value) { 205 bs, fnerr := rv2i(rv).(jsonMarshaler).MarshalJSON() 206 e.marshalAsis(bs, fnerr) 207 } 208 209 func (e *Encoder) raw(f *codecFnInfo, rv reflect.Value) { 210 e.rawBytes(rv2i(rv).(Raw)) 211 } 212 213 func (e *Encoder) encodeComplex64(v complex64) { 214 if imag(v) != 0 { 215 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v)) 216 } 217 e.e.EncodeFloat32(real(v)) 218 } 219 220 func (e *Encoder) encodeComplex128(v complex128) { 221 if imag(v) != 0 { 222 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v)) 223 } 224 e.e.EncodeFloat64(real(v)) 225 } 226 227 func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) { 228 e.e.EncodeBool(rvGetBool(rv)) 229 } 230 231 func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) { 232 e.e.EncodeTime(rvGetTime(rv)) 233 } 234 235 func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) { 236 e.e.EncodeString(rvGetString(rv)) 237 } 238 239 func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) { 240 e.e.EncodeFloat32(rvGetFloat32(rv)) 241 } 242 243 func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) { 244 e.e.EncodeFloat64(rvGetFloat64(rv)) 245 } 246 247 func (e *Encoder) kComplex64(f *codecFnInfo, rv reflect.Value) { 248 e.encodeComplex64(rvGetComplex64(rv)) 249 } 250 251 func (e *Encoder) kComplex128(f *codecFnInfo, rv reflect.Value) { 252 e.encodeComplex128(rvGetComplex128(rv)) 253 } 254 255 func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) { 256 e.e.EncodeInt(int64(rvGetInt(rv))) 257 } 258 259 func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) { 260 e.e.EncodeInt(int64(rvGetInt8(rv))) 261 } 262 263 func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) { 264 e.e.EncodeInt(int64(rvGetInt16(rv))) 265 } 266 267 func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) { 268 e.e.EncodeInt(int64(rvGetInt32(rv))) 269 } 270 271 func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) { 272 e.e.EncodeInt(int64(rvGetInt64(rv))) 273 } 274 275 func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) { 276 e.e.EncodeUint(uint64(rvGetUint(rv))) 277 } 278 279 func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) { 280 e.e.EncodeUint(uint64(rvGetUint8(rv))) 281 } 282 283 func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) { 284 e.e.EncodeUint(uint64(rvGetUint16(rv))) 285 } 286 287 func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) { 288 e.e.EncodeUint(uint64(rvGetUint32(rv))) 289 } 290 291 func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) { 292 e.e.EncodeUint(uint64(rvGetUint64(rv))) 293 } 294 295 func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) { 296 e.e.EncodeUint(uint64(rvGetUintptr(rv))) 297 } 298 299 func (e *Encoder) kErr(f *codecFnInfo, rv reflect.Value) { 300 e.errorf("unsupported kind %s, for %#v", rv.Kind(), rv) 301 } 302 303 func chanToSlice(rv reflect.Value, rtslice reflect.Type, timeout time.Duration) (rvcs reflect.Value) { 304 rvcs = rvZeroK(rtslice, reflect.Slice) 305 if timeout < 0 { // consume until close 306 for { 307 recv, recvOk := rv.Recv() 308 if !recvOk { 309 break 310 } 311 rvcs = reflect.Append(rvcs, recv) 312 } 313 } else { 314 cases := make([]reflect.SelectCase, 2) 315 cases[0] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: rv} 316 if timeout == 0 { 317 cases[1] = reflect.SelectCase{Dir: reflect.SelectDefault} 318 } else { 319 tt := time.NewTimer(timeout) 320 cases[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(tt.C)} 321 } 322 for { 323 chosen, recv, recvOk := reflect.Select(cases) 324 if chosen == 1 || !recvOk { 325 break 326 } 327 rvcs = reflect.Append(rvcs, recv) 328 } 329 } 330 return 331 } 332 333 func (e *Encoder) kSeqFn(rtelem reflect.Type) (fn *codecFn) { 334 for rtelem.Kind() == reflect.Ptr { 335 rtelem = rtelem.Elem() 336 } 337 // if kind is reflect.Interface, do not pre-determine the encoding type, 338 // because preEncodeValue may break it down to a concrete type and kInterface will bomb. 339 if rtelem.Kind() != reflect.Interface { 340 fn = e.h.fn(rtelem) 341 } 342 return 343 } 344 345 func (e *Encoder) kSliceWMbs(rv reflect.Value, ti *typeInfo) { 346 var l = rvLenSlice(rv) 347 if l == 0 { 348 e.mapStart(0) 349 } else { 350 e.haltOnMbsOddLen(l) 351 e.mapStart(l >> 1) // e.mapStart(l / 2) 352 fn := e.kSeqFn(ti.elem) 353 for j := 0; j < l; j++ { 354 if j&1 == 0 { // j%2 == 0 { 355 e.mapElemKey() 356 } else { 357 e.mapElemValue() 358 } 359 e.encodeValue(rvSliceIndex(rv, j, ti), fn) 360 } 361 } 362 e.mapEnd() 363 } 364 365 func (e *Encoder) kSliceW(rv reflect.Value, ti *typeInfo) { 366 var l = rvLenSlice(rv) 367 e.arrayStart(l) 368 if l > 0 { 369 fn := e.kSeqFn(ti.elem) 370 for j := 0; j < l; j++ { 371 e.arrayElem() 372 e.encodeValue(rvSliceIndex(rv, j, ti), fn) 373 } 374 } 375 e.arrayEnd() 376 } 377 378 func (e *Encoder) kArrayWMbs(rv reflect.Value, ti *typeInfo) { 379 var l = rv.Len() 380 if l == 0 { 381 e.mapStart(0) 382 } else { 383 e.haltOnMbsOddLen(l) 384 e.mapStart(l >> 1) // e.mapStart(l / 2) 385 fn := e.kSeqFn(ti.elem) 386 for j := 0; j < l; j++ { 387 if j&1 == 0 { // j%2 == 0 { 388 e.mapElemKey() 389 } else { 390 e.mapElemValue() 391 } 392 e.encodeValue(rv.Index(j), fn) 393 } 394 } 395 e.mapEnd() 396 } 397 398 func (e *Encoder) kArrayW(rv reflect.Value, ti *typeInfo) { 399 var l = rv.Len() 400 e.arrayStart(l) 401 if l > 0 { 402 fn := e.kSeqFn(ti.elem) 403 for j := 0; j < l; j++ { 404 e.arrayElem() 405 e.encodeValue(rv.Index(j), fn) 406 } 407 } 408 e.arrayEnd() 409 } 410 411 func (e *Encoder) kChan(f *codecFnInfo, rv reflect.Value) { 412 if f.ti.chandir&uint8(reflect.RecvDir) == 0 { 413 e.errorf("send-only channel cannot be encoded") 414 } 415 if !f.ti.mbs && uint8TypId == rt2id(f.ti.elem) { 416 e.kSliceBytesChan(rv) 417 return 418 } 419 rtslice := reflect.SliceOf(f.ti.elem) 420 rv = chanToSlice(rv, rtslice, e.h.ChanRecvTimeout) 421 ti := e.h.getTypeInfo(rt2id(rtslice), rtslice) 422 if f.ti.mbs { 423 e.kSliceWMbs(rv, ti) 424 } else { 425 e.kSliceW(rv, ti) 426 } 427 } 428 429 func (e *Encoder) kSlice(f *codecFnInfo, rv reflect.Value) { 430 if f.ti.mbs { 431 e.kSliceWMbs(rv, f.ti) 432 } else if f.ti.rtid == uint8SliceTypId || uint8TypId == rt2id(f.ti.elem) { 433 e.e.EncodeStringBytesRaw(rvGetBytes(rv)) 434 } else { 435 e.kSliceW(rv, f.ti) 436 } 437 } 438 439 func (e *Encoder) kArray(f *codecFnInfo, rv reflect.Value) { 440 if f.ti.mbs { 441 e.kArrayWMbs(rv, f.ti) 442 } else if handleBytesWithinKArray && uint8TypId == rt2id(f.ti.elem) { 443 e.e.EncodeStringBytesRaw(rvGetArrayBytes(rv, []byte{})) 444 } else { 445 e.kArrayW(rv, f.ti) 446 } 447 } 448 449 func (e *Encoder) kSliceBytesChan(rv reflect.Value) { 450 // do not use range, so that the number of elements encoded 451 // does not change, and encoding does not hang waiting on someone to close chan. 452 453 bs0 := e.blist.peek(32, true) 454 bs := bs0 455 456 irv := rv2i(rv) 457 ch, ok := irv.(<-chan byte) 458 if !ok { 459 ch = irv.(chan byte) 460 } 461 462 L1: 463 switch timeout := e.h.ChanRecvTimeout; { 464 case timeout == 0: // only consume available 465 for { 466 select { 467 case b := <-ch: 468 bs = append(bs, b) 469 default: 470 break L1 471 } 472 } 473 case timeout > 0: // consume until timeout 474 tt := time.NewTimer(timeout) 475 for { 476 select { 477 case b := <-ch: 478 bs = append(bs, b) 479 case <-tt.C: 480 // close(tt.C) 481 break L1 482 } 483 } 484 default: // consume until close 485 for b := range ch { 486 bs = append(bs, b) 487 } 488 } 489 490 e.e.EncodeStringBytesRaw(bs) 491 e.blist.put(bs) 492 if !byteSliceSameData(bs0, bs) { 493 e.blist.put(bs0) 494 } 495 } 496 497 func (e *Encoder) kStructSfi(f *codecFnInfo) []*structFieldInfo { 498 if e.h.Canonical { 499 return f.ti.sfi.sorted() 500 } 501 return f.ti.sfi.source() 502 } 503 504 func (e *Encoder) kStructNoOmitempty(f *codecFnInfo, rv reflect.Value) { 505 var tisfi []*structFieldInfo 506 if f.ti.toArray || e.h.StructToArray { // toArray 507 tisfi = f.ti.sfi.source() 508 e.arrayStart(len(tisfi)) 509 for _, si := range tisfi { 510 e.arrayElem() 511 e.encodeValue(si.path.field(rv), nil) 512 } 513 e.arrayEnd() 514 } else { 515 tisfi = e.kStructSfi(f) 516 e.mapStart(len(tisfi)) 517 keytyp := f.ti.keyType 518 for _, si := range tisfi { 519 e.mapElemKey() 520 e.kStructFieldKey(keytyp, si.path.encNameAsciiAlphaNum, si.encName) 521 e.mapElemValue() 522 e.encodeValue(si.path.field(rv), nil) 523 } 524 e.mapEnd() 525 } 526 } 527 528 func (e *Encoder) kStructFieldKey(keyType valueType, encNameAsciiAlphaNum bool, encName string) { 529 encStructFieldKey(encName, e.e, e.w(), keyType, encNameAsciiAlphaNum, e.js) 530 } 531 532 func (e *Encoder) kStruct(f *codecFnInfo, rv reflect.Value) { 533 var newlen int 534 ti := f.ti 535 toMap := !(ti.toArray || e.h.StructToArray) 536 var mf map[string]interface{} 537 if ti.flagMissingFielder { 538 mf = rv2i(rv).(MissingFielder).CodecMissingFields() 539 toMap = true 540 newlen += len(mf) 541 } else if ti.flagMissingFielderPtr { 542 rv2 := e.addrRV(rv, ti.rt, ti.ptr) 543 mf = rv2i(rv2).(MissingFielder).CodecMissingFields() 544 toMap = true 545 newlen += len(mf) 546 } 547 tisfi := ti.sfi.source() 548 newlen += len(tisfi) 549 550 var fkvs = e.slist.get(newlen)[:newlen] 551 552 recur := e.h.RecursiveEmptyCheck 553 554 var kv sfiRv 555 var j int 556 if toMap { 557 newlen = 0 558 for _, si := range e.kStructSfi(f) { 559 kv.r = si.path.field(rv) 560 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) { 561 continue 562 } 563 kv.v = si 564 fkvs[newlen] = kv 565 newlen++ 566 } 567 568 var mf2s []stringIntf 569 if len(mf) > 0 { 570 mf2s = make([]stringIntf, 0, len(mf)) 571 for k, v := range mf { 572 if k == "" { 573 continue 574 } 575 if ti.infoFieldOmitempty && isEmptyValue(reflect.ValueOf(v), e.h.TypeInfos, recur) { 576 continue 577 } 578 mf2s = append(mf2s, stringIntf{k, v}) 579 } 580 } 581 582 e.mapStart(newlen + len(mf2s)) 583 584 // When there are missing fields, and Canonical flag is set, 585 // we cannot have the missing fields and struct fields sorted independently. 586 // We have to capture them together and sort as a unit. 587 588 if len(mf2s) > 0 && e.h.Canonical { 589 mf2w := make([]encStructFieldObj, newlen+len(mf2s)) 590 for j = 0; j < newlen; j++ { 591 kv = fkvs[j] 592 mf2w[j] = encStructFieldObj{kv.v.encName, kv.r, nil, kv.v.path.encNameAsciiAlphaNum, true} 593 } 594 for _, v := range mf2s { 595 mf2w[j] = encStructFieldObj{v.v, reflect.Value{}, v.i, false, false} 596 j++ 597 } 598 sort.Sort((encStructFieldObjSlice)(mf2w)) 599 for _, v := range mf2w { 600 e.mapElemKey() 601 e.kStructFieldKey(ti.keyType, v.ascii, v.key) 602 e.mapElemValue() 603 if v.isRv { 604 e.encodeValue(v.rv, nil) 605 } else { 606 e.encode(v.intf) 607 } 608 } 609 } else { 610 keytyp := ti.keyType 611 for j = 0; j < newlen; j++ { 612 kv = fkvs[j] 613 e.mapElemKey() 614 e.kStructFieldKey(keytyp, kv.v.path.encNameAsciiAlphaNum, kv.v.encName) 615 e.mapElemValue() 616 e.encodeValue(kv.r, nil) 617 } 618 for _, v := range mf2s { 619 e.mapElemKey() 620 e.kStructFieldKey(keytyp, false, v.v) 621 e.mapElemValue() 622 e.encode(v.i) 623 } 624 } 625 626 e.mapEnd() 627 } else { 628 newlen = len(tisfi) 629 for i, si := range tisfi { // use unsorted array (to match sequence in struct) 630 kv.r = si.path.field(rv) 631 // use the zero value. 632 // if a reference or struct, set to nil (so you do not output too much) 633 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) { 634 switch kv.r.Kind() { 635 case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice: 636 kv.r = reflect.Value{} //encode as nil 637 } 638 } 639 fkvs[i] = kv 640 } 641 // encode it all 642 e.arrayStart(newlen) 643 for j = 0; j < newlen; j++ { 644 e.arrayElem() 645 e.encodeValue(fkvs[j].r, nil) 646 } 647 e.arrayEnd() 648 } 649 650 // do not use defer. Instead, use explicit pool return at end of function. 651 // defer has a cost we are trying to avoid. 652 // If there is a panic and these slices are not returned, it is ok. 653 e.slist.put(fkvs) 654 } 655 656 func (e *Encoder) kMap(f *codecFnInfo, rv reflect.Value) { 657 l := rvLenMap(rv) 658 e.mapStart(l) 659 if l == 0 { 660 e.mapEnd() 661 return 662 } 663 664 // determine the underlying key and val encFn's for the map. 665 // This eliminates some work which is done for each loop iteration i.e. 666 // rv.Type(), ref.ValueOf(rt).Pointer(), then check map/list for fn. 667 // 668 // However, if kind is reflect.Interface, do not pre-determine the 669 // encoding type, because preEncodeValue may break it down to 670 // a concrete type and kInterface will bomb. 671 672 var keyFn, valFn *codecFn 673 674 ktypeKind := reflect.Kind(f.ti.keykind) 675 vtypeKind := reflect.Kind(f.ti.elemkind) 676 677 rtval := f.ti.elem 678 rtvalkind := vtypeKind 679 for rtvalkind == reflect.Ptr { 680 rtval = rtval.Elem() 681 rtvalkind = rtval.Kind() 682 } 683 if rtvalkind != reflect.Interface { 684 valFn = e.h.fn(rtval) 685 } 686 687 var rvv = mapAddrLoopvarRV(f.ti.elem, vtypeKind) 688 689 rtkey := f.ti.key 690 var keyTypeIsString = stringTypId == rt2id(rtkey) // rtkeyid 691 if keyTypeIsString { 692 keyFn = e.h.fn(rtkey) 693 } else { 694 for rtkey.Kind() == reflect.Ptr { 695 rtkey = rtkey.Elem() 696 } 697 if rtkey.Kind() != reflect.Interface { 698 keyFn = e.h.fn(rtkey) 699 } 700 } 701 702 if e.h.Canonical { 703 e.kMapCanonical(f.ti, rv, rvv, keyFn, valFn) 704 e.mapEnd() 705 return 706 } 707 708 var rvk = mapAddrLoopvarRV(f.ti.key, ktypeKind) 709 710 var it mapIter 711 mapRange(&it, rv, rvk, rvv, true) 712 713 for it.Next() { 714 e.mapElemKey() 715 if keyTypeIsString { 716 e.e.EncodeString(it.Key().String()) 717 } else { 718 e.encodeValue(it.Key(), keyFn) 719 } 720 e.mapElemValue() 721 e.encodeValue(it.Value(), valFn) 722 } 723 it.Done() 724 725 e.mapEnd() 726 } 727 728 func (e *Encoder) kMapCanonical(ti *typeInfo, rv, rvv reflect.Value, keyFn, valFn *codecFn) { 729 // The base kind of the type of the map key is sufficient for ordering. 730 // We only do out of band if that kind is not ordered (number or string), bool or time.Time. 731 // If the key is a predeclared type, directly call methods on encDriver e.g. EncodeString 732 // but if not, call encodeValue, in case it has an extension registered or otherwise. 733 rtkey := ti.key 734 rtkeydecl := rtkey.PkgPath() == "" && rtkey.Name() != "" // key type is predeclared 735 736 mks := rv.MapKeys() 737 rtkeyKind := rtkey.Kind() 738 kfast := mapKeyFastKindFor(rtkeyKind) 739 visindirect := mapStoresElemIndirect(uintptr(ti.elemsize)) 740 visref := refBitset.isset(ti.elemkind) 741 742 switch rtkeyKind { 743 case reflect.Bool: 744 // though bool keys make no sense in a map, it *could* happen. 745 // in that case, we MUST support it in reflection mode, 746 // as that is the fallback for even codecgen and others. 747 748 // sort the keys so that false comes before true 749 // ie if 2 keys in order (true, false), then swap them 750 if len(mks) == 2 && mks[0].Bool() { 751 mks[0], mks[1] = mks[1], mks[0] 752 } 753 for i := range mks { 754 e.mapElemKey() 755 if rtkeydecl { 756 e.e.EncodeBool(mks[i].Bool()) 757 } else { 758 e.encodeValueNonNil(mks[i], keyFn) 759 } 760 e.mapElemValue() 761 e.encodeValue(mapGet(rv, mks[i], rvv, kfast, visindirect, visref), valFn) 762 } 763 case reflect.String: 764 mksv := make([]stringRv, len(mks)) 765 for i, k := range mks { 766 v := &mksv[i] 767 v.r = k 768 v.v = k.String() 769 } 770 sort.Sort(stringRvSlice(mksv)) 771 for i := range mksv { 772 e.mapElemKey() 773 if rtkeydecl { 774 e.e.EncodeString(mksv[i].v) 775 } else { 776 e.encodeValueNonNil(mksv[i].r, keyFn) 777 } 778 e.mapElemValue() 779 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 780 } 781 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr: 782 mksv := make([]uint64Rv, len(mks)) 783 for i, k := range mks { 784 v := &mksv[i] 785 v.r = k 786 v.v = k.Uint() 787 } 788 sort.Sort(uint64RvSlice(mksv)) 789 for i := range mksv { 790 e.mapElemKey() 791 if rtkeydecl { 792 e.e.EncodeUint(mksv[i].v) 793 } else { 794 e.encodeValueNonNil(mksv[i].r, keyFn) 795 } 796 e.mapElemValue() 797 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 798 } 799 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 800 mksv := make([]int64Rv, len(mks)) 801 for i, k := range mks { 802 v := &mksv[i] 803 v.r = k 804 v.v = k.Int() 805 } 806 sort.Sort(int64RvSlice(mksv)) 807 for i := range mksv { 808 e.mapElemKey() 809 if rtkeydecl { 810 e.e.EncodeInt(mksv[i].v) 811 } else { 812 e.encodeValueNonNil(mksv[i].r, keyFn) 813 } 814 e.mapElemValue() 815 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 816 } 817 case reflect.Float32: 818 mksv := make([]float64Rv, len(mks)) 819 for i, k := range mks { 820 v := &mksv[i] 821 v.r = k 822 v.v = k.Float() 823 } 824 sort.Sort(float64RvSlice(mksv)) 825 for i := range mksv { 826 e.mapElemKey() 827 if rtkeydecl { 828 e.e.EncodeFloat32(float32(mksv[i].v)) 829 } else { 830 e.encodeValueNonNil(mksv[i].r, keyFn) 831 } 832 e.mapElemValue() 833 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 834 } 835 case reflect.Float64: 836 mksv := make([]float64Rv, len(mks)) 837 for i, k := range mks { 838 v := &mksv[i] 839 v.r = k 840 v.v = k.Float() 841 } 842 sort.Sort(float64RvSlice(mksv)) 843 for i := range mksv { 844 e.mapElemKey() 845 if rtkeydecl { 846 e.e.EncodeFloat64(mksv[i].v) 847 } else { 848 e.encodeValueNonNil(mksv[i].r, keyFn) 849 } 850 e.mapElemValue() 851 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 852 } 853 default: 854 if rtkey == timeTyp { 855 mksv := make([]timeRv, len(mks)) 856 for i, k := range mks { 857 v := &mksv[i] 858 v.r = k 859 v.v = rv2i(k).(time.Time) 860 } 861 sort.Sort(timeRvSlice(mksv)) 862 for i := range mksv { 863 e.mapElemKey() 864 e.e.EncodeTime(mksv[i].v) 865 e.mapElemValue() 866 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn) 867 } 868 break 869 } 870 871 // out-of-band 872 // first encode each key to a []byte first, then sort them, then record 873 bs0 := e.blist.get(len(mks) * 16) 874 mksv := bs0 875 mksbv := make([]bytesRv, len(mks)) 876 877 func() { 878 // replicate sideEncode logic 879 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) { 880 e.wb = wb 881 e.bytes = bytes 882 e.c = c 883 e.e.restoreState(state) 884 }(e.wb, e.bytes, e.c, e.e.captureState()) 885 886 // e2 := NewEncoderBytes(&mksv, e.hh) 887 e.wb = bytesEncAppender{mksv[:0], &mksv} 888 e.bytes = true 889 e.c = 0 890 e.e.resetState() 891 892 for i, k := range mks { 893 v := &mksbv[i] 894 l := len(mksv) 895 896 e.c = containerMapKey 897 e.encodeValue(k, nil) 898 e.atEndOfEncode() 899 e.w().end() 900 901 v.r = k 902 v.v = mksv[l:] 903 } 904 }() 905 906 sort.Sort(bytesRvSlice(mksbv)) 907 for j := range mksbv { 908 e.mapElemKey() 909 e.encWr.writeb(mksbv[j].v) 910 e.mapElemValue() 911 e.encodeValue(mapGet(rv, mksbv[j].r, rvv, kfast, visindirect, visref), valFn) 912 } 913 e.blist.put(mksv) 914 if !byteSliceSameData(bs0, mksv) { 915 e.blist.put(bs0) 916 } 917 } 918 } 919 920 // Encoder writes an object to an output stream in a supported format. 921 // 922 // Encoder is NOT safe for concurrent use i.e. a Encoder cannot be used 923 // concurrently in multiple goroutines. 924 // 925 // However, as Encoder could be allocation heavy to initialize, a Reset method is provided 926 // so its state can be reused to decode new input streams repeatedly. 927 // This is the idiomatic way to use. 928 type Encoder struct { 929 panicHdl 930 931 e encDriver 932 933 h *BasicHandle 934 935 // hopefully, reduce derefencing cost by laying the encWriter inside the Encoder 936 encWr 937 938 // ---- cpu cache line boundary 939 hh Handle 940 941 blist bytesFreelist 942 err error 943 944 // ---- cpu cache line boundary 945 946 // ---- writable fields during execution --- *try* to keep in sep cache line 947 948 // ci holds interfaces during an encoding (if CheckCircularRef=true) 949 // 950 // We considered using a []uintptr (slice of pointer addresses) retrievable via rv.UnsafeAddr. 951 // However, it is possible for the same pointer to point to 2 different types e.g. 952 // type T struct { tHelper } 953 // Here, for var v T; &v and &v.tHelper are the same pointer. 954 // Consequently, we need a tuple of type and pointer, which interface{} natively provides. 955 ci []interface{} // []uintptr 956 957 perType encPerType 958 959 slist sfiRvFreelist 960 } 961 962 // NewEncoder returns an Encoder for encoding into an io.Writer. 963 // 964 // For efficiency, Users are encouraged to configure WriterBufferSize on the handle 965 // OR pass in a memory buffered writer (eg bufio.Writer, bytes.Buffer). 966 func NewEncoder(w io.Writer, h Handle) *Encoder { 967 e := h.newEncDriver().encoder() 968 if w != nil { 969 e.Reset(w) 970 } 971 return e 972 } 973 974 // NewEncoderBytes returns an encoder for encoding directly and efficiently 975 // into a byte slice, using zero-copying to temporary slices. 976 // 977 // It will potentially replace the output byte slice pointed to. 978 // After encoding, the out parameter contains the encoded contents. 979 func NewEncoderBytes(out *[]byte, h Handle) *Encoder { 980 e := h.newEncDriver().encoder() 981 if out != nil { 982 e.ResetBytes(out) 983 } 984 return e 985 } 986 987 func (e *Encoder) HandleName() string { 988 return e.hh.Name() 989 } 990 991 func (e *Encoder) init(h Handle) { 992 initHandle(h) 993 e.err = errEncoderNotInitialized 994 e.bytes = true 995 e.hh = h 996 e.h = h.getBasicHandle() 997 e.be = e.hh.isBinary() 998 } 999 1000 func (e *Encoder) w() *encWr { 1001 return &e.encWr 1002 } 1003 1004 func (e *Encoder) resetCommon() { 1005 e.e.reset() 1006 if e.ci != nil { 1007 e.ci = e.ci[:0] 1008 } 1009 e.c = 0 1010 e.calls = 0 1011 e.seq = 0 1012 e.err = nil 1013 } 1014 1015 // Reset resets the Encoder with a new output stream. 1016 // 1017 // This accommodates using the state of the Encoder, 1018 // where it has "cached" information about sub-engines. 1019 func (e *Encoder) Reset(w io.Writer) { 1020 e.bytes = false 1021 if e.wf == nil { 1022 e.wf = new(bufioEncWriter) 1023 } 1024 e.wf.reset(w, e.h.WriterBufferSize, &e.blist) 1025 e.resetCommon() 1026 } 1027 1028 // ResetBytes resets the Encoder with a new destination output []byte. 1029 func (e *Encoder) ResetBytes(out *[]byte) { 1030 e.bytes = true 1031 e.wb.reset(encInBytes(out), out) 1032 e.resetCommon() 1033 } 1034 1035 // Encode writes an object into a stream. 1036 // 1037 // Encoding can be configured via the struct tag for the fields. 1038 // The key (in the struct tags) that we look at is configurable. 1039 // 1040 // By default, we look up the "codec" key in the struct field's tags, 1041 // and fall bak to the "json" key if "codec" is absent. 1042 // That key in struct field's tag value is the key name, 1043 // followed by an optional comma and options. 1044 // 1045 // To set an option on all fields (e.g. omitempty on all fields), you 1046 // can create a field called _struct, and set flags on it. The options 1047 // which can be set on _struct are: 1048 // - omitempty: so all fields are omitted if empty 1049 // - toarray: so struct is encoded as an array 1050 // - int: so struct key names are encoded as signed integers (instead of strings) 1051 // - uint: so struct key names are encoded as unsigned integers (instead of strings) 1052 // - float: so struct key names are encoded as floats (instead of strings) 1053 // 1054 // More details on these below. 1055 // 1056 // Struct values "usually" encode as maps. Each exported struct field is encoded unless: 1057 // - the field's tag is "-", OR 1058 // - the field is empty (empty or the zero value) and its tag specifies the "omitempty" option. 1059 // 1060 // When encoding as a map, the first string in the tag (before the comma) 1061 // is the map key string to use when encoding. 1062 // ... 1063 // This key is typically encoded as a string. 1064 // However, there are instances where the encoded stream has mapping keys encoded as numbers. 1065 // For example, some cbor streams have keys as integer codes in the stream, but they should map 1066 // to fields in a structured object. Consequently, a struct is the natural representation in code. 1067 // For these, configure the struct to encode/decode the keys as numbers (instead of string). 1068 // This is done with the int,uint or float option on the _struct field (see above). 1069 // 1070 // However, struct values may encode as arrays. This happens when: 1071 // - StructToArray Encode option is set, OR 1072 // - the tag on the _struct field sets the "toarray" option 1073 // 1074 // Note that omitempty is ignored when encoding struct values as arrays, 1075 // as an entry must be encoded for each field, to maintain its position. 1076 // 1077 // Values with types that implement MapBySlice are encoded as stream maps. 1078 // 1079 // The empty values (for omitempty option) are false, 0, any nil pointer 1080 // or interface value, and any array, slice, map, or string of length zero. 1081 // 1082 // Anonymous fields are encoded inline except: 1083 // - the struct tag specifies a replacement name (first value) 1084 // - the field is of an interface type 1085 // 1086 // Examples: 1087 // 1088 // // NOTE: 'json:' can be used as struct tag key, in place 'codec:' below. 1089 // type MyStruct struct { 1090 // _struct bool `codec:",omitempty"` //set omitempty for every field 1091 // Field1 string `codec:"-"` //skip this field 1092 // Field2 int `codec:"myName"` //Use key "myName" in encode stream 1093 // Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty. 1094 // Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty. 1095 // io.Reader //use key "Reader". 1096 // MyStruct `codec:"my1" //use key "my1". 1097 // MyStruct //inline it 1098 // ... 1099 // } 1100 // 1101 // type MyStruct struct { 1102 // _struct bool `codec:",toarray"` //encode struct as an array 1103 // } 1104 // 1105 // type MyStruct struct { 1106 // _struct bool `codec:",uint"` //encode struct with "unsigned integer" keys 1107 // Field1 string `codec:"1"` //encode Field1 key using: EncodeInt(1) 1108 // Field2 string `codec:"2"` //encode Field2 key using: EncodeInt(2) 1109 // } 1110 // 1111 // The mode of encoding is based on the type of the value. When a value is seen: 1112 // - If a Selfer, call its CodecEncodeSelf method 1113 // - If an extension is registered for it, call that extension function 1114 // - If implements encoding.(Binary|Text|JSON)Marshaler, call Marshal(Binary|Text|JSON) method 1115 // - Else encode it based on its reflect.Kind 1116 // 1117 // Note that struct field names and keys in map[string]XXX will be treated as symbols. 1118 // Some formats support symbols (e.g. binc) and will properly encode the string 1119 // only once in the stream, and use a tag to refer to it thereafter. 1120 func (e *Encoder) Encode(v interface{}) (err error) { 1121 // tried to use closure, as runtime optimizes defer with no params. 1122 // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc). 1123 // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139 1124 if !debugging { 1125 defer func() { 1126 // if error occurred during encoding, return that error; 1127 // else if error occurred on end'ing (i.e. during flush), return that error. 1128 if x := recover(); x != nil { 1129 panicValToErr(e, x, &e.err) 1130 err = e.err 1131 } 1132 }() 1133 } 1134 1135 e.MustEncode(v) 1136 return 1137 } 1138 1139 // MustEncode is like Encode, but panics if unable to Encode. 1140 // 1141 // Note: This provides insight to the code location that triggered the error. 1142 func (e *Encoder) MustEncode(v interface{}) { 1143 halt.onerror(e.err) 1144 if e.hh == nil { 1145 halt.onerror(errNoFormatHandle) 1146 } 1147 1148 e.calls++ 1149 e.encode(v) 1150 e.calls-- 1151 if e.calls == 0 { 1152 e.atEndOfEncode() 1153 e.w().end() 1154 } 1155 } 1156 1157 // Release is a no-op. 1158 // 1159 // Deprecated: Pooled resources are not used with an Encoder. 1160 // This method is kept for compatibility reasons only. 1161 func (e *Encoder) Release() { 1162 } 1163 1164 func (e *Encoder) encode(iv interface{}) { 1165 // MARKER: a switch with only concrete types can be optimized. 1166 // consequently, we deal with nil and interfaces outside the switch. 1167 1168 if iv == nil { 1169 e.e.EncodeNil() 1170 return 1171 } 1172 1173 rv, ok := isNil(iv) 1174 if ok { 1175 e.e.EncodeNil() 1176 return 1177 } 1178 1179 switch v := iv.(type) { 1180 // case nil: 1181 // case Selfer: 1182 case Raw: 1183 e.rawBytes(v) 1184 case reflect.Value: 1185 e.encodeValue(v, nil) 1186 1187 case string: 1188 e.e.EncodeString(v) 1189 case bool: 1190 e.e.EncodeBool(v) 1191 case int: 1192 e.e.EncodeInt(int64(v)) 1193 case int8: 1194 e.e.EncodeInt(int64(v)) 1195 case int16: 1196 e.e.EncodeInt(int64(v)) 1197 case int32: 1198 e.e.EncodeInt(int64(v)) 1199 case int64: 1200 e.e.EncodeInt(v) 1201 case uint: 1202 e.e.EncodeUint(uint64(v)) 1203 case uint8: 1204 e.e.EncodeUint(uint64(v)) 1205 case uint16: 1206 e.e.EncodeUint(uint64(v)) 1207 case uint32: 1208 e.e.EncodeUint(uint64(v)) 1209 case uint64: 1210 e.e.EncodeUint(v) 1211 case uintptr: 1212 e.e.EncodeUint(uint64(v)) 1213 case float32: 1214 e.e.EncodeFloat32(v) 1215 case float64: 1216 e.e.EncodeFloat64(v) 1217 case complex64: 1218 e.encodeComplex64(v) 1219 case complex128: 1220 e.encodeComplex128(v) 1221 case time.Time: 1222 e.e.EncodeTime(v) 1223 case []byte: 1224 e.e.EncodeStringBytesRaw(v) 1225 case *Raw: 1226 e.rawBytes(*v) 1227 case *string: 1228 e.e.EncodeString(*v) 1229 case *bool: 1230 e.e.EncodeBool(*v) 1231 case *int: 1232 e.e.EncodeInt(int64(*v)) 1233 case *int8: 1234 e.e.EncodeInt(int64(*v)) 1235 case *int16: 1236 e.e.EncodeInt(int64(*v)) 1237 case *int32: 1238 e.e.EncodeInt(int64(*v)) 1239 case *int64: 1240 e.e.EncodeInt(*v) 1241 case *uint: 1242 e.e.EncodeUint(uint64(*v)) 1243 case *uint8: 1244 e.e.EncodeUint(uint64(*v)) 1245 case *uint16: 1246 e.e.EncodeUint(uint64(*v)) 1247 case *uint32: 1248 e.e.EncodeUint(uint64(*v)) 1249 case *uint64: 1250 e.e.EncodeUint(*v) 1251 case *uintptr: 1252 e.e.EncodeUint(uint64(*v)) 1253 case *float32: 1254 e.e.EncodeFloat32(*v) 1255 case *float64: 1256 e.e.EncodeFloat64(*v) 1257 case *complex64: 1258 e.encodeComplex64(*v) 1259 case *complex128: 1260 e.encodeComplex128(*v) 1261 case *time.Time: 1262 e.e.EncodeTime(*v) 1263 case *[]byte: 1264 if *v == nil { 1265 e.e.EncodeNil() 1266 } else { 1267 e.e.EncodeStringBytesRaw(*v) 1268 } 1269 default: 1270 // we can't check non-predefined types, as they might be a Selfer or extension. 1271 if skipFastpathTypeSwitchInDirectCall || !fastpathEncodeTypeSwitch(iv, e) { 1272 e.encodeValue(rv, nil) 1273 } 1274 } 1275 } 1276 1277 // encodeValue will encode a value. 1278 // 1279 // Note that encodeValue will handle nil in the stream early, so that the 1280 // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves. 1281 func (e *Encoder) encodeValue(rv reflect.Value, fn *codecFn) { 1282 // if a valid fn is passed, it MUST BE for the dereferenced type of rv 1283 1284 // MARKER: We check if value is nil here, so that the kXXX method do not have to. 1285 1286 var sptr interface{} 1287 var rvp reflect.Value 1288 var rvpValid bool 1289 TOP: 1290 switch rv.Kind() { 1291 case reflect.Ptr: 1292 if rvIsNil(rv) { 1293 e.e.EncodeNil() 1294 return 1295 } 1296 rvpValid = true 1297 rvp = rv 1298 rv = rv.Elem() 1299 goto TOP 1300 case reflect.Interface: 1301 if rvIsNil(rv) { 1302 e.e.EncodeNil() 1303 return 1304 } 1305 rvpValid = false 1306 rvp = reflect.Value{} 1307 rv = rv.Elem() 1308 goto TOP 1309 case reflect.Struct: 1310 if rvpValid && e.h.CheckCircularRef { 1311 sptr = rv2i(rvp) 1312 for _, vv := range e.ci { 1313 if eq4i(sptr, vv) { // error if sptr already seen 1314 e.errorf("circular reference found: %p, %T", sptr, sptr) 1315 } 1316 } 1317 e.ci = append(e.ci, sptr) 1318 } 1319 case reflect.Slice, reflect.Map, reflect.Chan: 1320 if rvIsNil(rv) { 1321 e.e.EncodeNil() 1322 return 1323 } 1324 case reflect.Invalid, reflect.Func: 1325 e.e.EncodeNil() 1326 return 1327 } 1328 1329 if fn == nil { 1330 fn = e.h.fn(rv.Type()) 1331 } 1332 1333 if !fn.i.addrE { // typically, addrE = false, so check it first 1334 // keep rv same 1335 } else if rvpValid { 1336 rv = rvp 1337 } else { 1338 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr) 1339 } 1340 fn.fe(e, &fn.i, rv) 1341 1342 if sptr != nil { // remove sptr 1343 e.ci = e.ci[:len(e.ci)-1] 1344 } 1345 } 1346 1347 // encodeValueNonNil can encode a number, bool, or string 1348 // OR non-nil values of kind map, slice and chan. 1349 func (e *Encoder) encodeValueNonNil(rv reflect.Value, fn *codecFn) { 1350 if fn == nil { 1351 fn = e.h.fn(rv.Type()) 1352 } 1353 1354 if fn.i.addrE { // typically, addrE = false, so check it first 1355 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr) 1356 } 1357 fn.fe(e, &fn.i, rv) 1358 } 1359 1360 // addrRV returns a addressable value which may be readonly 1361 func (e *Encoder) addrRV(rv reflect.Value, typ, ptrType reflect.Type) (rva reflect.Value) { 1362 if rv.CanAddr() { 1363 return rvAddr(rv, ptrType) 1364 } 1365 if e.h.NoAddressableReadonly { 1366 rva = reflect.New(typ) 1367 rvSetDirect(rva.Elem(), rv) 1368 return 1369 } 1370 return rvAddr(e.perType.AddressableRO(rv), ptrType) 1371 } 1372 1373 func (e *Encoder) marshalUtf8(bs []byte, fnerr error) { 1374 e.onerror(fnerr) 1375 if bs == nil { 1376 e.e.EncodeNil() 1377 } else { 1378 e.e.EncodeString(stringView(bs)) 1379 } 1380 } 1381 1382 func (e *Encoder) marshalAsis(bs []byte, fnerr error) { 1383 e.onerror(fnerr) 1384 if bs == nil { 1385 e.e.EncodeNil() 1386 } else { 1387 e.encWr.writeb(bs) // e.asis(bs) 1388 } 1389 } 1390 1391 func (e *Encoder) marshalRaw(bs []byte, fnerr error) { 1392 e.onerror(fnerr) 1393 if bs == nil { 1394 e.e.EncodeNil() 1395 } else { 1396 e.e.EncodeStringBytesRaw(bs) 1397 } 1398 } 1399 1400 func (e *Encoder) rawBytes(vv Raw) { 1401 v := []byte(vv) 1402 if !e.h.Raw { 1403 e.errorf("Raw values cannot be encoded: %v", v) 1404 } 1405 e.encWr.writeb(v) 1406 } 1407 1408 func (e *Encoder) wrapErr(v error, err *error) { 1409 *err = wrapCodecErr(v, e.hh.Name(), 0, true) 1410 } 1411 1412 // ---- container tracker methods 1413 // Note: We update the .c after calling the callback. 1414 // This way, the callback can know what the last status was. 1415 1416 func (e *Encoder) mapStart(length int) { 1417 e.e.WriteMapStart(length) 1418 e.c = containerMapStart 1419 } 1420 1421 func (e *Encoder) mapElemKey() { 1422 if e.js { 1423 e.jsondriver().WriteMapElemKey() 1424 } 1425 e.c = containerMapKey 1426 } 1427 1428 func (e *Encoder) mapElemValue() { 1429 if e.js { 1430 e.jsondriver().WriteMapElemValue() 1431 } 1432 e.c = containerMapValue 1433 } 1434 1435 func (e *Encoder) mapEnd() { 1436 e.e.WriteMapEnd() 1437 e.c = 0 1438 } 1439 1440 func (e *Encoder) arrayStart(length int) { 1441 e.e.WriteArrayStart(length) 1442 e.c = containerArrayStart 1443 } 1444 1445 func (e *Encoder) arrayElem() { 1446 if e.js { 1447 e.jsondriver().WriteArrayElem() 1448 } 1449 e.c = containerArrayElem 1450 } 1451 1452 func (e *Encoder) arrayEnd() { 1453 e.e.WriteArrayEnd() 1454 e.c = 0 1455 } 1456 1457 // ---------- 1458 1459 func (e *Encoder) haltOnMbsOddLen(length int) { 1460 if length&1 != 0 { // similar to &1==1 or %2 == 1 1461 e.errorf("mapBySlice requires even slice length, but got %v", length) 1462 } 1463 } 1464 1465 func (e *Encoder) atEndOfEncode() { 1466 // e.e.atEndOfEncode() 1467 if e.js { 1468 e.jsondriver().atEndOfEncode() 1469 } 1470 } 1471 1472 func (e *Encoder) sideEncode(v interface{}, basetype reflect.Type, bs *[]byte) { 1473 // rv := baseRV(v) 1474 // e2 := NewEncoderBytes(bs, e.hh) 1475 // e2.encodeValue(rv, e2.h.fnNoExt(basetype)) 1476 // e2.atEndOfEncode() 1477 // e2.w().end() 1478 1479 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) { 1480 e.wb = wb 1481 e.bytes = bytes 1482 e.c = c 1483 e.e.restoreState(state) 1484 }(e.wb, e.bytes, e.c, e.e.captureState()) 1485 1486 e.wb = bytesEncAppender{encInBytes(bs)[:0], bs} 1487 e.bytes = true 1488 e.c = 0 1489 e.e.resetState() 1490 1491 // must call using fnNoExt 1492 rv := baseRV(v) 1493 e.encodeValue(rv, e.h.fnNoExt(basetype)) 1494 e.atEndOfEncode() 1495 e.w().end() 1496 } 1497 1498 func encInBytes(out *[]byte) (in []byte) { 1499 in = *out 1500 if in == nil { 1501 in = make([]byte, defEncByteBufSize) 1502 } 1503 return 1504 } 1505 1506 func encStructFieldKey(encName string, ee encDriver, w *encWr, 1507 keyType valueType, encNameAsciiAlphaNum bool, js bool) { 1508 // use if-else-if, not switch (which compiles to binary-search) 1509 // since keyType is typically valueTypeString, branch prediction is pretty good. 1510 1511 if keyType == valueTypeString { 1512 if js && encNameAsciiAlphaNum { // keyType == valueTypeString 1513 w.writeqstr(encName) 1514 } else { // keyType == valueTypeString 1515 ee.EncodeString(encName) 1516 } 1517 } else if keyType == valueTypeInt { 1518 ee.EncodeInt(must.Int(strconv.ParseInt(encName, 10, 64))) 1519 } else if keyType == valueTypeUint { 1520 ee.EncodeUint(must.Uint(strconv.ParseUint(encName, 10, 64))) 1521 } else if keyType == valueTypeFloat { 1522 ee.EncodeFloat64(must.Float(strconv.ParseFloat(encName, 64))) 1523 } else { 1524 halt.errorf("invalid struct key type: %v", keyType) 1525 } 1526 }