MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / Decode

Method Decode

base/collection_gocb.go:473–507  ·  view source on GitHub ↗

SGJSONTranscoder supports reading BinaryType documents as JSON, for backward compatibility with legacy Sync Gateway data

(bytes []byte, flags uint32, out interface{})

Source from the content-addressed store, hash-verified

471// SGJSONTranscoder supports reading BinaryType documents as JSON, for backward
472// compatibility with legacy Sync Gateway data
473func (t *SGJSONTranscoder) Decode(bytes []byte, flags uint32, out interface{}) error {
474 valueType, compression := gocbcore.DecodeCommonFlags(flags)
475
476 // Make sure compression is disabled
477 if compression != gocbcore.NoCompression {
478 return errors.New("unexpected value compression")
479 }
480 // Type-based decoding
481 if valueType == gocbcore.BinaryType {
482 switch typedOut := out.(type) {
483 case *[]byte:
484 *typedOut = bytes
485 return nil
486 case *interface{}:
487 *typedOut = bytes
488 return nil
489 case *string:
490 *typedOut = string(bytes)
491 return nil
492 default:
493 return errors.New("you must encode raw JSON data in a byte array or string")
494 }
495 } else if valueType == gocbcore.StringType {
496 return gocb.NewRawStringTranscoder().Decode(bytes, flags, out)
497 } else if valueType == gocbcore.JSONType {
498 switch out.(type) {
499 case []byte, *[]byte:
500 return gocb.NewRawJSONTranscoder().Decode(bytes, flags, out)
501 default:
502 return gocb.NewJSONTranscoder().Decode(bytes, flags, out)
503 }
504 }
505
506 return errors.New("unexpected expectedFlags value")
507}
508
509// SGJSONTranscoder.Encode supports writing JSON as either raw bytes or an unmarshalled interface
510func (t *SGJSONTranscoder) Encode(value interface{}) ([]byte, uint32, error) {

Callers

nothing calls this directly

Calls 1

DecodeMethod · 0.65

Tested by

no test coverage detected