Skip to content

Commit a66cd86

Browse files
Maximilian WilhelmBarbarossaTM
Maximilian Wilhelm
authored andcommitted
.toProto() methods should return nil values for empty slices
Signed-off-by: Maximilian Wilhelm <[email protected]>
1 parent 2445e30 commit a66cd86

File tree

6 files changed

+93
-144
lines changed

6 files changed

+93
-144
lines changed

pkg/connector/netbox/netbox_test.go

+22-91
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,11 @@ func TestEnrichment(t *testing.T) {
6868
Name: "DUS01",
6969
},
7070
},
71-
Pops: make([]*octopuspb.Pop, 0),
72-
Colos: make([]*octopuspb.Colo, 0),
73-
Cables: make([]*octopuspb.Cable, 0),
74-
Circuits: make([]*octopuspb.Circuit, 0),
7571
Devices: []*octopuspb.Device{
7672
{
77-
Name: "ccr01.dus01",
78-
Role: "ccr",
79-
SiteName: "DUS01",
80-
Interfaces: []*octopuspb.Interface{},
81-
FrontPorts: []*octopuspb.FrontPort{},
82-
RearPorts: []*octopuspb.RearPort{},
73+
Name: "ccr01.dus01",
74+
Role: "ccr",
75+
SiteName: "DUS01",
8376
},
8477
},
8578
},
@@ -256,28 +249,21 @@ func TestEnrichment(t *testing.T) {
256249
Name: "DUS01",
257250
},
258251
},
259-
Pops: make([]*octopuspb.Pop, 0),
260-
Colos: make([]*octopuspb.Colo, 0),
261-
Cables: make([]*octopuspb.Cable, 0),
262-
Circuits: make([]*octopuspb.Circuit, 0),
263252
Devices: []*octopuspb.Device{
264253
{
265254
Name: "GCP",
266255
Role: "cloud-provider",
267256
SiteName: "ANY",
268257
Interfaces: []*octopuspb.Interface{
269258
{
270-
Name: "Interconnect0",
271-
Type: "10gbase-x-sfpp",
272-
Units: []*octopuspb.InterfaceUnit{},
259+
Name: "Interconnect0",
260+
Type: "10gbase-x-sfpp",
273261
MetaData: &octopuspb.MetaData{
274262
SemanticTags: map[string]string{},
275263
Tags: []string{"net:foo"},
276264
},
277265
},
278266
},
279-
FrontPorts: []*octopuspb.FrontPort{},
280-
RearPorts: []*octopuspb.RearPort{},
281267
MetaData: &octopuspb.MetaData{
282268
SemanticTags: map[string]string{
283269
"NET:ASN": "16550",
@@ -295,33 +281,26 @@ func TestEnrichment(t *testing.T) {
295281
Type: "10gbase-x-sfpp",
296282
Units: []*octopuspb.InterfaceUnit{
297283
{
298-
Id: 100,
299-
InnerTag: 100,
300-
Ipv4Addresses: []*octopuspb.IPAddress{},
301-
Ipv6Addresses: []*octopuspb.IPAddress{},
284+
Id: 100,
285+
InnerTag: 100,
302286
},
303287
},
304288
},
305289
{
306290
Name: "Ethernet0/1",
307291
Type: "10gbase-x-sfpp",
308292
LagMemberOf: "bond0",
309-
Units: []*octopuspb.InterfaceUnit{},
310293
},
311294
{
312295
Name: "Ethernet0/2",
313296
Type: "10gbase-x-sfpp",
314297
LagMemberOf: "bond0",
315-
Units: []*octopuspb.InterfaceUnit{},
316298
},
317299
{
318-
Name: "bond0",
319-
Type: "lag",
320-
Units: []*octopuspb.InterfaceUnit{},
300+
Name: "bond0",
301+
Type: "lag",
321302
},
322303
},
323-
FrontPorts: []*octopuspb.FrontPort{},
324-
RearPorts: []*octopuspb.RearPort{},
325304
},
326305
},
327306
},
@@ -452,10 +431,6 @@ func TestEnrichment(t *testing.T) {
452431
Name: "DUS01",
453432
},
454433
},
455-
Pops: make([]*octopuspb.Pop, 0),
456-
Colos: make([]*octopuspb.Colo, 0),
457-
Cables: make([]*octopuspb.Cable, 0),
458-
Circuits: make([]*octopuspb.Circuit, 0),
459434
Devices: []*octopuspb.Device{
460435
{
461436
Name: "GCP",
@@ -473,7 +448,6 @@ func TestEnrichment(t *testing.T) {
473448
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 0), 31).ToProto(),
474449
},
475450
},
476-
Ipv6Addresses: []*octopuspb.IPAddress{},
477451
},
478452
},
479453
MetaData: &octopuspb.MetaData{
@@ -482,8 +456,6 @@ func TestEnrichment(t *testing.T) {
482456
},
483457
},
484458
},
485-
FrontPorts: []*octopuspb.FrontPort{},
486-
RearPorts: []*octopuspb.RearPort{},
487459
},
488460
{
489461
Name: "ccr01.dus01",
@@ -501,36 +473,28 @@ func TestEnrichment(t *testing.T) {
501473
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 1), 31).ToProto(),
502474
},
503475
},
504-
Ipv6Addresses: []*octopuspb.IPAddress{},
505476
},
506477
{
507-
Id: 100,
508-
InnerTag: 100,
509-
Ipv4Addresses: []*octopuspb.IPAddress{},
510-
Ipv6Addresses: []*octopuspb.IPAddress{},
478+
Id: 100,
479+
InnerTag: 100,
511480
},
512481
},
513482
},
514483
{
515484
Name: "Ethernet0/1",
516485
Type: "10gbase-x-sfpp",
517486
LagMemberOf: "bond0",
518-
Units: []*octopuspb.InterfaceUnit{},
519487
},
520488
{
521489
Name: "Ethernet0/2",
522490
Type: "10gbase-x-sfpp",
523491
LagMemberOf: "bond0",
524-
Units: []*octopuspb.InterfaceUnit{},
525492
},
526493
{
527-
Name: "bond0",
528-
Type: "lag",
529-
Units: []*octopuspb.InterfaceUnit{},
494+
Name: "bond0",
495+
Type: "lag",
530496
},
531497
},
532-
FrontPorts: []*octopuspb.FrontPort{},
533-
RearPorts: []*octopuspb.RearPort{},
534498
},
535499
},
536500
},
@@ -652,40 +616,30 @@ func TestEnrichment(t *testing.T) {
652616
Name: "SiteA",
653617
},
654618
},
655-
Pops: make([]*octopuspb.Pop, 0),
656-
Colos: make([]*octopuspb.Colo, 0),
657619
Devices: []*octopuspb.Device{
658620
{
659621
Name: "devA",
660622
SiteName: "SiteA",
661623
Interfaces: []*octopuspb.Interface{
662624
{
663-
Name: "ifaA",
664-
Units: make([]*octopuspb.InterfaceUnit, 0),
625+
Name: "ifaA",
665626
},
666627
{
667-
Name: "ifaX",
668-
Units: make([]*octopuspb.InterfaceUnit, 0),
628+
Name: "ifaX",
669629
},
670630
},
671-
FrontPorts: []*octopuspb.FrontPort{},
672-
RearPorts: []*octopuspb.RearPort{},
673631
},
674632
{
675633
Name: "devB",
676634
SiteName: "SiteA",
677635
Interfaces: []*octopuspb.Interface{
678636
{
679-
Name: "ifaB",
680-
Units: make([]*octopuspb.InterfaceUnit, 0),
637+
Name: "ifaB",
681638
},
682639
{
683-
Name: "ifaY",
684-
Units: make([]*octopuspb.InterfaceUnit, 0),
640+
Name: "ifaY",
685641
},
686642
},
687-
FrontPorts: []*octopuspb.FrontPort{},
688-
RearPorts: []*octopuspb.RearPort{},
689643
},
690644
},
691645
Cables: []*octopuspb.Cable{
@@ -796,10 +750,6 @@ func TestEnrichment(t *testing.T) {
796750
Name: "DUS01",
797751
},
798752
},
799-
Pops: make([]*octopuspb.Pop, 0),
800-
Colos: make([]*octopuspb.Colo, 0),
801-
Cables: make([]*octopuspb.Cable, 0),
802-
Circuits: make([]*octopuspb.Circuit, 0),
803753
Devices: []*octopuspb.Device{
804754
{
805755
Name: "ccr01.dus01",
@@ -827,13 +777,10 @@ func TestEnrichment(t *testing.T) {
827777
IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 2), 32).ToProto(),
828778
},
829779
},
830-
Ipv6Addresses: []*octopuspb.IPAddress{},
831780
},
832781
},
833782
},
834783
},
835-
FrontPorts: []*octopuspb.FrontPort{},
836-
RearPorts: []*octopuspb.RearPort{},
837784
},
838785
},
839786
},
@@ -886,16 +833,11 @@ func TestEnrichment(t *testing.T) {
886833
Name: "DUS01",
887834
},
888835
},
889-
Pops: make([]*octopuspb.Pop, 0),
890-
Colos: make([]*octopuspb.Colo, 0),
891-
Cables: make([]*octopuspb.Cable, 0),
892-
Circuits: make([]*octopuspb.Circuit, 0),
893836
Devices: []*octopuspb.Device{
894837
{
895-
Name: "pp01.dus01",
896-
Role: "pp",
897-
SiteName: "DUS01",
898-
Interfaces: []*octopuspb.Interface{},
838+
Name: "pp01.dus01",
839+
Role: "pp",
840+
SiteName: "DUS01",
899841
FrontPorts: []*octopuspb.FrontPort{
900842
{
901843
Name: "FP-A",
@@ -976,10 +918,7 @@ func TestEnrichment(t *testing.T) {
976918
Name: "DUS01",
977919
},
978920
},
979-
Pops: make([]*octopuspb.Pop, 0),
980-
Colos: make([]*octopuspb.Colo, 0),
981-
Cables: make([]*octopuspb.Cable, 0),
982-
Circuits: make([]*octopuspb.Circuit, 0),
921+
983922
Devices: []*octopuspb.Device{
984923
{
985924
Name: "ccr01.dus01",
@@ -998,13 +937,10 @@ func TestEnrichment(t *testing.T) {
998937
IP: bnet.NewPfx(bnet.IPv4FromOctets(169, 254, 0, 0), 31).Ptr().ToProto(),
999938
},
1000939
},
1001-
Ipv6Addresses: make([]*octopuspb.IPAddress, 0),
1002940
},
1003941
},
1004942
},
1005943
},
1006-
FrontPorts: []*octopuspb.FrontPort{},
1007-
RearPorts: []*octopuspb.RearPort{},
1008944
},
1009945
},
1010946
},
@@ -1039,12 +975,7 @@ func TestEnrichment(t *testing.T) {
1039975
Prefixes: make(map[int64]*model.Prefix),
1040976
},
1041977
expected: &octopuspb.Topology{
1042-
Sites: make([]*octopuspb.Site, 0),
1043-
Pops: make([]*octopuspb.Pop, 0),
1044-
Colos: make([]*octopuspb.Colo, 0),
1045-
Cables: make([]*octopuspb.Cable, 0),
1046-
Circuits: make([]*octopuspb.Circuit, 0),
1047-
Devices: make([]*octopuspb.Device, 0),
978+
Devices: make([]*octopuspb.Device, 0),
1048979
Prefixes: []*octopuspb.Prefix{
1049980
{
1050981
Prefix: bnet.NewPfx(bnet.IPv4FromOctets(100, 64, 0, 0), 26).ToProto(),

pkg/model/device.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ func (d *Device) ToProto() *octopuspb.Device {
6464
Platform: d.Platform,
6565
DeviceType: d.DeviceType,
6666

67-
Interfaces: make([]*octopuspb.Interface, 0),
68-
FrontPorts: make([]*octopuspb.FrontPort, 0),
69-
RearPorts: make([]*octopuspb.RearPort, 0),
70-
MetaData: d.MetaData.ToProto(),
67+
MetaData: d.MetaData.ToProto(),
7168
}
7269

7370
if d.Colo != nil {
@@ -78,16 +75,25 @@ func (d *Device) ToProto() *octopuspb.Device {
7875
protoDev.SiteName = d.Site.Name
7976
}
8077

81-
for _, iface := range d.Interfaces {
82-
protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto())
78+
if len(d.Interfaces) > 0 {
79+
protoDev.Interfaces = make([]*octopuspb.Interface, 0)
80+
for _, iface := range d.Interfaces {
81+
protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto())
82+
}
8383
}
8484

85-
for _, fp := range d.FrontPorts {
86-
protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto())
85+
if len(d.FrontPorts) > 0 {
86+
protoDev.FrontPorts = make([]*octopuspb.FrontPort, 0)
87+
for _, fp := range d.FrontPorts {
88+
protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto())
89+
}
8790
}
8891

89-
for _, rp := range d.RearPorts {
90-
protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto())
92+
if len(d.RearPorts) > 0 {
93+
protoDev.RearPorts = make([]*octopuspb.RearPort, 0)
94+
for _, rp := range d.RearPorts {
95+
protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto())
96+
}
9197
}
9298

9399
return protoDev

pkg/model/device_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ func TestDeviceToProto(t *testing.T) {
129129
name: "Empty device",
130130
device: NewDevice("foo"),
131131
protoDevice: &octopuspb.Device{
132-
Name: "foo",
133-
Interfaces: []*octopuspb.Interface{},
134-
FrontPorts: make([]*octopuspb.FrontPort, 0),
135-
RearPorts: make([]*octopuspb.RearPort, 0),
132+
Name: "foo",
136133
},
137134
},
138135
{
@@ -154,13 +151,10 @@ func TestDeviceToProto(t *testing.T) {
154151
},
155152
},
156153
},
157-
Ipv6Addresses: []*octopuspb.IPAddress{},
158154
},
159155
},
160156
},
161157
},
162-
FrontPorts: make([]*octopuspb.FrontPort, 0),
163-
RearPorts: make([]*octopuspb.RearPort, 0),
164158
},
165159
},
166160
}

0 commit comments

Comments
 (0)