diff --git a/beam/data/data.go b/beam/data/data.go index 8ebee94..15c302e 100644 --- a/beam/data/data.go +++ b/beam/data/data.go @@ -91,13 +91,13 @@ func decodeString(msg string) (string, int, error) { length = int(l) } if len(parts[1]) < length + 1 { - return "", 0, fmt.Errorf("message is less than %d bytes", length) + return "", 0, fmt.Errorf("message '%s' is %d bytes, expected at least %d", parts[1], len(parts[1]), length + 1) } payload := parts[1][:length + 1] if payload[length] != ',' { return "", 0, fmt.Errorf("message is not comma-terminated") } - return payload[:length], len(parts[0]) + length + 1, nil + return payload[:length], len(parts[0]) + 1 + length + 1, nil } func decodeHeader(msg string) (int, int, error) { diff --git a/beam/data/data_test.go b/beam/data/data_test.go index b6573a1..6beb365 100644 --- a/beam/data/data_test.go +++ b/beam/data/data_test.go @@ -1,6 +1,7 @@ package data import ( + "strings" "testing" ) @@ -89,3 +90,40 @@ func TestEncodeBinaryValue(t *testing.T) { t.Fatalf("'%v' != '%v'", output, expectedOutput) } } + +func TestDecodeString(t *testing.T) { + validEncodedStrings := []struct{ + input string + output string + skip int + }{ + {"3:foo,", "foo", 6}, + {"5:hello,", "hello", 8}, + {"5:hello,5:world,", "hello", 8}, + } + for _, sample := range validEncodedStrings { + output, skip, err := decodeString(sample.input) + if err != nil { + t.Fatalf("error decoding '%v': %v", sample.input, err) + } + if skip != sample.skip { + t.Fatalf("invalid skip: %v!=%v", skip, sample.skip) + } + if output != sample.output { + t.Fatalf("invalid output: %v!=%v", output, sample.output) + } + } +} + +func TestDecode1Key1Value(t *testing.T) { + input := "000;3:foo,6:3:bar,," + output, err := Decode(input) + if err != nil { + t.Fatal(err) + } + if v, exists := output["foo"]; !exists { + t.Fatalf("wrong output: %v\n", output) + } else if len(v) != 1 || strings.Join(v, "") != "bar" { + t.Fatalf("wrong output: %v\n", output) + } +}