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:
Dan Sosedoff
2021-12-29 11:03:50 -06:00
committed by GitHub
parent 1323012cff
commit 706caa44bf
8 changed files with 159 additions and 32 deletions

40
pkg/client/codec.go Normal file
View 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)
}
}