From fadc1fed1b706148359d9811884b845b46a14268 Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 8 Feb 2019 21:31:00 +1030 Subject: [PATCH 1/6] stream/mts/meta: added ExtractAll func and added testing utilities --- stream/mts/meta/meta.go | 45 ++++++++++++++++++++++++++++++------ stream/mts/meta/meta_test.go | 23 ++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 66790315..a4db5da4 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -49,9 +49,10 @@ const ( ) var ( - errKeyAbsent = errors.New("Key does not exist in map") - errNoHeader = errors.New("Metadata string does not contain header") - errInvalidHeader = errors.New("Metadata string does not contain valid header") + errKeyAbsent = errors.New("Key does not exist in map") + errNoHeader = errors.New("Metadata string does not contain header") + errInvalidHeader = errors.New("Metadata string does not contain valid header") + errUnexpectedMetaFormat = errors.New("Unexpected meta format") ) // Metadata provides functionality for the storage and encoding of metadata @@ -168,10 +169,9 @@ func (m *Data) Encode() []byte { // key is not present in the metadata string, an error is returned. If the // metadata header is not present in the string, an error is returned. func Extract(key string, d []byte) (string, error) { - if d[0] != 0 { - return "", errNoHeader - } else if d[0] == 0 && binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { - return "", errInvalidHeader + err := checkHeader(d) + if err != nil { + return "", err } d = d[headSize:] entries := strings.Split(string(d), "\t") @@ -183,3 +183,34 @@ func Extract(key string, d []byte) (string, error) { } return "", errKeyAbsent } + +// ExtractAll extracts all metadata entries from given data. An Error is returned +// if the metadata does not have a valid header, or if the meta format is unexpected. +func ExtractAll(d []byte) ([][2]string, error) { + err := checkHeader(d) + if err != nil { + return nil, err + } + d = d[headSize:] + entries := strings.Split(string(d), "\t") + all := make([][2]string, len(entries)) + for i, entry := range entries { + kv := strings.Split(entry, "=") + if len(kv) != 2 { + return nil, errUnexpectedMetaFormat + } + copy(all[i][:], kv) + } + return all, nil +} + +// checkHeader checks that a valid metadata header exists in the given data. An +// error is returned if the header is absent, or if the header is not valid. +func checkHeader(d []byte) error { + if d[0] != 0 { + return errNoHeader + } else if d[0] == 0 && binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { + return errInvalidHeader + } + return nil +} diff --git a/stream/mts/meta/meta_test.go b/stream/mts/meta/meta_test.go index e1f9f3b7..f809b172 100644 --- a/stream/mts/meta/meta_test.go +++ b/stream/mts/meta/meta_test.go @@ -165,3 +165,26 @@ func TestReadFrom(t *testing.T) { } } } + +// TestExtractAll checks that meta.ExtractAll can correctly extract all metadata +// from descriptor data. +func TestExtractAll(t *testing.T) { + tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...) + want := [][2]string{ + { + "loc", + "a,b,c", + }, + { + "ts", + "12345", + }, + } + got, err := ExtractAll(tstMeta) + if err != nil { + t.Errorf("Unexpected error: %v\n", err) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want) + } +} From f9d8accdae77de44239c7348a5225e725576496f Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 12:14:32 +1030 Subject: [PATCH 2/6] stream/mts/meta/meta.go: Extract and ExtractAll to Get and GetAll --- stream/mts/meta/meta.go | 4 ++-- stream/mts/meta/meta_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index a4db5da4..52e81465 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -168,7 +168,7 @@ func (m *Data) Encode() []byte { // ReadFrom extracts a value from a metadata string d, for the given key. If the // key is not present in the metadata string, an error is returned. If the // metadata header is not present in the string, an error is returned. -func Extract(key string, d []byte) (string, error) { +func Get(key string, d []byte) (string, error) { err := checkHeader(d) if err != nil { return "", err @@ -186,7 +186,7 @@ func Extract(key string, d []byte) (string, error) { // ExtractAll extracts all metadata entries from given data. An Error is returned // if the metadata does not have a valid header, or if the meta format is unexpected. -func ExtractAll(d []byte) ([][2]string, error) { +func GetAll(d []byte) ([][2]string, error) { err := checkHeader(d) if err != nil { return nil, err diff --git a/stream/mts/meta/meta_test.go b/stream/mts/meta/meta_test.go index f809b172..e5f6a4ff 100644 --- a/stream/mts/meta/meta_test.go +++ b/stream/mts/meta/meta_test.go @@ -156,7 +156,7 @@ func TestReadFrom(t *testing.T) { } for _, test := range tests { - got, err := Extract(test.key, []byte(tstMeta)) + got, err := Get(test.key, []byte(tstMeta)) if err != nil { t.Errorf("Unexpected err: %v\n", err) } @@ -180,7 +180,7 @@ func TestExtractAll(t *testing.T) { "12345", }, } - got, err := ExtractAll(tstMeta) + got, err := GetAll(tstMeta) if err != nil { t.Errorf("Unexpected error: %v\n", err) } From e796a5a3b7dae48b9291c800d1663ff4f5fa4128 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 12:16:57 +1030 Subject: [PATCH 3/6] stream/mts/meta: updating function comments and test function names according to Extract->Get and ExtractAll->GetAll change --- stream/mts/meta/meta.go | 4 ++-- stream/mts/meta/meta_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 52e81465..29ed0a06 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -165,7 +165,7 @@ func (m *Data) Encode() []byte { return m.enc } -// ReadFrom extracts a value from a metadata string d, for the given key. If the +// ReadFrom gets a value from a metadata string d, for the given key. If the // key is not present in the metadata string, an error is returned. If the // metadata header is not present in the string, an error is returned. func Get(key string, d []byte) (string, error) { @@ -184,7 +184,7 @@ func Get(key string, d []byte) (string, error) { return "", errKeyAbsent } -// ExtractAll extracts all metadata entries from given data. An Error is returned +// GetAll gets all metadata entries from given data. An Error is returned // if the metadata does not have a valid header, or if the meta format is unexpected. func GetAll(d []byte) ([][2]string, error) { err := checkHeader(d) diff --git a/stream/mts/meta/meta_test.go b/stream/mts/meta/meta_test.go index e5f6a4ff..455a829c 100644 --- a/stream/mts/meta/meta_test.go +++ b/stream/mts/meta/meta_test.go @@ -138,7 +138,7 @@ func TestEncode(t *testing.T) { // TestReadFrom checks that we can correctly obtain a value for a partiular key // from a string of metadata using the ReadFrom func. -func TestReadFrom(t *testing.T) { +func TestGetFrom(t *testing.T) { tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...) tests := []struct { @@ -166,9 +166,9 @@ func TestReadFrom(t *testing.T) { } } -// TestExtractAll checks that meta.ExtractAll can correctly extract all metadata +// TestGetAll checks that meta.GetAll can correctly get all metadata // from descriptor data. -func TestExtractAll(t *testing.T) { +func TestGetAll(t *testing.T) { tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...) want := [][2]string{ { From 50575270b9da50936e52bc55d554235005fddb41 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 21:35:35 +1030 Subject: [PATCH 4/6] stream/mts/meta: checking if given slice is nil or empty and returning error if either. Also updated some func comments --- stream/mts/meta/meta.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 29ed0a06..d69a2b60 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -165,9 +165,7 @@ func (m *Data) Encode() []byte { return m.enc } -// ReadFrom gets a value from a metadata string d, for the given key. If the -// key is not present in the metadata string, an error is returned. If the -// metadata header is not present in the string, an error is returned. +// Get returns the value for the given key in d. func Get(key string, d []byte) (string, error) { err := checkHeader(d) if err != nil { @@ -184,9 +182,14 @@ func Get(key string, d []byte) (string, error) { return "", errKeyAbsent } -// GetAll gets all metadata entries from given data. An Error is returned -// if the metadata does not have a valid header, or if the meta format is unexpected. +// GetAll returns metadata keys and values from d. func GetAll(d []byte) ([][2]string, error) { + if d == nil { + return nil, errors.New("nil slice given") + } + if len(d) == 0 { + return nil, errors.New("empty slice given") + } err := checkHeader(d) if err != nil { return nil, err @@ -204,8 +207,7 @@ func GetAll(d []byte) ([][2]string, error) { return all, nil } -// checkHeader checks that a valid metadata header exists in the given data. An -// error is returned if the header is absent, or if the header is not valid. +// checkHeader checks that a valid metadata header exists in the given data. func checkHeader(d []byte) error { if d[0] != 0 { return errNoHeader From cc0becf58cd0d5916b87770370fee7bef1e927d1 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 22:25:36 +1030 Subject: [PATCH 5/6] stream/mts/meta: simplified meta checking for Get and GetAll --- stream/mts/meta/meta.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index d69a2b60..4b5cf8e9 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -50,8 +50,7 @@ const ( var ( errKeyAbsent = errors.New("Key does not exist in map") - errNoHeader = errors.New("Metadata string does not contain header") - errInvalidHeader = errors.New("Metadata string does not contain valid header") + errInvalidMeta = errors.New("Invalid metadata given") errUnexpectedMetaFormat = errors.New("Unexpected meta format") ) @@ -167,7 +166,7 @@ func (m *Data) Encode() []byte { // Get returns the value for the given key in d. func Get(key string, d []byte) (string, error) { - err := checkHeader(d) + err := checkMeta(d) if err != nil { return "", err } @@ -184,13 +183,7 @@ func Get(key string, d []byte) (string, error) { // GetAll returns metadata keys and values from d. func GetAll(d []byte) ([][2]string, error) { - if d == nil { - return nil, errors.New("nil slice given") - } - if len(d) == 0 { - return nil, errors.New("empty slice given") - } - err := checkHeader(d) + err := checkMeta(d) if err != nil { return nil, err } @@ -208,11 +201,9 @@ func GetAll(d []byte) ([][2]string, error) { } // checkHeader checks that a valid metadata header exists in the given data. -func checkHeader(d []byte) error { - if d[0] != 0 { - return errNoHeader - } else if d[0] == 0 && binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { - return errInvalidHeader +func checkMeta(d []byte) error { + if len(d) == 0 || d[0] != 0 || binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { + return errInvalidMeta } return nil } From 1f3d34b6bb0e038add0eb9c929c2209598e9700f Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 22:27:25 +1030 Subject: [PATCH 6/6] stream/mts/meta/meta_test.go: corrected func comment for TestGetFrom --- stream/mts/meta/meta_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream/mts/meta/meta_test.go b/stream/mts/meta/meta_test.go index 455a829c..459b9912 100644 --- a/stream/mts/meta/meta_test.go +++ b/stream/mts/meta/meta_test.go @@ -136,7 +136,7 @@ func TestEncode(t *testing.T) { } } -// TestReadFrom checks that we can correctly obtain a value for a partiular key +// TestGetFrom checks that we can correctly obtain a value for a partiular key // from a string of metadata using the ReadFrom func. func TestGetFrom(t *testing.T) { tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...)