github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/types/types_jsonpb.go (about) 1 // Copyright 2015 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package types 12 13 import ( 14 "bytes" 15 16 "github.com/gogo/protobuf/jsonpb" 17 ) 18 19 // This file contains logic to allow the *types.T to properly marshal to json. 20 // It is a separate file to make it straightforward to defeat the linter that 21 // refuses to allow one to call a method Marshal unless it's protoutil.Marshal. 22 23 // MarshalJSONPB marshals the T to json. This is necessary as otherwise 24 // this field will be lost to the crdb_internal.pb_to_json and the likes. 25 func (t *T) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) { 26 temp := *t 27 if err := temp.downgradeType(); err != nil { 28 return nil, err 29 } 30 var buf bytes.Buffer 31 if err := marshaler.Marshal(&buf, &temp.InternalType); err != nil { 32 return nil, err 33 } 34 return buf.Bytes(), nil 35 } 36 37 // UnmarshalJSONPB unmarshals the T to json. This is necessary as otherwise 38 // this field will be lost to the crdb_internal.json_to_pb and the likes. 39 func (t *T) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, data []byte) error { 40 if err := unmarshaler.Unmarshal(bytes.NewReader(data), &t.InternalType); err != nil { 41 return err 42 } 43 return t.upgradeType() 44 }