cleanly separate version handling

This commit is contained in:
AJ ONeal 2020-05-04 22:12:48 -06:00
parent bdb8c87a30
commit 5b8941ed37
1 changed files with 54 additions and 49 deletions

View File

@ -46,10 +46,18 @@ func splitHeader(header []byte, names []string) (map[string]string, error) {
return result, nil
}
//ReadMessage -
// ReadMessage checks the protocol and switches accordingly
func ReadMessage(b []byte) (*Packer, error) {
// Detect protocol in use
if b[0] == packerV1 {
return ReadV1Message(b)
}
return nil, fmt.Errorf("Version %d not supported", 255-b[0])
}
// ReadV1Message parses a v1-formatted message
func ReadV1Message(b []byte) (*Packer, error) {
// Separate the header and body using the header length in the second byte.
p := NewPacker(nil)
header := b[2 : b[1]+2]
@ -98,9 +106,6 @@ func ReadMessage(b []byte) (*Packer, error) {
//handle payload
p.Data.AppendBytes(data)
return p, nil
}
return nil, fmt.Errorf("Version %d not supported", 255-b[0])
}
//PackV1 -- Outputs version 1 of packer