Skip to content

Commit a9fefb2

Browse files
committed
method sortByName of type stringerList
+ more use of type stringerList[...] and its method 'push'
1 parent dda8c8b commit a9fefb2

File tree

9 files changed

+36
-62
lines changed

9 files changed

+36
-62
lines changed

go/pkg/pass1/check-unstable-nat-rules.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package pass1
22

3-
import ()
4-
53
func (c *spoc) checkUnstableNatRules() {
64
c.progress("Checking rules for unstable subnet relation")
75
process := func(l ruleList) {
@@ -11,7 +9,7 @@ func (c *spoc) checkUnstableNatRules() {
119
for _, obj := range l {
1210
if n, ok := obj.(*network); ok {
1311
if n.unstableNat != nil {
14-
result = append(result, n)
12+
result.push(n)
1513
}
1614
}
1715
}

go/pkg/pass1/collect-routers-and-networks.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
package pass1
22

3-
import (
4-
"slices"
5-
"strings"
6-
)
7-
83
func (c *spoc) collectRoutersAndNetworks() {
9-
rl := c.allRouters
10-
slices.SortFunc(rl, func(a, b *router) int {
11-
return strings.Compare(a.name, b.name)
12-
})
13-
for _, r := range rl {
4+
c.allRouters.sortByName()
5+
for _, r := range c.allRouters {
146
if r.managed != "" || r.routingOnly {
15-
c.managedRouters = append(c.managedRouters, r)
7+
c.managedRouters.push(r)
168
}
179
}
18-
c.allRouters = rl
1910

2011
// Collect vrf instances belonging to one device.
2112
// Also collect all IPv4 and IPv6 routers with same name.

go/pkg/pass1/nat.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,9 @@ func (c *spoc) errMissinNatOutside(
555555
}
556556
}
557557
}
558-
sortByName := func(l intfList) intfList {
559-
slices.SortFunc(l, func(a, b *routerIntf) int {
560-
return strings.Compare(a.name, b.name)
561-
})
562-
return l
563-
}
564-
natIntf = slices.Compact(sortByName(natIntf))
558+
natIntf = slices.Compact(natIntf.sortByName())
565559
natInfos := make(stringList, len(natIntf))
566-
missingIntf = slices.Compact(sortByName(missingIntf))
560+
missingIntf = slices.Compact(missingIntf.sortByName())
567561
for i, intf := range natIntf {
568562
natInfos[i] = intf.name
569563
if isNatIncoming(tag, intf) {

go/pkg/pass1/print-code.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,14 +1477,14 @@ func (c *spoc) printCrypto(fh *os.File, r *router) {
14771477
cryptoType := r.model.crypto
14781478

14791479
// List of ipsec definitions used at current router.
1480-
var ipsecList []*ipsec
1480+
var ipsecList stringerList[ipsec]
14811481
seenIpsec := make(map[*ipsec]bool)
14821482
for _, intf := range r.interfaces {
14831483
if intf.ipType == tunnelIP {
14841484
s := intf.getCrypto().ipsec
14851485
if !seenIpsec[s] {
14861486
seenIpsec[s] = true
1487-
ipsecList = append(ipsecList, s)
1487+
ipsecList.push(s)
14881488
}
14891489
}
14901490
}
@@ -1495,12 +1495,9 @@ func (c *spoc) printCrypto(fh *os.File, r *router) {
14951495
}
14961496

14971497
// Sort entries by name to get deterministic output.
1498-
slices.SortFunc(ipsecList, func(a, b *ipsec) int {
1499-
return strings.Compare(a.name, b.name)
1500-
})
1498+
ipsecList = ipsecList.sortByName()
15011499

15021500
// List of isakmp definitions used at current router.
1503-
// Sort entries by name to get deterministic output.
15041501
var isakmpList []*isakmp
15051502
seenIsakmp := make(map[*isakmp]bool)
15061503
for _, i := range ipsecList {

go/pkg/pass1/routes.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package pass1
33
import (
44
"fmt"
55
"slices"
6-
"strings"
76
)
87

98
/*
@@ -872,20 +871,8 @@ func (c *spoc) checkDuplicateRoutes(r *router) {
872871
net2intf[n] = intf
873872
}
874873

875-
// Sort for finding duplicates and for deterministic error messages.
876-
slices.SortFunc(hopList, func(a, b *routerIntf) int {
877-
return strings.Compare(a.name, b.name)
878-
})
879-
j := 0
880-
var prev *routerIntf
881-
for _, hop := range hopList {
882-
if hop != prev {
883-
hopList[j] = hop
884-
j++
885-
prev = hop
886-
}
887-
}
888-
hopList = hopList[:j]
874+
// Sort for deterministic error messages.
875+
hopList = slices.Compact(hopList.sortByName())
889876

890877
// Simple case: one hop
891878
if len(hopList) == 1 {

go/pkg/pass1/set-zone.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ attribute needProtect.
319319
Parameter : Map with crosslinked routers having attribute needProtect set.
320320
*/
321321
func clusterCrosslinkRouters(crosslinkRouters map[*router]bool) {
322-
var cluster []*router
322+
var cluster stringerList[router]
323323
seen := make(map[*router]bool)
324324

325325
// Add routers to cluster via depth first search.
@@ -352,10 +352,7 @@ func clusterCrosslinkRouters(crosslinkRouters map[*router]bool) {
352352
// Fill router cluster
353353
cluster = nil
354354
walk(r)
355-
356-
slices.SortFunc(cluster, func(a, b *router) int {
357-
return strings.Compare(a.name, b.name)
358-
})
355+
cluster.sortByName()
359356

360357
// Collect all interfaces belonging to needProtect routers of cluster...
361358
var crosslinkIntfs intfList
@@ -387,9 +384,7 @@ type bLookup map[*routerIntf]borderType
387384
// setAreas sets up areas, assure proper border definitions.
388385
func (c *spoc) setAreas() map[pathObj]map[*area]bool {
389386
objInArea := make(map[pathObj]map[*area]bool)
390-
slices.SortFunc(c.ascendingAreas, func(a, b *area) int {
391-
return cmp.Compare(a.name, b.name)
392-
})
387+
c.ascendingAreas.sortByName()
393388
for _, a := range c.ascendingAreas {
394389
if n := a.anchor; n != nil {
395390
c.setArea(n.zone, a, nil, nil, objInArea)

go/pkg/pass1/setup-objects.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (c *spoc) setupRouter46(a *ast.Router) {
351351
r.ipV6 = false
352352
stripFilterOnly(r, r6.interfaces)
353353
c.setupRouter2(r)
354-
c.allRouters = append(c.allRouters, r)
354+
c.allRouters.push(r)
355355
}
356356
if r6.interfaces != nil {
357357
if r.interfaces == nil {
@@ -361,7 +361,7 @@ func (c *spoc) setupRouter46(a *ast.Router) {
361361
r6.ipV6 = true
362362
stripFilterOnly(r6, r.interfaces)
363363
c.setupRouter2(r6)
364-
c.allRouters = append(c.allRouters, r6)
364+
c.allRouters.push(r6)
365365
}
366366
}
367367

@@ -1331,7 +1331,7 @@ func (c *spoc) setupArea(v *ast.Area) {
13311331
}
13321332
l := groupObjList{n}
13331333
if n2 := n.combined46; n2 != nil {
1334-
l = append(l, n2)
1334+
l.push(n2)
13351335
}
13361336
if len(l) >= 1 {
13371337
n := l[0].(*network)
@@ -1364,7 +1364,7 @@ func (c *spoc) setupArea(v *ast.Area) {
13641364
}
13651365
}
13661366
}
1367-
c.ascendingAreas = append(c.ascendingAreas, ar)
1367+
c.ascendingAreas.push(ar)
13681368
if p := ar.routerAttributes.policyDistributionPoint; p != nil {
13691369
if p.ipV6 != ar.ipV6 {
13701370
if ar2 == nil {
@@ -1386,7 +1386,7 @@ func (c *spoc) setupArea(v *ast.Area) {
13861386
// Attribute 'nat' is only applied to IPv4 part in
13871387
// combined v4/v6 area.
13881388
ar2.nat = nil
1389-
c.ascendingAreas = append(c.ascendingAreas, ar2)
1389+
c.ascendingAreas.push(ar2)
13901390
}
13911391
}
13921392

@@ -3219,7 +3219,7 @@ func (c *spoc) tryNetworkRefList(a *ast.Attribute, ctx string) netList {
32193219
if len(name) == len(v) {
32203220
c.err("Expected type 'network:' in %s", ctx2)
32213221
} else if n, found := c.symTable.network[name]; found {
3222-
result = append(result, n)
3222+
result.push(n)
32233223
} else {
32243224
c.warn("Ignoring undefined network:%s in %s", name, ctx2)
32253225
}
@@ -3946,7 +3946,7 @@ func (c *spoc) moveLockedIntf(intf *routerIntf) {
39463946
cp.origRouter = orig
39473947
cp.interfaces = intfList{intf}
39483948
intf.router = &cp
3949-
c.allRouters = append(c.allRouters, &cp)
3949+
c.allRouters.push(&cp)
39503950

39513951
// Don't check fragment for reachability.
39523952
cp.policyDistributionPoint = nil

go/pkg/pass1/spoc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ type spoc struct {
3232
symTable *symbolTable
3333
userObj userInfo
3434
allNetworks netList
35-
allRouters []*router
36-
managedRouters []*router
35+
allRouters stringerList[router]
36+
managedRouters stringerList[router]
3737
allPathRules pathRules
3838
allZones []*zone
3939
ascendingServices []*service
40-
ascendingAreas []*area
40+
ascendingAreas stringerList[area]
4141
pathrestrictions []*pathRestriction
4242
prt *stdProto
4343
network00 *network

go/pkg/pass1/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package pass1
33
import (
44
"fmt"
55
"net/netip"
6+
"slices"
7+
"strings"
68

79
"github.com/hknutzen/Netspoc/go/pkg/ast"
810

@@ -15,6 +17,13 @@ func (a *stringerList[E]) push(e *E) {
1517
*a = append(*a, e)
1618
}
1719

20+
func (l stringerList[E]) sortByName() stringerList[E] {
21+
slices.SortFunc(l, func(a, b *E) int {
22+
return strings.Compare((*a).String(), (*b).String())
23+
})
24+
return l
25+
}
26+
1827
type intfList = stringerList[routerIntf]
1928

2029
type stringList []string
@@ -491,6 +500,9 @@ type ipsec struct {
491500
espEncryption string
492501
pfsGroup string
493502
}
503+
504+
func (x ipsec) String() string { return x.name }
505+
494506
type isakmp struct {
495507
name string
496508
authentication string

0 commit comments

Comments
 (0)