Skip to content

Commit

Permalink
Use encoding/csv to when parsing selector record
Browse files Browse the repository at this point in the history
`encoding/csv` allows us to handle selectors with commas in them which
our naive parser embarassingly didn't such as:
```
repository.properties['github/primary_language'] in ['TypeScript', 'Go'
```

Fixes: #4514
  • Loading branch information
jhrozek committed Sep 17, 2024
1 parent 6f89a1d commit d6536ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/db/profile_selector_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package db

import (
"encoding/csv"
"fmt"
"strings"
)
Expand All @@ -37,7 +38,11 @@ func (s *ProfileSelector) Scan(value interface{}) error {
str = strings.TrimSuffix(str, ")")

// Split the string by commas to get the individual field values
parts := strings.Split(str, ",")
cr := csv.NewReader(strings.NewReader(str))
parts, err := cr.Read()
if err != nil {
return fmt.Errorf("failed to scan SelectorInfo: %v", err)
}

// Assign the values to the struct fields
if len(parts) != 5 {
Expand Down
14 changes: 14 additions & 0 deletions internal/db/profile_selector_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func TestScan(t *testing.T) {
Comment: "comment1",
},
},
{
name: "Valid input with commas in the selector",
input: []byte(fmt.Sprintf("(%s,%s,repository,\"repository.properties['github/primary_language'] in ['TypeScript', 'Go']\",\"comment1\")", selectorId, profileId)),
expected: ProfileSelector{
ID: selectorId,
ProfileID: profileId,
Entity: NullEntities{
Valid: true,
Entities: EntitiesRepository,
},
Selector: "repository.properties['github/primary_language'] in ['TypeScript', 'Go']",
Comment: "comment1",
},
},
}

for _, tc := range tc {
Expand Down

0 comments on commit d6536ac

Please sign in to comment.