From c46fe1bdc3f6214d7c5a9ce5ec5f33576bce5d68 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 27 Oct 2021 19:03:42 +0200 Subject: [PATCH] fix: *OptionalInteger with omitempty This removes null values from the config --- profile.go | 1 - swarm.go | 18 +++++++++--------- types.go | 8 ++++---- types_test.go | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/profile.go b/profile.go index 47b1505..1d379d9 100644 --- a/profile.go +++ b/profile.go @@ -179,7 +179,6 @@ fetching may be degraded. c.Swarm.ConnMgr.LowWater = 20 c.Swarm.ConnMgr.HighWater = 40 c.Swarm.ConnMgr.GracePeriod = time.Minute.String() - c.Swarm.RelayService.Enabled = False return nil }, }, diff --git a/swarm.go b/swarm.go index 193be90..8c0bced 100644 --- a/swarm.go +++ b/swarm.go @@ -40,27 +40,27 @@ type SwarmConfig struct { // For every field a reasonable default will be defined in go-ipfs. type RelayService struct { // Enables the limited relay (circuit v2 relay). - Enabled Flag + Enabled *Flag `json:",omitempty"` // Limit is the (optional) relayed connection limits. - Limit RelayLimit + Limit *RelayLimit `json:",omitempty"` // ReservationTTL is the duration of a new (or refreshed reservation). ReservationTTL *OptionalDuration `json:",omitempty"` // MaxReservations is the maximum number of active relay slots. - MaxReservations OptionalInteger + MaxReservations *OptionalInteger `json:",omitempty"` // MaxCircuits is the maximum number of open relay connections for each peer; defaults to 16. - MaxCircuits OptionalInteger + MaxCircuits *OptionalInteger `json:",omitempty"` // BufferSize is the size of the relayed connection buffers. - BufferSize OptionalInteger + BufferSize *OptionalInteger `json:",omitempty"` // MaxReservationsPerPeer is the maximum number of reservations originating from the same peer. - MaxReservationsPerPeer OptionalInteger + MaxReservationsPerPeer *OptionalInteger `json:",omitempty"` // MaxReservationsPerIP is the maximum number of reservations originating from the same IP address. - MaxReservationsPerIP OptionalInteger + MaxReservationsPerIP *OptionalInteger `json:",omitempty"` // MaxReservationsPerASN is the maximum number of reservations origination from the same ASN. - MaxReservationsPerASN OptionalInteger + MaxReservationsPerASN *OptionalInteger `json:",omitempty"` } // RelayLimit are the per relayed connection resource limits. @@ -68,7 +68,7 @@ type RelayLimit struct { // Duration is the time limit before resetting a relayed connection. Duration *OptionalDuration `json:",omitempty"` // Data is the limit of data relayed (on each direction) before resetting the connection. - Data OptionalInteger + Data *OptionalInteger `json:",omitempty"` } type Transports struct { diff --git a/types.go b/types.go index ac90fa9..14b82d7 100644 --- a/types.go +++ b/types.go @@ -270,16 +270,16 @@ type OptionalInteger struct { } // WithDefault resolves the integer with the given default. -func (p OptionalInteger) WithDefault(defaultValue int64) (value int64) { - if p.value == nil { +func (p *OptionalInteger) WithDefault(defaultValue int64) (value int64) { + if p == nil || p.value == nil { return defaultValue } return *p.value } // IsDefault returns if this is a default optional integer -func (p OptionalInteger) IsDefault() bool { - return p.value == nil +func (p *OptionalInteger) IsDefault() bool { + return p == nil || p.value == nil } func (p OptionalInteger) MarshalJSON() ([]byte, error) { diff --git a/types_test.go b/types_test.go index 06ea73a..c188e7d 100644 --- a/types_test.go +++ b/types_test.go @@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) { } } + // marshal with omitempty type Foo struct { I *OptionalInteger `json:",omitempty"` } @@ -386,6 +387,20 @@ func TestOptionalInteger(t *testing.T) { if string(out) != expected { t.Fatal("expected omitempty to omit the optional integer") } + + // unmarshal from omitempty output and get default value + var foo2 Foo + if err := json.Unmarshal(out, &foo2); err != nil { + t.Fatalf("%s failed to unmarshall with %s", string(out), err) + } + if i := foo2.I.WithDefault(42); i != 42 { + t.Fatalf("expected default value to be used, got %d", i) + } + if !foo2.I.IsDefault() { + t.Fatal("expected value to be the default") + } + + // test invalid values for _, invalid := range []string{ "foo", "-1.1", "1.1", "0.0", "[]", } {