From b9ec74f5cd9ab31b904d855b95e13c8410249d56 Mon Sep 17 00:00:00 2001 From: Joshua Tacoma Date: Sun, 25 Nov 2012 17:20:12 -0500 Subject: [PATCH] Return remaining text from Unmarshal(). When a TNetstring is followed by other data (e.g. other TNetstrings) it's useful to know what that other data is. An additional method could be added, but the current minimal Marshal/Unmarshal is nice and clean. This is a minimal change to make that trailing data easily available. --- decode.go | 8 ++++---- tnetstring_test.go | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/decode.go b/decode.go index 37f3a0b..4005537 100644 --- a/decode.go +++ b/decode.go @@ -7,14 +7,14 @@ import ( "strings" ) -func Unmarshal(data string, v interface{}) error { +func Unmarshal(data string, v interface{}) (string, error) { val := reflect.ValueOf(v) val = reflect.Indirect(val) if !val.CanSet() { - return errors.New("tnetstring: Unmarshal requires a settable value") + return data, errors.New("tnetstring: Unmarshal requires a settable value") } - _, err := unmarshal(data, val) - return err + n, err := unmarshal(data, val) + return data[n:], err } func indirect(v reflect.Value) reflect.Value { diff --git a/tnetstring_test.go b/tnetstring_test.go index 9b688c9..6355cbe 100644 --- a/tnetstring_test.go +++ b/tnetstring_test.go @@ -76,10 +76,13 @@ func TestUnmarshal(t *testing.T) { continue } val := reflect.New(ty) - err := Unmarshal(test.data, val.Interface()) + rest, err := Unmarshal(test.data, val.Interface()) if err != nil { t.Errorf("#%d Unmarshal error: %s", i, err) } + if rest != "" { + t.Errorf("#%d Unmarshal returned non-empty left-over: %v", i, rest) + } if !reflect.DeepEqual(test.val, val.Elem().Interface()) { t.Errorf("#%d want\n%v\ngot\n%v", i, test.val, val.Elem().Interface()) }