github.com/lestrrat-go/jwx/v2@v2.0.21/jwe/headers_gen.go (about) 1 // Code generated by tools/cmd/genjwe/main.go. DO NOT EDIT. 2 3 package jwe 4 5 import ( 6 "bytes" 7 "context" 8 "fmt" 9 "sort" 10 "sync" 11 12 "github.com/lestrrat-go/jwx/v2/cert" 13 "github.com/lestrrat-go/jwx/v2/internal/base64" 14 "github.com/lestrrat-go/jwx/v2/internal/json" 15 "github.com/lestrrat-go/jwx/v2/internal/pool" 16 "github.com/lestrrat-go/jwx/v2/jwa" 17 "github.com/lestrrat-go/jwx/v2/jwk" 18 ) 19 20 const ( 21 AgreementPartyUInfoKey = "apu" 22 AgreementPartyVInfoKey = "apv" 23 AlgorithmKey = "alg" 24 CompressionKey = "zip" 25 ContentEncryptionKey = "enc" 26 ContentTypeKey = "cty" 27 CriticalKey = "crit" 28 EphemeralPublicKeyKey = "epk" 29 JWKKey = "jwk" 30 JWKSetURLKey = "jku" 31 KeyIDKey = "kid" 32 TypeKey = "typ" 33 X509CertChainKey = "x5c" 34 X509CertThumbprintKey = "x5t" 35 X509CertThumbprintS256Key = "x5t#S256" 36 X509URLKey = "x5u" 37 ) 38 39 // Headers describe a standard Header set. 40 type Headers interface { 41 json.Marshaler 42 json.Unmarshaler 43 AgreementPartyUInfo() []byte 44 AgreementPartyVInfo() []byte 45 Algorithm() jwa.KeyEncryptionAlgorithm 46 Compression() jwa.CompressionAlgorithm 47 ContentEncryption() jwa.ContentEncryptionAlgorithm 48 ContentType() string 49 Critical() []string 50 EphemeralPublicKey() jwk.Key 51 JWK() jwk.Key 52 JWKSetURL() string 53 KeyID() string 54 Type() string 55 X509CertChain() *cert.Chain 56 X509CertThumbprint() string 57 X509CertThumbprintS256() string 58 X509URL() string 59 Iterate(ctx context.Context) Iterator 60 Walk(ctx context.Context, v Visitor) error 61 AsMap(ctx context.Context) (map[string]interface{}, error) 62 Get(string) (interface{}, bool) 63 Set(string, interface{}) error 64 Remove(string) error 65 Encode() ([]byte, error) 66 Decode([]byte) error 67 // PrivateParams returns the map containing the non-standard ('private') parameters 68 // in the associated header. WARNING: DO NOT USE PrivateParams() 69 // IF YOU HAVE CONCURRENT CODE ACCESSING THEM. Use AsMap() to 70 // get a copy of the entire header instead 71 PrivateParams() map[string]interface{} 72 Clone(context.Context) (Headers, error) 73 Copy(context.Context, Headers) error 74 Merge(context.Context, Headers) (Headers, error) 75 } 76 77 type stdHeaders struct { 78 agreementPartyUInfo []byte 79 agreementPartyVInfo []byte 80 algorithm *jwa.KeyEncryptionAlgorithm 81 compression *jwa.CompressionAlgorithm 82 contentEncryption *jwa.ContentEncryptionAlgorithm 83 contentType *string 84 critical []string 85 ephemeralPublicKey jwk.Key 86 jwk jwk.Key 87 jwkSetURL *string 88 keyID *string 89 typ *string 90 x509CertChain *cert.Chain 91 x509CertThumbprint *string 92 x509CertThumbprintS256 *string 93 x509URL *string 94 privateParams map[string]interface{} 95 mu *sync.RWMutex 96 } 97 98 func NewHeaders() Headers { 99 return &stdHeaders{ 100 mu: &sync.RWMutex{}, 101 privateParams: map[string]interface{}{}, 102 } 103 } 104 105 func (h *stdHeaders) AgreementPartyUInfo() []byte { 106 h.mu.RLock() 107 defer h.mu.RUnlock() 108 return h.agreementPartyUInfo 109 } 110 111 func (h *stdHeaders) AgreementPartyVInfo() []byte { 112 h.mu.RLock() 113 defer h.mu.RUnlock() 114 return h.agreementPartyVInfo 115 } 116 117 func (h *stdHeaders) Algorithm() jwa.KeyEncryptionAlgorithm { 118 h.mu.RLock() 119 defer h.mu.RUnlock() 120 if h.algorithm == nil { 121 return "" 122 } 123 return *(h.algorithm) 124 } 125 126 func (h *stdHeaders) Compression() jwa.CompressionAlgorithm { 127 h.mu.RLock() 128 defer h.mu.RUnlock() 129 if h.compression == nil { 130 return jwa.NoCompress 131 } 132 return *(h.compression) 133 } 134 135 func (h *stdHeaders) ContentEncryption() jwa.ContentEncryptionAlgorithm { 136 h.mu.RLock() 137 defer h.mu.RUnlock() 138 if h.contentEncryption == nil { 139 return "" 140 } 141 return *(h.contentEncryption) 142 } 143 144 func (h *stdHeaders) ContentType() string { 145 h.mu.RLock() 146 defer h.mu.RUnlock() 147 if h.contentType == nil { 148 return "" 149 } 150 return *(h.contentType) 151 } 152 153 func (h *stdHeaders) Critical() []string { 154 h.mu.RLock() 155 defer h.mu.RUnlock() 156 return h.critical 157 } 158 159 func (h *stdHeaders) EphemeralPublicKey() jwk.Key { 160 h.mu.RLock() 161 defer h.mu.RUnlock() 162 return h.ephemeralPublicKey 163 } 164 165 func (h *stdHeaders) JWK() jwk.Key { 166 h.mu.RLock() 167 defer h.mu.RUnlock() 168 return h.jwk 169 } 170 171 func (h *stdHeaders) JWKSetURL() string { 172 h.mu.RLock() 173 defer h.mu.RUnlock() 174 if h.jwkSetURL == nil { 175 return "" 176 } 177 return *(h.jwkSetURL) 178 } 179 180 func (h *stdHeaders) KeyID() string { 181 h.mu.RLock() 182 defer h.mu.RUnlock() 183 if h.keyID == nil { 184 return "" 185 } 186 return *(h.keyID) 187 } 188 189 func (h *stdHeaders) Type() string { 190 h.mu.RLock() 191 defer h.mu.RUnlock() 192 if h.typ == nil { 193 return "" 194 } 195 return *(h.typ) 196 } 197 198 func (h *stdHeaders) X509CertChain() *cert.Chain { 199 h.mu.RLock() 200 defer h.mu.RUnlock() 201 return h.x509CertChain 202 } 203 204 func (h *stdHeaders) X509CertThumbprint() string { 205 h.mu.RLock() 206 defer h.mu.RUnlock() 207 if h.x509CertThumbprint == nil { 208 return "" 209 } 210 return *(h.x509CertThumbprint) 211 } 212 213 func (h *stdHeaders) X509CertThumbprintS256() string { 214 h.mu.RLock() 215 defer h.mu.RUnlock() 216 if h.x509CertThumbprintS256 == nil { 217 return "" 218 } 219 return *(h.x509CertThumbprintS256) 220 } 221 222 func (h *stdHeaders) X509URL() string { 223 h.mu.RLock() 224 defer h.mu.RUnlock() 225 if h.x509URL == nil { 226 return "" 227 } 228 return *(h.x509URL) 229 } 230 231 func (h *stdHeaders) makePairs() []*HeaderPair { 232 h.mu.RLock() 233 defer h.mu.RUnlock() 234 var pairs []*HeaderPair 235 if h.agreementPartyUInfo != nil { 236 pairs = append(pairs, &HeaderPair{Key: AgreementPartyUInfoKey, Value: h.agreementPartyUInfo}) 237 } 238 if h.agreementPartyVInfo != nil { 239 pairs = append(pairs, &HeaderPair{Key: AgreementPartyVInfoKey, Value: h.agreementPartyVInfo}) 240 } 241 if h.algorithm != nil { 242 pairs = append(pairs, &HeaderPair{Key: AlgorithmKey, Value: *(h.algorithm)}) 243 } 244 if h.compression != nil { 245 pairs = append(pairs, &HeaderPair{Key: CompressionKey, Value: *(h.compression)}) 246 } 247 if h.contentEncryption != nil { 248 pairs = append(pairs, &HeaderPair{Key: ContentEncryptionKey, Value: *(h.contentEncryption)}) 249 } 250 if h.contentType != nil { 251 pairs = append(pairs, &HeaderPair{Key: ContentTypeKey, Value: *(h.contentType)}) 252 } 253 if h.critical != nil { 254 pairs = append(pairs, &HeaderPair{Key: CriticalKey, Value: h.critical}) 255 } 256 if h.ephemeralPublicKey != nil { 257 pairs = append(pairs, &HeaderPair{Key: EphemeralPublicKeyKey, Value: h.ephemeralPublicKey}) 258 } 259 if h.jwk != nil { 260 pairs = append(pairs, &HeaderPair{Key: JWKKey, Value: h.jwk}) 261 } 262 if h.jwkSetURL != nil { 263 pairs = append(pairs, &HeaderPair{Key: JWKSetURLKey, Value: *(h.jwkSetURL)}) 264 } 265 if h.keyID != nil { 266 pairs = append(pairs, &HeaderPair{Key: KeyIDKey, Value: *(h.keyID)}) 267 } 268 if h.typ != nil { 269 pairs = append(pairs, &HeaderPair{Key: TypeKey, Value: *(h.typ)}) 270 } 271 if h.x509CertChain != nil { 272 pairs = append(pairs, &HeaderPair{Key: X509CertChainKey, Value: h.x509CertChain}) 273 } 274 if h.x509CertThumbprint != nil { 275 pairs = append(pairs, &HeaderPair{Key: X509CertThumbprintKey, Value: *(h.x509CertThumbprint)}) 276 } 277 if h.x509CertThumbprintS256 != nil { 278 pairs = append(pairs, &HeaderPair{Key: X509CertThumbprintS256Key, Value: *(h.x509CertThumbprintS256)}) 279 } 280 if h.x509URL != nil { 281 pairs = append(pairs, &HeaderPair{Key: X509URLKey, Value: *(h.x509URL)}) 282 } 283 for k, v := range h.privateParams { 284 pairs = append(pairs, &HeaderPair{Key: k, Value: v}) 285 } 286 return pairs 287 } 288 289 func (h *stdHeaders) PrivateParams() map[string]interface{} { 290 h.mu.RLock() 291 defer h.mu.RUnlock() 292 return h.privateParams 293 } 294 295 func (h *stdHeaders) Get(name string) (interface{}, bool) { 296 h.mu.RLock() 297 defer h.mu.RUnlock() 298 switch name { 299 case AgreementPartyUInfoKey: 300 if h.agreementPartyUInfo == nil { 301 return nil, false 302 } 303 return h.agreementPartyUInfo, true 304 case AgreementPartyVInfoKey: 305 if h.agreementPartyVInfo == nil { 306 return nil, false 307 } 308 return h.agreementPartyVInfo, true 309 case AlgorithmKey: 310 if h.algorithm == nil { 311 return nil, false 312 } 313 return *(h.algorithm), true 314 case CompressionKey: 315 if h.compression == nil { 316 return nil, false 317 } 318 return *(h.compression), true 319 case ContentEncryptionKey: 320 if h.contentEncryption == nil { 321 return nil, false 322 } 323 return *(h.contentEncryption), true 324 case ContentTypeKey: 325 if h.contentType == nil { 326 return nil, false 327 } 328 return *(h.contentType), true 329 case CriticalKey: 330 if h.critical == nil { 331 return nil, false 332 } 333 return h.critical, true 334 case EphemeralPublicKeyKey: 335 if h.ephemeralPublicKey == nil { 336 return nil, false 337 } 338 return h.ephemeralPublicKey, true 339 case JWKKey: 340 if h.jwk == nil { 341 return nil, false 342 } 343 return h.jwk, true 344 case JWKSetURLKey: 345 if h.jwkSetURL == nil { 346 return nil, false 347 } 348 return *(h.jwkSetURL), true 349 case KeyIDKey: 350 if h.keyID == nil { 351 return nil, false 352 } 353 return *(h.keyID), true 354 case TypeKey: 355 if h.typ == nil { 356 return nil, false 357 } 358 return *(h.typ), true 359 case X509CertChainKey: 360 if h.x509CertChain == nil { 361 return nil, false 362 } 363 return h.x509CertChain, true 364 case X509CertThumbprintKey: 365 if h.x509CertThumbprint == nil { 366 return nil, false 367 } 368 return *(h.x509CertThumbprint), true 369 case X509CertThumbprintS256Key: 370 if h.x509CertThumbprintS256 == nil { 371 return nil, false 372 } 373 return *(h.x509CertThumbprintS256), true 374 case X509URLKey: 375 if h.x509URL == nil { 376 return nil, false 377 } 378 return *(h.x509URL), true 379 default: 380 v, ok := h.privateParams[name] 381 return v, ok 382 } 383 } 384 385 func (h *stdHeaders) Set(name string, value interface{}) error { 386 h.mu.Lock() 387 defer h.mu.Unlock() 388 return h.setNoLock(name, value) 389 } 390 391 func (h *stdHeaders) setNoLock(name string, value interface{}) error { 392 switch name { 393 case AgreementPartyUInfoKey: 394 if v, ok := value.([]byte); ok { 395 h.agreementPartyUInfo = v 396 return nil 397 } 398 return fmt.Errorf(`invalid value for %s key: %T`, AgreementPartyUInfoKey, value) 399 case AgreementPartyVInfoKey: 400 if v, ok := value.([]byte); ok { 401 h.agreementPartyVInfo = v 402 return nil 403 } 404 return fmt.Errorf(`invalid value for %s key: %T`, AgreementPartyVInfoKey, value) 405 case AlgorithmKey: 406 if v, ok := value.(jwa.KeyEncryptionAlgorithm); ok { 407 h.algorithm = &v 408 return nil 409 } 410 return fmt.Errorf(`invalid value for %s key: %T`, AlgorithmKey, value) 411 case CompressionKey: 412 if v, ok := value.(jwa.CompressionAlgorithm); ok { 413 h.compression = &v 414 return nil 415 } 416 return fmt.Errorf(`invalid value for %s key: %T`, CompressionKey, value) 417 case ContentEncryptionKey: 418 if v, ok := value.(jwa.ContentEncryptionAlgorithm); ok { 419 if v == "" { 420 return fmt.Errorf(`"enc" field cannot be an empty string`) 421 } 422 h.contentEncryption = &v 423 return nil 424 } 425 return fmt.Errorf(`invalid value for %s key: %T`, ContentEncryptionKey, value) 426 case ContentTypeKey: 427 if v, ok := value.(string); ok { 428 h.contentType = &v 429 return nil 430 } 431 return fmt.Errorf(`invalid value for %s key: %T`, ContentTypeKey, value) 432 case CriticalKey: 433 if v, ok := value.([]string); ok { 434 h.critical = v 435 return nil 436 } 437 return fmt.Errorf(`invalid value for %s key: %T`, CriticalKey, value) 438 case EphemeralPublicKeyKey: 439 if v, ok := value.(jwk.Key); ok { 440 h.ephemeralPublicKey = v 441 return nil 442 } 443 return fmt.Errorf(`invalid value for %s key: %T`, EphemeralPublicKeyKey, value) 444 case JWKKey: 445 if v, ok := value.(jwk.Key); ok { 446 h.jwk = v 447 return nil 448 } 449 return fmt.Errorf(`invalid value for %s key: %T`, JWKKey, value) 450 case JWKSetURLKey: 451 if v, ok := value.(string); ok { 452 h.jwkSetURL = &v 453 return nil 454 } 455 return fmt.Errorf(`invalid value for %s key: %T`, JWKSetURLKey, value) 456 case KeyIDKey: 457 if v, ok := value.(string); ok { 458 h.keyID = &v 459 return nil 460 } 461 return fmt.Errorf(`invalid value for %s key: %T`, KeyIDKey, value) 462 case TypeKey: 463 if v, ok := value.(string); ok { 464 h.typ = &v 465 return nil 466 } 467 return fmt.Errorf(`invalid value for %s key: %T`, TypeKey, value) 468 case X509CertChainKey: 469 if v, ok := value.(*cert.Chain); ok { 470 h.x509CertChain = v 471 return nil 472 } 473 return fmt.Errorf(`invalid value for %s key: %T`, X509CertChainKey, value) 474 case X509CertThumbprintKey: 475 if v, ok := value.(string); ok { 476 h.x509CertThumbprint = &v 477 return nil 478 } 479 return fmt.Errorf(`invalid value for %s key: %T`, X509CertThumbprintKey, value) 480 case X509CertThumbprintS256Key: 481 if v, ok := value.(string); ok { 482 h.x509CertThumbprintS256 = &v 483 return nil 484 } 485 return fmt.Errorf(`invalid value for %s key: %T`, X509CertThumbprintS256Key, value) 486 case X509URLKey: 487 if v, ok := value.(string); ok { 488 h.x509URL = &v 489 return nil 490 } 491 return fmt.Errorf(`invalid value for %s key: %T`, X509URLKey, value) 492 default: 493 if h.privateParams == nil { 494 h.privateParams = map[string]interface{}{} 495 } 496 h.privateParams[name] = value 497 } 498 return nil 499 } 500 501 func (h *stdHeaders) Remove(key string) error { 502 h.mu.Lock() 503 defer h.mu.Unlock() 504 switch key { 505 case AgreementPartyUInfoKey: 506 h.agreementPartyUInfo = nil 507 case AgreementPartyVInfoKey: 508 h.agreementPartyVInfo = nil 509 case AlgorithmKey: 510 h.algorithm = nil 511 case CompressionKey: 512 h.compression = nil 513 case ContentEncryptionKey: 514 h.contentEncryption = nil 515 case ContentTypeKey: 516 h.contentType = nil 517 case CriticalKey: 518 h.critical = nil 519 case EphemeralPublicKeyKey: 520 h.ephemeralPublicKey = nil 521 case JWKKey: 522 h.jwk = nil 523 case JWKSetURLKey: 524 h.jwkSetURL = nil 525 case KeyIDKey: 526 h.keyID = nil 527 case TypeKey: 528 h.typ = nil 529 case X509CertChainKey: 530 h.x509CertChain = nil 531 case X509CertThumbprintKey: 532 h.x509CertThumbprint = nil 533 case X509CertThumbprintS256Key: 534 h.x509CertThumbprintS256 = nil 535 case X509URLKey: 536 h.x509URL = nil 537 default: 538 delete(h.privateParams, key) 539 } 540 return nil 541 } 542 543 func (h *stdHeaders) UnmarshalJSON(buf []byte) error { 544 h.agreementPartyUInfo = nil 545 h.agreementPartyVInfo = nil 546 h.algorithm = nil 547 h.compression = nil 548 h.contentEncryption = nil 549 h.contentType = nil 550 h.critical = nil 551 h.ephemeralPublicKey = nil 552 h.jwk = nil 553 h.jwkSetURL = nil 554 h.keyID = nil 555 h.typ = nil 556 h.x509CertChain = nil 557 h.x509CertThumbprint = nil 558 h.x509CertThumbprintS256 = nil 559 h.x509URL = nil 560 dec := json.NewDecoder(bytes.NewReader(buf)) 561 LOOP: 562 for { 563 tok, err := dec.Token() 564 if err != nil { 565 return fmt.Errorf(`error reading token: %w`, err) 566 } 567 switch tok := tok.(type) { 568 case json.Delim: 569 // Assuming we're doing everything correctly, we should ONLY 570 // get either '{' or '}' here. 571 if tok == '}' { // End of object 572 break LOOP 573 } else if tok != '{' { 574 return fmt.Errorf(`expected '{', but got '%c'`, tok) 575 } 576 case string: // Objects can only have string keys 577 switch tok { 578 case AgreementPartyUInfoKey: 579 if err := json.AssignNextBytesToken(&h.agreementPartyUInfo, dec); err != nil { 580 return fmt.Errorf(`failed to decode value for key %s: %w`, AgreementPartyUInfoKey, err) 581 } 582 case AgreementPartyVInfoKey: 583 if err := json.AssignNextBytesToken(&h.agreementPartyVInfo, dec); err != nil { 584 return fmt.Errorf(`failed to decode value for key %s: %w`, AgreementPartyVInfoKey, err) 585 } 586 case AlgorithmKey: 587 var decoded jwa.KeyEncryptionAlgorithm 588 if err := dec.Decode(&decoded); err != nil { 589 return fmt.Errorf(`failed to decode value for key %s: %w`, AlgorithmKey, err) 590 } 591 h.algorithm = &decoded 592 case CompressionKey: 593 var decoded jwa.CompressionAlgorithm 594 if err := dec.Decode(&decoded); err != nil { 595 return fmt.Errorf(`failed to decode value for key %s: %w`, CompressionKey, err) 596 } 597 h.compression = &decoded 598 case ContentEncryptionKey: 599 var decoded jwa.ContentEncryptionAlgorithm 600 if err := dec.Decode(&decoded); err != nil { 601 return fmt.Errorf(`failed to decode value for key %s: %w`, ContentEncryptionKey, err) 602 } 603 h.contentEncryption = &decoded 604 case ContentTypeKey: 605 if err := json.AssignNextStringToken(&h.contentType, dec); err != nil { 606 return fmt.Errorf(`failed to decode value for key %s: %w`, ContentTypeKey, err) 607 } 608 case CriticalKey: 609 var decoded []string 610 if err := dec.Decode(&decoded); err != nil { 611 return fmt.Errorf(`failed to decode value for key %s: %w`, CriticalKey, err) 612 } 613 h.critical = decoded 614 case EphemeralPublicKeyKey: 615 var buf json.RawMessage 616 if err := dec.Decode(&buf); err != nil { 617 return fmt.Errorf(`failed to decode value for key %s:%w`, EphemeralPublicKeyKey, err) 618 } 619 key, err := jwk.ParseKey(buf) 620 if err != nil { 621 return fmt.Errorf(`failed to parse JWK for key %s: %w`, EphemeralPublicKeyKey, err) 622 } 623 h.ephemeralPublicKey = key 624 case JWKKey: 625 var buf json.RawMessage 626 if err := dec.Decode(&buf); err != nil { 627 return fmt.Errorf(`failed to decode value for key %s:%w`, JWKKey, err) 628 } 629 key, err := jwk.ParseKey(buf) 630 if err != nil { 631 return fmt.Errorf(`failed to parse JWK for key %s: %w`, JWKKey, err) 632 } 633 h.jwk = key 634 case JWKSetURLKey: 635 if err := json.AssignNextStringToken(&h.jwkSetURL, dec); err != nil { 636 return fmt.Errorf(`failed to decode value for key %s: %w`, JWKSetURLKey, err) 637 } 638 case KeyIDKey: 639 if err := json.AssignNextStringToken(&h.keyID, dec); err != nil { 640 return fmt.Errorf(`failed to decode value for key %s: %w`, KeyIDKey, err) 641 } 642 case TypeKey: 643 if err := json.AssignNextStringToken(&h.typ, dec); err != nil { 644 return fmt.Errorf(`failed to decode value for key %s: %w`, TypeKey, err) 645 } 646 case X509CertChainKey: 647 var decoded cert.Chain 648 if err := dec.Decode(&decoded); err != nil { 649 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertChainKey, err) 650 } 651 h.x509CertChain = &decoded 652 case X509CertThumbprintKey: 653 if err := json.AssignNextStringToken(&h.x509CertThumbprint, dec); err != nil { 654 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertThumbprintKey, err) 655 } 656 case X509CertThumbprintS256Key: 657 if err := json.AssignNextStringToken(&h.x509CertThumbprintS256, dec); err != nil { 658 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertThumbprintS256Key, err) 659 } 660 case X509URLKey: 661 if err := json.AssignNextStringToken(&h.x509URL, dec); err != nil { 662 return fmt.Errorf(`failed to decode value for key %s: %w`, X509URLKey, err) 663 } 664 default: 665 decoded, err := registry.Decode(dec, tok) 666 if err != nil { 667 return err 668 } 669 h.setNoLock(tok, decoded) 670 } 671 default: 672 return fmt.Errorf(`invalid token %T`, tok) 673 } 674 } 675 return nil 676 } 677 678 func (h stdHeaders) MarshalJSON() ([]byte, error) { 679 data := make(map[string]interface{}) 680 fields := make([]string, 0, 16) 681 for _, pair := range h.makePairs() { 682 fields = append(fields, pair.Key.(string)) 683 data[pair.Key.(string)] = pair.Value 684 } 685 686 sort.Strings(fields) 687 buf := pool.GetBytesBuffer() 688 defer pool.ReleaseBytesBuffer(buf) 689 buf.WriteByte('{') 690 enc := json.NewEncoder(buf) 691 for i, f := range fields { 692 if i > 0 { 693 buf.WriteRune(',') 694 } 695 buf.WriteRune('"') 696 buf.WriteString(f) 697 buf.WriteString(`":`) 698 v := data[f] 699 switch v := v.(type) { 700 case []byte: 701 buf.WriteRune('"') 702 buf.WriteString(base64.EncodeToString(v)) 703 buf.WriteRune('"') 704 default: 705 if err := enc.Encode(v); err != nil { 706 return nil, fmt.Errorf(`failed to encode value for field %s`, f) 707 } 708 buf.Truncate(buf.Len() - 1) 709 } 710 } 711 buf.WriteByte('}') 712 ret := make([]byte, buf.Len()) 713 copy(ret, buf.Bytes()) 714 return ret, nil 715 }