Skip to content

Commit f0d72b6

Browse files
committed
address issue #90, part b
1 parent 792145c commit f0d72b6

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

attrprefix_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ func TestMarshalPrefixDefault(t *testing.T) {
120120

121121
func TestMarshalPrefixNoHyphen(t *testing.T) {
122122
fmt.Println("----------------- TestMarshalPrefixNoHyphen ...")
123+
// 2021.03.09 - per issue #90, no produces a complex element
123124
PrependAttrWithHyphen(false)
124125
m, err := NewMapXml(data)
125126
if err != nil {
126127
t.Fatal(err)
127128
}
128-
_, err = m.XmlIndent("", " ")
129-
if err == nil {
129+
x, err := m.XmlIndent("", " ")
130+
if err != nil {
130131
t.Fatal("error not reported for invalid key label")
131132
}
132-
fmt.Println("err ok:", err)
133+
fmt.Println("x:", string(x))
133134
}
134135

135136
func TestMarshalPrefixUnderscore(t *testing.T) {

xml.go

+33-8
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ func XmlCheckIsValid(b ...bool) {
650650
xmlCheckIsValid = b[0]
651651
return
652652
}
653-
xmlCheckIsValid = !xmlCheckIsValid
653+
xmlCheckIsValid = !xmlCheckIsValid
654654
}
655655

656656
// Encode a Map as XML. The companion of NewMapXml().
@@ -1108,7 +1108,9 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
11081108
}
11091109

11101110
// simple element? Note: '#text" is an invalid XML tag.
1111+
isComplex := false
11111112
if v, ok := vv["#text"]; ok && n+1 == lenvv {
1113+
// just the value and attributes
11121114
switch v.(type) {
11131115
case string:
11141116
if xmlEscapeChars {
@@ -1119,6 +1121,8 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
11191121
case []byte:
11201122
if xmlEscapeChars {
11211123
v = escapeChars(string(v.([]byte)))
1124+
} else {
1125+
v = string(v.([]byte))
11221126
}
11231127
}
11241128
if _, err = b.WriteString(">" + fmt.Sprintf("%v", v)); err != nil {
@@ -1129,16 +1133,33 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
11291133
isSimple = true
11301134
break
11311135
} else if ok {
1132-
// Handle edge case where simple element with attributes
1133-
// is unmarshal'd using NewMapXml() where attribute prefix
1134-
// has been set to "".
1135-
// TODO(clb): should probably scan all keys for invalid chars.
1136-
return fmt.Errorf("invalid attribute key label: #text - due to attributes not being prefixed")
1136+
// need to handle when there are subelements in addition to the simple element value
1137+
// issue #90
1138+
switch v.(type) {
1139+
case string:
1140+
if xmlEscapeChars {
1141+
v = escapeChars(v.(string))
1142+
} else {
1143+
v = v.(string)
1144+
}
1145+
case []byte:
1146+
if xmlEscapeChars {
1147+
v = escapeChars(string(v.([]byte)))
1148+
} else {
1149+
v = string(v.([]byte))
1150+
}
1151+
}
1152+
if _, err = b.WriteString(">" + fmt.Sprintf("%v", v)); err != nil {
1153+
return err
1154+
}
1155+
isComplex = true
11371156
}
11381157

11391158
// close tag with possible attributes
1140-
if _, err = b.WriteString(">"); err != nil {
1141-
return err
1159+
if !isComplex {
1160+
if _, err = b.WriteString(">"); err != nil {
1161+
return err
1162+
}
11421163
}
11431164
if doIndent {
11441165
// *s += "\n"
@@ -1152,6 +1173,10 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
11521173
elemlist := make([][2]interface{}, len(vv))
11531174
n = 0
11541175
for k, v := range vv {
1176+
if k == "#text" {
1177+
// simple element handled above
1178+
continue
1179+
}
11551180
if lenAttrPrefix > 0 && lenAttrPrefix < len(k) && k[:lenAttrPrefix] == attrPrefix {
11561181
continue
11571182
}

0 commit comments

Comments
 (0)