This commit is contained in:
Simon Martens
2025-03-05 16:41:39 +01:00
commit e19fd47c17
88 changed files with 9765 additions and 0 deletions

49
xml/optionalbool.go Normal file
View File

@@ -0,0 +1,49 @@
package xmlparsing
import (
"encoding/xml"
"strings"
)
type OptionalBool int
const (
Unspecified OptionalBool = iota
True
False
)
func (b *OptionalBool) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var attr struct {
Value string `xml:"value,attr"`
}
if err := d.DecodeElement(&attr, &start); err != nil {
return err
}
switch strings.ToLower(attr.Value) {
case "true":
*b = True
case "false":
*b = False
default:
*b = Unspecified
}
return nil
}
func (b OptionalBool) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if b == Unspecified {
return nil
}
value := "false"
if b == True {
value = "true"
}
type alias struct {
Value string `xml:"value,attr"`
}
return e.EncodeElement(alias{Value: value}, start)
}