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

Conversation

jhrozek
Copy link
Contributor

@jhrozek jhrozek commented Sep 17, 2024

Summary

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

Change Type

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

unit test plus using that selector

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

JAORMX
JAORMX previously approved these changes Sep 17, 2024
@coveralls
Copy link

coveralls commented Sep 17, 2024

Coverage Status

coverage: 52.821% (-0.01%) from 52.832%
when pulling 161cf63 on jhrozek:selector_csv
into 8a591d6 on stacklok:main.

internal/db/profile_selector_scan_test.go Outdated Show resolved Hide resolved
Comment on lines 41 to 46
cr := csv.NewReader(strings.NewReader(str))
parts, err := cr.Read()
if err != nil {
return fmt.Errorf("failed to scan SelectorInfo: %v", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update the reverse (store) code as well?

Do we have some unit tests to cover the round-trip as well as the serialized format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no serialization, really. The selectors are used as-is, but they are retrieved by aggregating the selectors table with the profiles table in

-- name: GetProfileByProjectAndName :many
which uses a SQL type defined at
CREATE TYPE profile_selector AS (
which we force through sqlc (
- db_type: profile_selector
)

The code I modified is a Scan() to convert the Go type for the selectors defined in models.go to the sql type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! I'm slightly nervous about whether the Postgres ARRAY_AGG produces csv output, but this makes sense for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out that there is pq.Array which sounds like something I want to use. I'll take a look into using it - thanks for bringing up the concern!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I confused myself. pq.Array would be useful for parsing the whole array, but here we are parsing the ROW, not the array. I would still go with the csv approach, but we might want to consider using LazyQuotes. I'll do some more tests before we merge this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think using LazyQuotes is what we want to be more permissive about quoting esp with comments - the CEL content itself should be validated before storing in DB by calling CEL's validation

evankanderson
evankanderson previously approved these changes Sep 17, 2024
`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: stacklok#4514
Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still wish we had a better parser, but I don't want to block this improvement on it.

@jhrozek jhrozek merged commit 66044f9 into stacklok:main Sep 19, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parsing a stored selector fails if the selector is using commas
4 participants