utils/types/yaml.go

46 lines
758 B
Go
Raw Normal View History

2025-07-16 11:40:25 -05:00
package types
import "gopkg.in/yaml.v3"
type YamlTypeError struct {
Node *yaml.Node
}
func (ye *YamlTypeError) Error() string {
return ye.Node.Tag + " is not a valid type for this field"
}
type IntOrString struct {
intVal int
strVal string
isInt bool
}
func (is IntOrString) MarshalYAML() (any, error) {
if is.isInt {
return is.intVal, nil
}
return is.strVal, nil
}
func (is *IntOrString) UnmarshalYAML(value *yaml.Node) error {
if value.Tag == `!!int` {
is.isInt = true
return value.Decode(&is.intVal)
}
if value.Tag == `!!str` {
is.isInt = false
return value.Decode(&is.strVal)
}
return &YamlTypeError{Node: value}
}
func (is IntOrString) IsZero() bool {
if is.isInt {
return is.intVal == 0
}
return is.strVal == ""
}