Skip to content

Commit 35d1980

Browse files
committed
Get property inner string if value is missing (#73)
1 parent 90e899f commit 35d1980

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

tmx_property.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package tiled
2424

2525
import (
2626
"encoding/hex"
27+
"encoding/xml"
2728
"image/color"
2829
"strconv"
2930
)
@@ -34,14 +35,41 @@ type Properties []*Property
3435
// Property is used for custom properties
3536
type Property struct {
3637
// The name of the property.
37-
Name string `xml:"name,attr"`
38+
Name string
3839
// The type of the property. Can be string (default), int, float, bool, color or file (since 0.16, with color and file added in 0.17).
39-
Type string `xml:"type,attr"`
40+
Type string
4041
// The value of the property.
4142
// Boolean properties have a value of either "true" or "false".
4243
// Color properties are stored in the format #AARRGGBB.
4344
// File properties are stored as paths relative from the location of the map file.
44-
Value string `xml:"value,attr"`
45+
Value string
46+
}
47+
48+
// UnmarshalXML implements the xml.Unmarshaler interface for Property. Setting Value even if it's in the inner text.
49+
func (p *Property) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
50+
var valueFoundInAttr bool
51+
for _, attr := range start.Attr {
52+
switch attr.Name.Local {
53+
case "name":
54+
p.Name = attr.Value
55+
case "type":
56+
p.Type = attr.Value
57+
case "value":
58+
p.Value = attr.Value
59+
valueFoundInAttr = true
60+
}
61+
}
62+
if valueFoundInAttr {
63+
return d.Skip()
64+
}
65+
66+
var innerText string
67+
if err := d.DecodeElement(&innerText, &start); err != nil {
68+
return err
69+
}
70+
p.Value = innerText
71+
72+
return nil
4573
}
4674

4775
// Get finds all properties by specified name

0 commit comments

Comments
 (0)