badoption: Finish netip options

This commit is contained in:
世界 2024-12-01 14:33:23 +08:00
parent 3f30aaf25e
commit 478265cd45
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -35,6 +35,13 @@ func (a *Addr) UnmarshalJSON(content []byte) error {
type Prefix netip.Prefix
func (p *Prefix) Build(defaultPrefix netip.Prefix) netip.Prefix {
if p == nil {
return defaultPrefix
}
return netip.Prefix(*p)
}
func (p *Prefix) MarshalJSON() ([]byte, error) {
return json.Marshal(netip.Prefix(*p).String())
}
@ -55,6 +62,13 @@ func (p *Prefix) UnmarshalJSON(content []byte) error {
type Prefixable netip.Prefix
func (p *Prefixable) Build(defaultPrefix netip.Prefix) netip.Prefix {
if p == nil {
return defaultPrefix
}
return netip.Prefix(*p)
}
func (p *Prefixable) MarshalJSON() ([]byte, error) {
prefix := netip.Prefix(*p)
if prefix.Bits() == prefix.Addr().BitLen() {