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

!fix(orderby): use BuildOptions instead of allowedColumns #46

Merged
merged 2 commits into from
May 2, 2024

Conversation

cnlangzi
Copy link
Member

@cnlangzi cnlangzi commented May 2, 2024

Changes

  • use BuildOption instead of allowedColumns

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @cnlangzi - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

func (ob *OrderByBuilder) isAllowed(col string) bool {
if ob.allowedColumns == nil {
return true
func (ob *OrderByBuilder) getColumn(col string) (string, bool) {
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider renaming getColumn to reflect its functionality.

The function does more than just getting a column; it also checks if the column is allowed and applies transformations. A name like 'processAndValidateColumn' might be more descriptive.

Suggested change
func (ob *OrderByBuilder) getColumn(col string) (string, bool) {
func (ob *OrderByBuilder) processAndValidateColumn(col string) (string, bool) {

build: func() *Builder {
b := New("SELECT * FROM users")

ob := NewOrderBy(WithToName(strcase.ToSnake), WithAllow("created_at")).
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Missing test case for WithToName functionality.

The test case 'with_to_name_order_by_should_work' checks the transformation of column names but does not verify if non-allowed columns are correctly excluded when transformed. It would be beneficial to add assertions to ensure that transformed but disallowed columns do not appear in the final SQL.

Suggested change
ob := NewOrderBy(WithToName(strcase.ToSnake), WithAllow("created_at")).
ob := NewOrderBy(WithToName(strcase.ToSnake), WithAllow("created_at")).
ByAsc("id", "name").
ByDesc("createdAt", "unsafe_input")
assert.False(t, strings.Contains(ob.String(), "unsafe_input"), "Disallowed column 'unsafe_input' should not appear in the final SQL")
assert.False(t, strings.Contains(ob.String(), "created_at"), "Allowed column 'created_at' should not appear in the final SQL when not explicitly ordered")

Copy link

codecov bot commented May 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.76%. Comparing base (f7859c0) to head (caa47d4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #46      +/-   ##
==========================================
+ Coverage   76.65%   76.76%   +0.11%     
==========================================
  Files          44       44              
  Lines        1829     1838       +9     
==========================================
+ Hits         1402     1411       +9     
  Misses        304      304              
  Partials      123      123              
Flag Coverage Δ
Unit-Tests 76.76% <100.00%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

deepsource-io bot commented May 2, 2024

Here's the code health analysis summary for commits f7859c0..caa47d4. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@cnlangzi cnlangzi merged commit d72967c into main May 2, 2024
7 checks passed
@cnlangzi cnlangzi deleted the fix/orderby branch May 2, 2024 03:12
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.

1 participant