Last active
January 15, 2020 21:26
-
-
Save amlwwalker/92d60ae8b27099c166dd013161052c4f to your computer and use it in GitHub Desktop.
Taking a struct, converting it to a byte array, gob encoding that, compressing it, decompressing it and converting back to a byte array/struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package compressor | |
import ( | |
"bytes" | |
"compress/gzip" | |
"encoding/gob" | |
"fmt" | |
"io" | |
"io/ioutil" | |
) | |
type SomeStruct struct { | |
A string | |
B int64 | |
C float64 | |
} | |
//1. | |
func StructToBytes(obj SomeStruct) (bytes.Buffer, error) { | |
//now gob this | |
var indexBuffer bytes.Buffer | |
// writer := bufio.NewWriter(&indexBuffer) | |
encoder := gob.NewEncoder(&indexBuffer) | |
if err := encoder.Encode(obj); err != nil { | |
return indexBuffer, err | |
} | |
fmt.Printf("returning [1]%+v\r\n", indexBuffer.Bytes()) | |
return indexBuffer, nil | |
} | |
//1. | |
func BytesToGob(obj []byte) (bytes.Buffer, error) { | |
//now gob this | |
var indexBuffer bytes.Buffer | |
// writer := bufio.NewWriter(&indexBuffer) | |
encoder := gob.NewEncoder(&indexBuffer) | |
if err := encoder.Encode(obj); err != nil { | |
return indexBuffer, err | |
} | |
fmt.Printf("returning [1]%+v\r\n", indexBuffer.Bytes()) | |
return indexBuffer, nil | |
} | |
//2. | |
func CompressBinary(binaryBuffer *bytes.Buffer) (bytes.Buffer, error) { | |
//now compress it | |
var compressionBuffer bytes.Buffer | |
compressor := gzip.NewWriter(&compressionBuffer) | |
_, err := compressor.Write(binaryBuffer.Bytes()) | |
err = compressor.Close() | |
fmt.Printf("returning [2]%+v error: %+s\r\n", compressionBuffer.Bytes(), err) | |
return compressionBuffer, err | |
} | |
//3. | |
func DecompressBinary(compressionBuffer bytes.Buffer) (*gzip.Reader, error) { | |
//now decompress it | |
fmt.Println("received bytes to decompress ", compressionBuffer.Bytes()) | |
dataReader := bytes.NewReader(compressionBuffer.Bytes()) | |
if reader, err := gzip.NewReader(dataReader); err != nil { | |
fmt.Println("gzip failed ", err) | |
return &gzip.Reader{}, err | |
} else { | |
err := reader.Close() | |
return reader, err | |
} | |
} | |
//4. | |
func GobToStruct(binaryBytes io.Reader) ([]byte, error) { | |
//thanks to https://stackoverflow.com/questions/59759616/golang-storing-to-disk-after-struct-byte-gob-gzip-io-gzip-go?noredirect=1#comment105665145_59759616 | |
decoder := gob.NewDecoder(binaryBytes) | |
var tmp []byte | |
if err := decoder.Decode(&tmp); err != nil { | |
if err != io.EOF && err != io.ErrUnexpectedEOF { | |
fmt.Println("gob failed ", err) | |
return tmp, err | |
} | |
} | |
fmt.Printf("tmp %+v\r\n", tmp) | |
return tmp, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package compressor | |
import ( | |
"fmt" | |
"io/ioutil" | |
"testing" | |
) | |
var testStruct = SomeStruct{ | |
A: "alex walker", | |
B: 24, | |
C: 1.234, | |
} | |
func TestMain(t *testing.T) { | |
//use assert library to check for similarity or require library to check something exists | |
t.Run("check that we can 'gob' the data correctly", func(t *testing.T) { | |
if structBytes, err := StructToBytes(testStruct); err != nil { | |
t.Error("failed to create the bytes from the struct provided ", err) | |
t.FailNow() | |
} else if binaryToGobBuffer, err := BytesToGob(structBytes.Bytes()); err != nil { | |
t.Error("failed to create the gob from the bytes provided ", err) | |
t.FailNow() | |
//issue here with gob reading from an interface | |
} else if compressedData, err := CompressBinary(&binaryToGobBuffer); err != nil { | |
t.Error("failed to create the gob from the struct provided ", err) | |
t.FailNow() | |
} else if decompressionReader, err := DecompressBinary(compressedData); err != nil { | |
t.Error("failed to decompress the binary data ", err) | |
t.FailNow() | |
} else if res, err := GobToStruct(decompressionReader); err != nil { | |
t.Error("failed to convert bytes to struct ", err) | |
t.FailNow() | |
} else { | |
fmt.Printf("result %+v\r\n", res) | |
} | |
}) | |
} | |
func openFile(path string) ([]byte, error) { | |
data, err := ioutil.ReadFile(path) | |
if err != nil { | |
fmt.Println("File reading error", err) | |
} | |
return data, err | |
} | |
func writeFile(path string, data []byte) error { | |
err := ioutil.WriteFile(path, data, 0644) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated code now works thanks to stackoverflow