mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
add initial address definition
This commit is contained in:
parent
327062958b
commit
9fe13b3589
1 changed files with 52 additions and 0 deletions
52
pkg/definitions/address.go
Normal file
52
pkg/definitions/address.go
Normal 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
|
||||
}
|
Loading…
Add table
Reference in a new issue