Serialize binary bytea cols into hex/base64 (#537)
- Adds binary serialization into hex/base64 - Default codec is base64 - Codec can be changed via `--binary-codec` CLI option
This commit is contained in:
40
pkg/client/codec.go
Normal file
40
pkg/client/codec.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
CodecNone = "none"
|
||||
CodecHex = "hex"
|
||||
CodecBase64 = "base64"
|
||||
)
|
||||
|
||||
var (
|
||||
// BinaryEncodingFormat specifies the default serialization format of binary data
|
||||
BinaryCodec = CodecBase64
|
||||
)
|
||||
|
||||
func SetBinaryCodec(codec string) error {
|
||||
switch codec {
|
||||
case CodecNone, CodecHex, CodecBase64:
|
||||
BinaryCodec = codec
|
||||
default:
|
||||
return fmt.Errorf("invalid binary codec: %v", codec)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func encodeBinaryData(data []byte, codec string) string {
|
||||
switch codec {
|
||||
case CodecHex:
|
||||
return hex.EncodeToString(data)
|
||||
case CodecBase64:
|
||||
return base64.StdEncoding.EncodeToString(data)
|
||||
default:
|
||||
return string(data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user