Skip to content

Commit

Permalink
.toProto() methods should return nil values for empty slices
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Wilhelm <[email protected]>
  • Loading branch information
Maximilian Wilhelm committed Oct 26, 2023
1 parent 7f5df41 commit f8ba7c9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 139 deletions.
113 changes: 22 additions & 91 deletions pkg/connector/netbox/netbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,11 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: []*octopuspb.Device{
{
Name: "ccr01.dus01",
Role: "ccr",
SiteName: "DUS01",
Interfaces: []*octopuspb.Interface{},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
Name: "ccr01.dus01",
Role: "ccr",
SiteName: "DUS01",
},
},
},
Expand Down Expand Up @@ -256,28 +249,21 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: []*octopuspb.Device{
{
Name: "GCP",
Role: "cloud-provider",
SiteName: "ANY",
Interfaces: []*octopuspb.Interface{
{
Name: "Interconnect0",
Type: "10gbase-x-sfpp",
Units: []*octopuspb.InterfaceUnit{},
Name: "Interconnect0",
Type: "10gbase-x-sfpp",
MetaData: &octopuspb.MetaData{
SemanticTags: map[string]string{},
Tags: []string{"net:foo"},
},
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
MetaData: &octopuspb.MetaData{
SemanticTags: map[string]string{
"NET:ASN": "16550",
Expand All @@ -295,33 +281,26 @@ func TestEnrichment(t *testing.T) {
Type: "10gbase-x-sfpp",
Units: []*octopuspb.InterfaceUnit{
{
Id: 100,
InnerTag: 100,
Ipv4Addresses: []*octopuspb.IPAddress{},
Ipv6Addresses: []*octopuspb.IPAddress{},
Id: 100,
InnerTag: 100,
},
},
},
{
Name: "Ethernet0/1",
Type: "10gbase-x-sfpp",
LagMemberOf: "bond0",
Units: []*octopuspb.InterfaceUnit{},
},
{
Name: "Ethernet0/2",
Type: "10gbase-x-sfpp",
LagMemberOf: "bond0",
Units: []*octopuspb.InterfaceUnit{},
},
{
Name: "bond0",
Type: "lag",
Units: []*octopuspb.InterfaceUnit{},
Name: "bond0",
Type: "lag",
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
},
},
Expand Down Expand Up @@ -452,10 +431,6 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: []*octopuspb.Device{
{
Name: "GCP",
Expand All @@ -473,7 +448,6 @@ func TestEnrichment(t *testing.T) {
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 0), 31).ToProto(),
},
},
Ipv6Addresses: []*octopuspb.IPAddress{},
},
},
MetaData: &octopuspb.MetaData{
Expand All @@ -482,8 +456,6 @@ func TestEnrichment(t *testing.T) {
},
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
{
Name: "ccr01.dus01",
Expand All @@ -501,36 +473,28 @@ func TestEnrichment(t *testing.T) {
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 1), 31).ToProto(),
},
},
Ipv6Addresses: []*octopuspb.IPAddress{},
},
{
Id: 100,
InnerTag: 100,
Ipv4Addresses: []*octopuspb.IPAddress{},
Ipv6Addresses: []*octopuspb.IPAddress{},
Id: 100,
InnerTag: 100,
},
},
},
{
Name: "Ethernet0/1",
Type: "10gbase-x-sfpp",
LagMemberOf: "bond0",
Units: []*octopuspb.InterfaceUnit{},
},
{
Name: "Ethernet0/2",
Type: "10gbase-x-sfpp",
LagMemberOf: "bond0",
Units: []*octopuspb.InterfaceUnit{},
},
{
Name: "bond0",
Type: "lag",
Units: []*octopuspb.InterfaceUnit{},
Name: "bond0",
Type: "lag",
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
},
},
Expand Down Expand Up @@ -652,40 +616,30 @@ func TestEnrichment(t *testing.T) {
Name: "SiteA",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Devices: []*octopuspb.Device{
{
Name: "devA",
SiteName: "SiteA",
Interfaces: []*octopuspb.Interface{
{
Name: "ifaA",
Units: make([]*octopuspb.InterfaceUnit, 0),
Name: "ifaA",
},
{
Name: "ifaX",
Units: make([]*octopuspb.InterfaceUnit, 0),
Name: "ifaX",
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
{
Name: "devB",
SiteName: "SiteA",
Interfaces: []*octopuspb.Interface{
{
Name: "ifaB",
Units: make([]*octopuspb.InterfaceUnit, 0),
Name: "ifaB",
},
{
Name: "ifaY",
Units: make([]*octopuspb.InterfaceUnit, 0),
Name: "ifaY",
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
},
Cables: []*octopuspb.Cable{
Expand Down Expand Up @@ -796,10 +750,6 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: []*octopuspb.Device{
{
Name: "ccr01.dus01",
Expand Down Expand Up @@ -827,13 +777,10 @@ func TestEnrichment(t *testing.T) {
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 2), 32).ToProto(),
},
},
Ipv6Addresses: []*octopuspb.IPAddress{},
},
},
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
},
},
Expand Down Expand Up @@ -886,16 +833,11 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: []*octopuspb.Device{
{
Name: "pp01.dus01",
Role: "pp",
SiteName: "DUS01",
Interfaces: []*octopuspb.Interface{},
Name: "pp01.dus01",
Role: "pp",
SiteName: "DUS01",
FrontPorts: []*octopuspb.FrontPort{
{
Name: "FP-A",
Expand Down Expand Up @@ -976,10 +918,7 @@ func TestEnrichment(t *testing.T) {
Name: "DUS01",
},
},
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),

Devices: []*octopuspb.Device{
{
Name: "ccr01.dus01",
Expand All @@ -998,13 +937,10 @@ func TestEnrichment(t *testing.T) {
IP: bnet.NewPfx(bnet.IPv4FromOctets(169, 254, 0, 0), 31).Ptr().ToProto(),
},
},
Ipv6Addresses: make([]*octopuspb.IPAddress, 0),
},
},
},
},
FrontPorts: []*octopuspb.FrontPort{},
RearPorts: []*octopuspb.RearPort{},
},
},
},
Expand Down Expand Up @@ -1039,12 +975,7 @@ func TestEnrichment(t *testing.T) {
Prefixes: make(map[int64]*model.Prefix),
},
expected: &octopuspb.Topology{
Sites: make([]*octopuspb.Site, 0),
Pops: make([]*octopuspb.Pop, 0),
Colos: make([]*octopuspb.Colo, 0),
Cables: make([]*octopuspb.Cable, 0),
Circuits: make([]*octopuspb.Circuit, 0),
Devices: make([]*octopuspb.Device, 0),
Devices: make([]*octopuspb.Device, 0),
Prefixes: []*octopuspb.Prefix{
{
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(100, 64, 0, 0), 26).ToProto(),
Expand Down
27 changes: 18 additions & 9 deletions pkg/model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (d *Device) ToProto() *octopuspb.Device {
Platform: d.Platform,
DeviceType: d.DeviceType,

Interfaces: make([]*octopuspb.Interface, 0),
FrontPorts: make([]*octopuspb.FrontPort, 0),
RearPorts: make([]*octopuspb.RearPort, 0),
Interfaces: nil,
FrontPorts: nil,
RearPorts: nil,
MetaData: d.MetaData.ToProto(),
}

Expand All @@ -78,16 +78,25 @@ func (d *Device) ToProto() *octopuspb.Device {
protoDev.SiteName = d.Site.Name
}

for _, iface := range d.Interfaces {
protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto())
if len(d.Interfaces) > 0 {
protoDev.Interfaces = make([]*octopuspb.Interface, 0)
for _, iface := range d.Interfaces {
protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto())
}
}

for _, fp := range d.FrontPorts {
protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto())
if len(d.FrontPorts) > 0 {
protoDev.FrontPorts = make([]*octopuspb.FrontPort, 0)
for _, fp := range d.FrontPorts {
protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto())
}
}

for _, rp := range d.RearPorts {
protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto())
if len(d.RearPorts) > 0 {
protoDev.RearPorts = make([]*octopuspb.RearPort, 0)
for _, rp := range d.RearPorts {
protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto())
}
}

return protoDev
Expand Down
8 changes: 1 addition & 7 deletions pkg/model/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ func TestDeviceToProto(t *testing.T) {
name: "Empty device",
device: NewDevice("foo"),
protoDevice: &octopuspb.Device{
Name: "foo",
Interfaces: []*octopuspb.Interface{},
FrontPorts: make([]*octopuspb.FrontPort, 0),
RearPorts: make([]*octopuspb.RearPort, 0),
Name: "foo",
},
},
{
Expand All @@ -154,13 +151,10 @@ func TestDeviceToProto(t *testing.T) {
},
},
},
Ipv6Addresses: []*octopuspb.IPAddress{},
},
},
},
},
FrontPorts: make([]*octopuspb.FrontPort, 0),
RearPorts: make([]*octopuspb.RearPort, 0),
},
},
}
Expand Down
Loading

0 comments on commit f8ba7c9

Please sign in to comment.