mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2025-10-29 09:15:33 +00:00
Lots of stuff
This commit is contained in:
57
xmlparsing/optionalbool.go
Normal file
57
xmlparsing/optionalbool.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package xmlparsing
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type OptionalBool int
|
||||
|
||||
const (
|
||||
Unspecified OptionalBool = iota
|
||||
True
|
||||
False
|
||||
)
|
||||
|
||||
func (b OptionalBool) IsTrue() bool {
|
||||
return b == True
|
||||
}
|
||||
|
||||
func (b OptionalBool) IsFalse() bool {
|
||||
return b == False || b == Unspecified
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user