I want to parse an xml attribute as an iota enum type (int) in go.
Below you can see what I tried but this does not work because the address of enum variables can't be taken.
type EnumType int
const (
EnumUnknown EnumType = iota
EnumFoo
EnumBar
)
func (E *EnumType) UnmarshalXMLAttr(attr xml.Attr) error {
switch attr.Value {
case "foo":
E = &EnumFoo
case "bar":
E = &EnumBar
default:
E = &EnumUnknown
}
return nil
}
// Example of how the unmarshal could be called:
type Tag struct {
Attribute EnumType `xml:"attribute,attr"`
}
func main() {
tag := &Tag{}
xml.Unmarshal([]byte("<tag attribute=\"foo\"/>"), tag)
}
Any other way to make UnmarshalXMLAttr
work with int types?
Update: I know I can solve this by adding the UnmarshalXML
method to Tag but I want to avoid this if possible because I have a lot of different tags with a lot of different attributes but only a few custom typed attributes. So implementing UnmarshalXML
methods for each tag would not be preferred.