Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use encoding/csv to when parsing selector record #4515

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 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,12 @@ 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))
cr.LazyQuotes = true // Enable LazyQuotes to allow for uneven number of quotes
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
33 changes: 32 additions & 1 deletion internal/db/profile_selector_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"github.com/stretchr/testify/assert"
)

// TestScan tests the Scan method of the ProfileSelector struct
// for more tests that exercise the retrieval of a profile with selectors that also happens to use Scan
// see TestProfileListWithSelectors
func TestScan(t *testing.T) {
t.Parallel()

Expand All @@ -35,7 +38,7 @@ func TestScan(t *testing.T) {
}{
{
name: "Valid input with all fields",
input: []byte(fmt.Sprintf("(%s,%s,repository,\"entity.name == \"\"test/test\"\" && repository.is_fork != true\",\"comment1\")", selectorId, profileId)),
input: []byte(fmt.Sprintf(`(%s,%s,repository,"entity.name == ""test/test"" && repository.is_fork != true","comment1")`, selectorId, profileId)),
expected: ProfileSelector{
ID: selectorId,
ProfileID: profileId,
Expand All @@ -47,6 +50,34 @@ 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",
},
},
{
name: "Comment includes uneven quotes",
input: []byte(fmt.Sprintf(`(%s,%s,repository,"repository.name == foo",""comment1")`, selectorId, profileId)),
expected: ProfileSelector{
ID: selectorId,
ProfileID: profileId,
Entity: NullEntities{
Valid: true,
Entities: EntitiesRepository,
},
Selector: "repository.name == foo",
Comment: "comment1",
},
},
}

for _, tc := range tc {
Expand Down