- Notifications
You must be signed in to change notification settings - Fork907
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
github-actionsbot commentedMay 9, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
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.
Some style/organization comments, but I'm not sure about all the details of the preferred style for Go and tests here so curious what others have to say.
coderd/database/dbmem/dbmem.go Outdated
@@ -13021,7 +13021,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G | |||
if arg.ExactName != "" && !strings.EqualFold(template.Name, arg.ExactName) { | |||
continue | |||
} | |||
if arg.Deprecated.Valid && arg.Deprecated.Bool== (template.Deprecated != "") { | |||
if arg.Deprecated.Valid && arg.Deprecated.Bool!= (template.Deprecated != "") { |
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 renamingarg.Deprecated
but I imagine that's a bigger change to roll out here. It's just not very nice to read this as something like
if arg.Deprecated.Valid && arg.IgnoreDeprecated && template.IsDeprecated
potentially the template struct could be refactored to haveIsDeprecated
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 witharg.Deprecated.Valid && arg.IgnoreDeprecated && template.Deprecated.Valid
isn't the most readable.
Just for context: this method filters templates based on thearg
param in the in-memory database used for testing. This logic ensures that only templates matching the requested deprecated state are returned.
TheDeprecated
field is of typesql.NullBool
, which is represented as:
type NullBool struct {Bool boolValid bool // Valid is true if Bool is not NULL}
So we need to checkValid
to avoid misinterpreting a NULL as false.
Thetemplate
is of typedatabase.Template
, generated bysqlc
.
I could add a methodIsDeprecated
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.
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:
-- Filter by deprecatedAND CASEWHENsqlc.narg('deprecated') ::booleanIS NOT NULL THENCASEWHENsqlc.narg('deprecated') ::boolean THENdeprecated!=''ELSEdeprecated=''ENDELSE trueEND
Aside: I've found it useful to comment the Go code in dbmem with the relevant parts of the corresponding SQL query.
ssncferreiraMay 13, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
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, indbmem
we are doing aninverse filtering to exclude non-matching templates.
In the SQL, we filter as follows:
- If
deprecated
is set totrue
, we only include templates wheredeprecated != ''
, i.e., there is a deprecation message. - If
deprecated
is set tofalse
, we only include templates wheredeprecated = ''
, i.e., there is no deprecation message. - If
deprecated
is not set (NULL
), we include all templates (ELSE true
clause).
In the in-memory logic we are doing aninverse filtering, thecontinue
is used to exclude non-matching templates:
if arg.Deprecated.Valid && arg.Deprecated.Bool != (template.Deprecated != "") { continue}
We skip templates when theDeprecated
filter is set and the template's deprecation statusdoes 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 simpleisDeprecated
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.
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
Uh oh!
There was an error while loading.Please reload this page.
612391d
tod9146c9
Comparecoderd/database/dbmem/dbmem.go Outdated
@@ -13021,7 +13021,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G | |||
if arg.ExactName != "" && !strings.EqualFold(template.Name, arg.ExactName) { | |||
continue | |||
} | |||
if arg.Deprecated.Valid && arg.Deprecated.Bool== (template.Deprecated != "") { | |||
if arg.Deprecated.Valid && arg.Deprecated.Bool!= (template.Deprecated != "") { |
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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
ea595ed
tofa8f999
Compare… coderd/templates_test.go
…nction and improve comments by adding the matching SQL logic
599bb35
intomainUh oh!
There was an error while loading.Please reload this page.
Description
Modifies the behaviour of the "list templates" API endpoints to return non-deprecated templates by default. Users can still query for deprecated templates by specifying the
deprecated=true
query parameter.Note: The deprecation feature is an enterprise-level feature
Affected Endpoints
Fixes#17565