diff options
| author | Shivakumar GN <shivakumar.gn@gmail.com> | 2013-02-03 11:21:07 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2013-02-03 11:21:07 -0500 |
| commit | 848d10f06cf327212d1ce7041c0eeae5fda317f1 (patch) | |
| tree | db089bb4b3a60abe34817d22b4059b3e1de1a547 /src/pkg/encoding | |
| parent | 09a17ca1f113b7959391b0daf49ecfcd930cf30b (diff) | |
| download | go-848d10f06cf327212d1ce7041c0eeae5fda317f1.tar.xz | |
xml: omit newline at beginning of MarshalIndent output
(Still valid XML.)
Fixes #3354.
R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/7288047
Diffstat (limited to 'src/pkg/encoding')
| -rw-r--r-- | src/pkg/encoding/xml/marshal.go | 7 | ||||
| -rw-r--r-- | src/pkg/encoding/xml/marshal_test.go | 32 |
2 files changed, 38 insertions, 1 deletions
diff --git a/src/pkg/encoding/xml/marshal.go b/src/pkg/encoding/xml/marshal.go index 803805fed3..ea891bfb3e 100644 --- a/src/pkg/encoding/xml/marshal.go +++ b/src/pkg/encoding/xml/marshal.go @@ -124,6 +124,7 @@ type printer struct { prefix string depth int indentedIn bool + putNewline bool } // marshalValue writes one or more XML elements representing val. @@ -394,7 +395,11 @@ func (p *printer) writeIndent(depthDelta int) { } p.indentedIn = false } - p.WriteByte('\n') + if p.putNewline { + p.WriteByte('\n') + } else { + p.putNewline = true + } if len(p.prefix) > 0 { p.WriteString(p.prefix) } diff --git a/src/pkg/encoding/xml/marshal_test.go b/src/pkg/encoding/xml/marshal_test.go index 67fcfd9ed5..ed856813a7 100644 --- a/src/pkg/encoding/xml/marshal_test.go +++ b/src/pkg/encoding/xml/marshal_test.go @@ -7,6 +7,7 @@ package xml import ( "bytes" "errors" + "fmt" "io" "reflect" "strconv" @@ -840,6 +841,24 @@ var marshalErrorTests = []struct { }, } +var marshalIndentTests = []struct { + Value interface{} + Prefix string + Indent string + ExpectXML string +}{ + { + Value: &SecretAgent{ + Handle: "007", + Identity: "James Bond", + Obfuscate: "<redacted/>", + }, + Prefix: "", + Indent: "\t", + ExpectXML: fmt.Sprintf("<agent handle=\"007\">\n\t<Identity>James Bond</Identity><redacted/>\n</agent>"), + }, +} + func TestMarshalErrors(t *testing.T) { for idx, test := range marshalErrorTests { _, err := Marshal(test.Value) @@ -884,6 +903,19 @@ func TestUnmarshal(t *testing.T) { } } +func TestMarshalIndent(t *testing.T) { + for i, test := range marshalIndentTests { + data, err := MarshalIndent(test.Value, test.Prefix, test.Indent) + if err != nil { + t.Errorf("#%d: Error: %s", i, err) + continue + } + if got, want := string(data), test.ExpectXML; got != want { + t.Errorf("#%d: MarshalIndent:\nGot:%s\nWant:\n%s", i, got, want) + } + } +} + type limitedBytesWriter struct { w io.Writer remain int // until writes fail |
