Skip to content

Commit

Permalink
add clbanning/anyxml functioality
Browse files Browse the repository at this point in the history
Include change made to clbanning/anyxml to handle nil maps without panic.
  • Loading branch information
clbanning committed Jul 26, 2018
1 parent 0d91012 commit 8205183
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 18 deletions.
48 changes: 30 additions & 18 deletions anyxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ const (
// Alternative values for DefaultRootTag and DefaultElementTag can be set as:
// AnyXml( v, myRootTag, myElementTag).
func AnyXml(v interface{}, tags ...string) ([]byte, error) {
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.Marshal(v)
}

var err error
s := new(string)
p := new(pretty)

var rt, et string
if len(tags) == 1 || len(tags) == 2 {
rt = tags[0]
Expand All @@ -74,6 +66,20 @@ func AnyXml(v interface{}, tags ...string) ([]byte, error) {
et = DefaultElementTag
}

if v == nil {
if useGoXmlEmptyElemSyntax {
return []byte("<" + rt + "></" + rt + ">"), nil
}
return []byte("<" + rt + "/>"), nil
}
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.Marshal(v)
}

var err error
s := new(string)
p := new(pretty)

var ss string
var b []byte
switch v.(type) {
Expand Down Expand Up @@ -114,16 +120,6 @@ func AnyXml(v interface{}, tags ...string) ([]byte, error) {
// Alternative values for DefaultRootTag and DefaultElementTag can be set as:
// AnyXmlIndent( v, "", " ", myRootTag, myElementTag).
func AnyXmlIndent(v interface{}, prefix, indent string, tags ...string) ([]byte, error) {
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.MarshalIndent(v, prefix, indent)
}

var err error
s := new(string)
p := new(pretty)
p.indent = indent
p.padding = prefix

var rt, et string
if len(tags) == 1 || len(tags) == 2 {
rt = tags[0]
Expand All @@ -136,6 +132,22 @@ func AnyXmlIndent(v interface{}, prefix, indent string, tags ...string) ([]byte,
et = DefaultElementTag
}

if v == nil {
if useGoXmlEmptyElemSyntax {
return []byte(prefix + "<" + rt + ">\n" + prefix + "</" + rt + ">"), nil
}
return []byte(prefix + "<" + rt + "/>"), nil
}
if reflect.TypeOf(v).Kind() == reflect.Struct {
return xml.MarshalIndent(v, prefix, indent)
}

var err error
s := new(string)
p := new(pretty)
p.indent = indent
p.padding = prefix

var ss string
var b []byte
switch v.(type) {
Expand Down
99 changes: 99 additions & 0 deletions anyxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,102 @@ func TestAnyXmlIndent(t *testing.T) {
}
fmt.Println("s->x:\n", string(x))
}


func TestNilMap(t *testing.T) {
XmlDefaultEmptyElemSyntax()
checkval := "<root/>"
xmlout, err := AnyXml(nil, "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

checkval = " <root/>"
xmlout, err = AnyXmlIndent(nil, " ", " ", "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

// use Go XML marshal syntax for empty element"
XmlGoEmptyElemSyntax()
checkval = "<root></root>"
xmlout, err = AnyXml(nil, "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

checkval = ` <root>
</root>`
xmlout, err = AnyXmlIndent(nil, " ", " ", "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}
XmlDefaultEmptyElemSyntax()
}

func TestNilValue(t *testing.T) {
val := map[string]interface{}{"toplevel": nil}
checkval := "<root><toplevel/></root>"

XmlDefaultEmptyElemSyntax()
xmlout, err := AnyXml(val, "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

checkval = ` <root>
<toplevel/>
</root>`
xmlout, err = AnyXmlIndent(val, " ", " ", "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

XmlGoEmptyElemSyntax()
checkval = "<root><toplevel></toplevel></root>"
xmlout, err = AnyXml(val, "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}

checkval = ` <root>
<toplevel></toplevel>
</root>`
xmlout, err = AnyXmlIndent(val, " ", " ", "root")
if err != nil {
t.Fatal(err)
}
if string(xmlout) != checkval {
fmt.Println(string(xmlout), "!=", checkval)
t.Fatal()
}
XmlDefaultEmptyElemSyntax()
}

0 comments on commit 8205183

Please sign in to comment.