Address review feedback

This commit is contained in:
vishalnayak 2016-08-09 11:01:59 -04:00
parent b43cc03f0e
commit 185363d6e0
2 changed files with 13 additions and 21 deletions

View file

@ -113,18 +113,11 @@ func Decompress(data []byte) ([]byte, bool, error) {
var err error
var reader io.ReadCloser
if data == nil || len(data) == 0 {
return nil, false, fmt.Errorf("'data' being decompressed is invalid")
}
// Read the first byte
bytesReader := bytes.NewReader(data)
firstByte, err := bytesReader.ReadByte()
if err != nil {
return nil, false, fmt.Errorf("failed to read the first byte from the input")
return nil, false, fmt.Errorf("'data' being decompressed is empty")
}
switch {
case firstByte == CompressionCanaryGzip:
case data[0] == CompressionCanaryGzip:
// If the first byte matches the canary byte, remove the canary
// byte and try to decompress the data that is after the canary.
if len(data) < 2 {
@ -132,7 +125,7 @@ func Decompress(data []byte) ([]byte, bool, error) {
}
data = data[1:]
reader, err = gzip.NewReader(bytes.NewReader(data))
case firstByte == CompressionCanaryLzw:
case data[0] == CompressionCanaryLzw:
// If the first byte matches the canary byte, remove the canary
// byte and try to decompress the data that is after the canary.
if len(data) < 2 {

View file

@ -24,9 +24,9 @@ func EncodeJSON(in interface{}) ([]byte, error) {
}
// EncodeJSONAndCompress encodes the given input into JSON and compresses the
// encoded value using Gzip format (BestCompression level). A canary byte is
// placed at the beginning of the returned bytes for the logic in decompression
// method to identify compressed input.
// encoded value (using Gzip format BestCompression level, by default). A
// canary byte is placed at the beginning of the returned bytes for the logic
// in decompression method to identify compressed input.
func EncodeJSONAndCompress(in interface{}, config *compressutil.CompressionConfig) ([]byte, error) {
if in == nil {
return nil, fmt.Errorf("input for encoding is nil")
@ -45,15 +45,14 @@ func EncodeJSONAndCompress(in interface{}, config *compressutil.CompressionConfi
}
}
// For compression, use Gzip format with 'BestCompression' level.
return compressutil.Compress(encodedBytes, config)
}
// DecompressAndDecodeJSON tries to decompress the given data. The call to
// decompress, fails if the content was not compressed in the first place,
// which is identified by a canary byte before the compressed data. If the data
// is not compressed, it is JSON decoded directly. Otherwise the decompressed
// data will be JSON decoded.
// DecodeJSON tries to decompress the given data. The call to decompress, fails
// if the content was not compressed in the first place, which is identified by
// a canary byte before the compressed data. If the data is not compressed, it
// is JSON decoded directly. Otherwise the decompressed data will be JSON
// decoded.
func DecodeJSON(data []byte, out interface{}) error {
if data == nil || len(data) == 0 {
return fmt.Errorf("'data' being decoded is nil")
@ -62,15 +61,15 @@ func DecodeJSON(data []byte, out interface{}) error {
return fmt.Errorf("output parameter 'out' is nil")
}
// Decompress the dataBytes using Gzip format.
// Decompress the data if it was compressed in the first place
decompressedBytes, uncompressed, err := compressutil.Decompress(data)
if err != nil {
return fmt.Errorf("failed to decompress JSON: err: %v", err)
}
if !uncompressed && (decompressedBytes == nil || len(decompressedBytes) == 0) {
return fmt.Errorf("decompressed data being decoded is invalid")
}
// If the input supplied failed to contain the compression canary, it
// will be notified by the compression utility. Decode the decompressed
// input.