package main import ( "github.com/goccy/go-yaml" "net" ) func validateCIDR(cidr string) error { _, _, err := net.ParseCIDR(cidr) return err } type CIDR string // UnmarshalYAML implements the yaml.Unmarshaler interface func (c *CIDR) UnmarshalYAML(value []byte) error { // Get the string value from the node var s string if err := yaml.Unmarshal(value, &s); err != nil { return err } if err := validateCIDR(s); err != nil { return err } *c = CIDR(s) return nil } // MarshalYAML implements the yaml.Marshaler interface func (c CIDR) MarshalYAML() ([]byte, error) { // Do any custom processing here before marshalling return []byte(string(c)), nil } // CIDRS represents each network entry in the YAML type CIDRS struct { Name string `yaml:"name"` CIDR CIDR `yaml:"cidr"` Except []CIDR `yaml:"except,omitempty"` } // Config represents the top-level YAML structure type Config struct { Networks []CIDRS `yaml:"networks"` }