mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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
This commit is contained in:
parent
09a17ca1f1
commit
848d10f06c
2 changed files with 38 additions and 1 deletions
|
|
@ -124,6 +124,7 @@ type printer struct {
|
||||||
prefix string
|
prefix string
|
||||||
depth int
|
depth int
|
||||||
indentedIn bool
|
indentedIn bool
|
||||||
|
putNewline bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// marshalValue writes one or more XML elements representing val.
|
// marshalValue writes one or more XML elements representing val.
|
||||||
|
|
@ -394,7 +395,11 @@ func (p *printer) writeIndent(depthDelta int) {
|
||||||
}
|
}
|
||||||
p.indentedIn = false
|
p.indentedIn = false
|
||||||
}
|
}
|
||||||
|
if p.putNewline {
|
||||||
p.WriteByte('\n')
|
p.WriteByte('\n')
|
||||||
|
} else {
|
||||||
|
p.putNewline = true
|
||||||
|
}
|
||||||
if len(p.prefix) > 0 {
|
if len(p.prefix) > 0 {
|
||||||
p.WriteString(p.prefix)
|
p.WriteString(p.prefix)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ package xml
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"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) {
|
func TestMarshalErrors(t *testing.T) {
|
||||||
for idx, test := range marshalErrorTests {
|
for idx, test := range marshalErrorTests {
|
||||||
_, err := Marshal(test.Value)
|
_, 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 {
|
type limitedBytesWriter struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
remain int // until writes fail
|
remain int // until writes fail
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue