diff --git a/anyxml.go b/anyxml.go index f870126..ef89561 100644 --- a/anyxml.go +++ b/anyxml.go @@ -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] @@ -74,6 +66,20 @@ func AnyXml(v interface{}, tags ...string) ([]byte, error) { et = DefaultElementTag } + if v == nil { + if useGoXmlEmptyElemSyntax { + return []byte("<" + 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) { @@ -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] @@ -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 + ""), 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) { diff --git a/anyxml_test.go b/anyxml_test.go index 5401b6f..72bd2d9 100644 --- a/anyxml_test.go +++ b/anyxml_test.go @@ -108,3 +108,102 @@ func TestAnyXmlIndent(t *testing.T) { } fmt.Println("s->x:\n", string(x)) } + + +func TestNilMap(t *testing.T) { + XmlDefaultEmptyElemSyntax() + checkval := "" + xmlout, err := AnyXml(nil, "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + + checkval = " " + 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 = "" + xmlout, err = AnyXml(nil, "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + + checkval = ` + ` + 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 := "" + + XmlDefaultEmptyElemSyntax() + xmlout, err := AnyXml(val, "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + + checkval = ` + + ` + xmlout, err = AnyXmlIndent(val, " ", " ", "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + + XmlGoEmptyElemSyntax() + checkval = "" + xmlout, err = AnyXml(val, "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + + checkval = ` + + ` + xmlout, err = AnyXmlIndent(val, " ", " ", "root") + if err != nil { + t.Fatal(err) + } + if string(xmlout) != checkval { + fmt.Println(string(xmlout), "!=", checkval) + t.Fatal() + } + XmlDefaultEmptyElemSyntax() +}