Skip to content

Commit

Permalink
badoption: Finish netip options
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 1, 2024
1 parent 3f30aaf commit 478265c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions common/json/badoption/netip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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() {
Expand Down

0 comments on commit 478265c

Please sign in to comment.