Skip to content

Commit

Permalink
Add XmlCheckIsValid toggle
Browse files Browse the repository at this point in the history
After the value is encoded, the XML is checked to insure that it is valid.  Requires giithub.com/clbanning/mxj/v2.
  • Loading branch information
clbanning committed Feb 2, 2021
1 parent 2b32986 commit fec1f70
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
19 changes: 17 additions & 2 deletions anyxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@
<element>true</element>
</mydoc>
An example of encoding a map[interface{}]interface{} value with mixed key types is
An example of encoding a map[interface{}]interface{} value with mixed key types is
in anyxml/examples/goofy_map.go.
*/
package anyxml

import (
"encoding/xml"
"reflect"

// "github.com/clbanning/mxj"
"github.com/clbanning/mxj/v2"
)

// Default missingElementTag value.
var missingElemTag = "element"

// MissingElementTag is used to set the lable to be used
// for values that are not map[string]interface{} type. By default
// for values that are not map[string]interface{} type. By default
// the tag label "element" is used. The default can be reset by
// passing an empty string, "", argument: MissingElementTag("").
func MissingElementTag(s string) {
Expand Down Expand Up @@ -127,6 +130,12 @@ func Xml(v interface{}, rootTag ...string) ([]byte, error) {
b = []byte(*s)
}

if xmlCheckIsValid {
if _, err = mxj.NewMapXml(b); err != nil {
return nil, err
}
}

return b, err
}

Expand Down Expand Up @@ -190,5 +199,11 @@ func XmlIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte,
b = []byte(*s)
}

if xmlCheckIsValid {
if _, err = mxj.NewMapXml(b); err != nil {
return nil, err
}
}

return b, err
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/clbanning/anymxj/v2

go 1.15
28 changes: 28 additions & 0 deletions isvalid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package anyxml

import (
"encoding/json"
"fmt"
"testing"
)

func TestXmlCheckIsValid(t *testing.T) {
fmt.Println("================== TestXmlCheckIsValid")

data := []byte(`{"":"empty", "$invalid":"hex$", "entities":"<>&", "nil": null}`)
m := make(map[string]interface{})
err := json.Unmarshal(data, &m)
if err != nil {
t.Fatal("json.Unmarshal err;", err)
}
fmt.Printf("%v\n", m)

XmlCheckIsValid()
if _, err := Xml(m); err == nil {
t.Fatal("Xml err: nil")
}

if _, err := XmlIndent(m, "", " "); err == nil {
t.Fatal("Xml err: nil")
}
}
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ See mxj package documentation for caveats, etc.
Since some values, such as arrays, may require injecting tag labels to generate the XML, unmarshaling
the resultant XML is not necessarily symmetric, i.e., you cannot get the original value back without some manipulation.

<h4>Dependencies</h4>

import github.com/clbanning/mxj/v2

<h4>Documentation</h4>

http://godoc.org/github.com/clbanning/anyxml
Expand Down
18 changes: 16 additions & 2 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ func escapeChars(s string) string {
return string(b)
}

// From clbanning/mxj issue #88
// xmlCheckIsValid set switch to force decoding the encoded XML to
// see if it is valid XML.
var xmlCheckIsValid bool

// XmlCheckIsValid forces the encoded XML to be checked for validity.
func XmlCheckIsValid(b ...bool) {
if len(b) == 1 {
xmlCheckIsValid = b[0]
return
}
xmlCheckIsValid = !xmlCheckIsValid
}

// where the work actually happens
// returns an error if an attribute is not atomic
// patched with new version in github.com/clbanning/mxj - 2015.11.15
Expand Down Expand Up @@ -199,7 +213,7 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp
var n int
var ss string
for k, v := range vv {
if k[:1] == "-" {
if len(k) > 0 && k[:1] == "-" {
switch v.(type) {
case string:
ss = v.(string)
Expand Down Expand Up @@ -267,7 +281,7 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp
elemlist := make([][2]interface{}, len(vv))
n = 0
for k, v := range vv {
if k[:1] == "-" {
if len(k) > 0 && k[:1] == "-" {
continue
}
elemlist[n][0] = k
Expand Down

0 comments on commit fec1f70

Please sign in to comment.