Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
fix: *OptionalInteger with omitempty
Browse files Browse the repository at this point in the history
This removes null values from the config
  • Loading branch information
lidel committed Oct 27, 2021
1 parent f0f0f61 commit c46fe1b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
1 change: 0 additions & 1 deletion profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
Expand Down
18 changes: 9 additions & 9 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@ 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.
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 {
Expand Down
8 changes: 4 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func TestOptionalInteger(t *testing.T) {
}
}

// marshal with omitempty
type Foo struct {
I *OptionalInteger `json:",omitempty"`
}
Expand All @@ -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", "[]",
} {
Expand Down

0 comments on commit c46fe1b

Please sign in to comment.