-
Notifications
You must be signed in to change notification settings - Fork 943
fix: list templates returns non-deprecated templates by default #17747
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
79313cc
fix: list templates returns non-deprecated templates by default
ssncferreira db52150
docs: update method docstrings for fetching templates and organizatio…
ssncferreira 3e5e40d
fix: comment in enterprise/coderd/templates_test.go
ssncferreira fa8f999
refactor: add comment to coderd/database/dbmem/dbmem.go
ssncferreira b344266
fix: move templates tests from enterprise/coderd/templates_test.go to…
ssncferreira 12985c5
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-depr…
ssncferreira 06acc27
refactor(dbmem): improve deprecation filtering logic with a helper fu…
ssncferreira c88d4d2
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-depr…
ssncferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: add comment to coderd/database/dbmem/dbmem.go
- Loading branch information
commit fa8f99992887e89df725e7696c322350887a6e83
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a comment here might be useful as well. Initially I wanted to suggest renaming
arg.Deprecated
but I imagine that's a bigger change to roll out here. It's just not very nice to read this as something likepotentially the template struct could be refactored to have
IsDeprecated
andDeprecatedMessage
as opposed to just havingDeprecated
message whose value being"some string"
means the template is deprecated. Alternatively, the template could have a functionIsDeprecated
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that the current expression with
arg.Deprecated.Valid && arg.IgnoreDeprecated && template.Deprecated.Valid
isn't the most readable.Just for context: this method filters templates based on the
arg
param in the in-memory database used for testing. This logic ensures that only templates matching the requested deprecated state are returned.The
Deprecated
field is of typesql.NullBool
, which is represented as:So we need to check
Valid
to avoid misinterpreting a NULL as false.The
template
is of typedatabase.Template
, generated bysqlc
.I could add a method
IsDeprecated
to theTemplate
struct to clean this up, but since that type is generated bysqlc
, don't think it would be a good idea to add this type of custom logic directly at the DB layer. An alternative might be to define a wrapper around the generated type or introduce a view model in the service layer that handles this logic more cleanly but that may add more complexity than it solves, especially for a test-specific filter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For readability I'd suggest a comment at most, or some light variable extraction.
dbmem
is only used for testing and it is planned to retire it at some point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cstyan added a comment in fa8f999
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading this change a little more closely, doesn't this invert the query logic?
Comparing with the SQL:
Aside: I've found it useful to comment the Go code in dbmem with the relevant parts of the corresponding SQL query.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in
dbmem
we are doing an inverse filtering to exclude non-matching templates.In the SQL, we filter as follows:
deprecated
is set totrue
, we only include templates wheredeprecated != ''
, i.e., there is a deprecation message.deprecated
is set tofalse
, we only include templates wheredeprecated = ''
, i.e., there is no deprecation message.deprecated
is not set (NULL
), we include all templates (ELSE true
clause).In the in-memory logic we are doing an inverse filtering, the
continue
is used to exclude non-matching templates:We skip templates when the
Deprecated
filter is set and the template's deprecation status does not match theDeprecated
value. This mirrors the behavior of the SQL CASE logic.I think what’s confusing here is the double negation. I’ll improve the comment and maybe add a simple
isDeprecated
function for readability.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduced a helper function
isDeprecated
and improved the comment by adding the corresponding SQL logic for better readability: 06acc27@cstyan @johnstcn let me know if this makes it clearer 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I like it