add initial address definition

This commit is contained in:
Samuel Lorch 2023-03-01 18:17:42 +01:00
parent 327062958b
commit 9fe13b3589

View file

@ -0,0 +1,52 @@
package definitions
import (
"encoding/json"
"net/netip"
"go4.org/netipx"
)
type Address struct {
Type AddressType `json:"type"`
Comment string `json:"comment,omitempty"`
Host *netip.Addr `json:"host,omitempty"`
Range *netipx.IPRange `json:"range,omitempty"`
Children *[]string `json:"children,omitempty"`
}
type AddressType int
const (
Host AddressType = iota
Range
Network
Group
)
func (t AddressType) String() string {
return [...]string{"host", "range", "network", "group"}[t]
}
func (t *AddressType) FromString(input string) AddressType {
return map[string]AddressType{
"host": Host,
"range": Range,
"network": Network,
"group": Group,
}[input]
}
func (t AddressType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *AddressType) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*t = t.FromString(s)
return nil
}