From 7834d53f9bf1f3ff33efd2f4d065562509895355 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 3 Apr 2017 19:44:25 -0400 Subject: [PATCH] Trim spaces when unmarshalling UIDs This ensures the correct value is parsed when SGML tags aren't closed. Also add a test. --- types.go | 10 ++++++++++ types_test.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/types.go b/types.go index 7f4fe2d..f31b4f9 100644 --- a/types.go +++ b/types.go @@ -237,6 +237,16 @@ func (ob Boolean) Equal(o Boolean) bool { type UID string +func (ou *UID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + var value string + err := d.DecodeElement(&value, &start) + if err != nil { + return err + } + *ou = UID(strings.TrimSpace(value)) + return nil +} + // The OFX specification recommends that UIDs follow the standard UUID // 36-character format func (ou UID) RecommendedFormat() (bool, error) { diff --git a/types_test.go b/types_test.go index c1a0c5a..5b0d357 100644 --- a/types_test.go +++ b/types_test.go @@ -250,6 +250,8 @@ func TestUnmarshalString(t *testing.T) { unmarshalHelper(t, " new line ", &s, &overwritten) s = "Some Name" unmarshalHelper(t, "Some Name", &s, &overwritten) + // Make sure stray newlines are handled properly + unmarshalHelper(t, "Some Name\n", &s, &overwritten) } func TestMarshalBoolean(t *testing.T) { @@ -264,6 +266,8 @@ func TestUnmarshalBoolean(t *testing.T) { unmarshalHelper(t, "Y", &b, &overwritten) b = false unmarshalHelper(t, "N", &b, &overwritten) + // Make sure stray newlines are handled properly + unmarshalHelper(t, "N\n", &b, &overwritten) } func TestMarshalUID(t *testing.T) { @@ -274,6 +278,9 @@ func TestMarshalUID(t *testing.T) { func TestUnmarshalUID(t *testing.T) { var u, overwritten ofxgo.UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04", "" unmarshalHelper(t, "d1cf3d3d-9ef9-4a97-b180-81706829cb04", &u, &overwritten) + // Make sure stray newlines are handled properly + u = "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab" + unmarshalHelper(t, "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab\n", &u, &overwritten) } func TestUIDRecommendedFormat(t *testing.T) {