Skip to content

Commit 7104b72

Browse files
authored
refactor: use slices.Contains for readability and consistency (#104)
Signed-off-by: cryo <[email protected]>
1 parent 9d70e1f commit 7104b72

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

src/semantic-router/pkg/config/config.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"slices"
78
"sync"
89

910
"gopkg.in/yaml.v3"
@@ -439,14 +440,7 @@ func (c *RouterConfig) IsModelAllowedForPIIType(modelName string, piiType string
439440
}
440441

441442
// If allow_by_default is false, only explicitly allowed PII types are permitted
442-
for _, allowedPII := range policy.PIITypes {
443-
if allowedPII == piiType {
444-
return true
445-
}
446-
}
447-
448-
// PII type not found in allowed list and allow_by_default is false
449-
return false
443+
return slices.Contains(policy.PIITypes, piiType)
450444
}
451445

452446
// IsModelAllowedForPIITypes checks if a model is allowed to process any of the given PII types
@@ -487,23 +481,17 @@ func (c *RouterConfig) GetEndpointsForModel(modelName string) []VLLMEndpoint {
487481

488482
// First, find all endpoints that can serve this model
489483
for _, endpoint := range c.VLLMEndpoints {
490-
for _, model := range endpoint.Models {
491-
if model == modelName {
492-
availableEndpoints = append(availableEndpoints, endpoint)
493-
break
494-
}
484+
if slices.Contains(endpoint.Models, modelName) {
485+
availableEndpoints = append(availableEndpoints, endpoint)
495486
}
496487
}
497488

498489
// Check if model has preferred endpoints configured
499490
if modelConfig, ok := c.ModelConfig[modelName]; ok && len(modelConfig.PreferredEndpoints) > 0 {
500491
var preferredEndpoints []VLLMEndpoint
501492
for _, endpoint := range availableEndpoints {
502-
for _, preferredName := range modelConfig.PreferredEndpoints {
503-
if endpoint.Name == preferredName {
504-
preferredEndpoints = append(preferredEndpoints, endpoint)
505-
break
506-
}
493+
if slices.Contains(modelConfig.PreferredEndpoints, endpoint.Name) {
494+
preferredEndpoints = append(preferredEndpoints, endpoint)
507495
}
508496
}
509497
if len(preferredEndpoints) > 0 {

src/semantic-router/pkg/utils/pii/policy.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pii
22

33
import (
44
"log"
5+
"slices"
56

67
"github.com/vllm-project/semantic-router/semantic-router/pkg/config"
78
)
@@ -59,14 +60,7 @@ func (pc *PolicyChecker) CheckPolicy(model string, detectedPII []string) (bool,
5960
}
6061

6162
// If allow_by_default is false, check if this PII type is explicitly allowed
62-
isAllowed := false
63-
for _, allowedPII := range policy.PIITypes {
64-
if allowedPII == piiType {
65-
isAllowed = true
66-
break
67-
}
68-
}
69-
63+
isAllowed := slices.Contains(policy.PIITypes, piiType)
7064
if !isAllowed {
7165
deniedPII = append(deniedPII, piiType)
7266
}

0 commit comments

Comments
 (0)