Skip to content

Commit

Permalink
Omit unset fields of Target when serializing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer9-fb authored and llogen committed Aug 4, 2021
1 parent 0fa1ae4 commit c7ae247
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,27 @@ type ErrPayload struct {
// FQDN, PrimaryIPv4, and PrimaryIPv6 are used by plugins to contact the target, set as many as possible for maximum plugin compatibility.
// Plugins are generally expected to attempt contacting devices via FQDN, IPv4, and IPv6. Note there is no way to enforce this and more specialized plugins might only support a subset.
type Target struct {
ID string
FQDN string
PrimaryIPv4, PrimaryIPv6 net.IP
ID string `json:"ID"`
FQDN string `json:"FQDN,omitempty"`
PrimaryIPv4 net.IP `json:"PrimaryIPv4,omitempty"`
PrimaryIPv6 net.IP `json:"PrimaryIPv6,omitempty"`
}

func (t *Target) String() string {
if t == nil {
return "(*Target)(nil)"
}
var res strings.Builder
res.WriteString(fmt.Sprintf("Target{ID: \"%s\"", t.ID))
res.WriteString(fmt.Sprintf(`Target{ID: "%s"`, t.ID))
// deref params if they are set, to be more useful than %v
if t.FQDN != "" {
res.WriteString(fmt.Sprintf(", FQDN: \"%s\"", t.FQDN))
res.WriteString(fmt.Sprintf(`, FQDN: "%s"`, t.FQDN))
}
if t.PrimaryIPv4 != nil {
res.WriteString(fmt.Sprintf(", PrimaryIPv4: \"%v\"", t.PrimaryIPv4))
res.WriteString(fmt.Sprintf(`, PrimaryIPv4: "%v"`, t.PrimaryIPv4))
}
if t.PrimaryIPv6 != nil {
res.WriteString(fmt.Sprintf(", PrimaryIPv6: \"%v\"", t.PrimaryIPv6))
res.WriteString(fmt.Sprintf(`, PrimaryIPv6: "%v"`, t.PrimaryIPv6))
}
res.WriteString("}")
return res.String()
Expand Down
27 changes: 27 additions & 0 deletions pkg/target/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package target

import (
"encoding/json"
"net"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -21,3 +23,28 @@ func TestNilTarget(t *testing.T) {
}()
require.Nil(t, recoverResult)
}

func TestTargetStringification(t *testing.T) {
var t0 *Target
require.Equal(t, `(*Target)(nil)`, t0.String())

t1 := &Target{ID: "123"}
require.Equal(t, `Target{ID: "123"}`, t1.String())
tj1, _ := json.Marshal(t1)
require.Equal(t, `{"ID":"123"}`, string(tj1))

t2 := &Target{FQDN: "example.com"}
require.Equal(t, `Target{ID: "", FQDN: "example.com"}`, t2.String())
tj2, _ := json.Marshal(t2)
require.Equal(t, `{"ID":"","FQDN":"example.com"}`, string(tj2))

t3 := &Target{ID: "123", FQDN: "example.com", PrimaryIPv4: net.IPv4(1, 2, 3, 4)}
require.Equal(t, `Target{ID: "123", FQDN: "example.com", PrimaryIPv4: "1.2.3.4"}`, t3.String())
tj3, _ := json.Marshal(t3)
require.Equal(t, `{"ID":"123","FQDN":"example.com","PrimaryIPv4":"1.2.3.4"}`, string(tj3))

t4 := &Target{ID: "123", FQDN: "example.com", PrimaryIPv6: net.IPv6loopback}
require.Equal(t, `Target{ID: "123", FQDN: "example.com", PrimaryIPv6: "::1"}`, t4.String())
tj4, _ := json.Marshal(t4)
require.Equal(t, `{"ID":"123","FQDN":"example.com","PrimaryIPv6":"::1"}`, string(tj4))
}

0 comments on commit c7ae247

Please sign in to comment.