Skip to content

Commit 49615d6

Browse files
committed
Refactor code to use closure function
1 parent ba8f9dd commit 49615d6

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

internal/controller/state/graph/route_common.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,23 @@ func buildSectionNameRefs(
277277
routeNamespace string,
278278
gws map[types.NamespacedName]*Gateway,
279279
) ([]ParentRef, error) {
280-
sectionNameRefs := make([]ParentRef, 0, len(parentRefs))
281-
282280
type key struct {
283281
gwNsName types.NamespacedName
284282
sectionName string
285283
}
286284
uniqueSectionsPerGateway := make(map[key]struct{})
287285

286+
sectionNameRefs := make([]ParentRef, 0, len(parentRefs))
287+
288+
checkUniqueSections := func(key key) error {
289+
if _, exist := uniqueSectionsPerGateway[key]; exist {
290+
return fmt.Errorf("duplicate section name %q for Gateway %s", key.sectionName, key.gwNsName.String())
291+
}
292+
293+
uniqueSectionsPerGateway[key] = struct{}{}
294+
return nil
295+
}
296+
288297
parentRefIndex := 0
289298

290299
for _, p := range parentRefs {
@@ -301,13 +310,11 @@ func buildSectionNameRefs(
301310
// If there is no section name, we create ParentRefs for each listener in the gateway
302311
if p.SectionName == nil {
303312
for _, l := range gw.Listeners {
304-
sectionName := string(l.Source.Name)
305-
k.sectionName = sectionName
313+
k.sectionName = string(l.Source.Name)
306314

307-
if _, exist := uniqueSectionsPerGateway[k]; exist {
308-
return nil, fmt.Errorf("duplicate section name %q for Gateway %s", sectionName, gwNsName.String())
315+
if err := checkUniqueSections(k); err != nil {
316+
return nil, err
309317
}
310-
uniqueSectionsPerGateway[k] = struct{}{}
311318

312319
sectionNameRefs = append(sectionNameRefs, ParentRef{
313320
Idx: parentRefIndex,
@@ -320,14 +327,10 @@ func buildSectionNameRefs(
320327
continue
321328
}
322329

323-
sectionName := string(*p.SectionName)
324-
325-
k.sectionName = sectionName
326-
327-
if _, exist := uniqueSectionsPerGateway[k]; exist {
328-
return nil, fmt.Errorf("duplicate section name %q for Gateway %s", sectionName, gwNsName.String())
330+
k.sectionName = string(*p.SectionName)
331+
if err := checkUniqueSections(k); err != nil {
332+
return nil, err
329333
}
330-
uniqueSectionsPerGateway[k] = struct{}{}
331334

332335
sectionNameRefs = append(sectionNameRefs, ParentRef{
333336
Idx: parentRefIndex,

0 commit comments

Comments
 (0)