Skip to content

fix(caser): ToSnake does not replace spaces with _#1094

Closed
ankur0904 wants to merge 3 commits intocloudquery:mainfrom
ankur0904:fix-bug-caser-#728
Closed

fix(caser): ToSnake does not replace spaces with _#1094
ankur0904 wants to merge 3 commits intocloudquery:mainfrom
ankur0904:fix-bug-caser-#728

Conversation

@ankur0904
Copy link
Copy Markdown

@ankur0904 ankur0904 commented Jul 14, 2023

Summary

This PR fixes #728


Use the following steps to ensure your PR is ready to be reviewed

  • Read the contribution guidelines 🧑‍🎓
  • Run go fmt to format your code 🖊
  • Lint your changes via golangci-lint run 🚨 (install golangci-lint here)
  • Update or add tests 🧪
  • Ensure the status checks below are successful ✅

@ankur0904 ankur0904 requested a review from yevgenypats as a code owner July 14, 2023 19:13
@erezrokah erezrokah changed the title fix : bug(caser): ToSnake does not replace spaces with _ fix(caser): ToSnake does not replace spaces with _ Jul 15, 2023
@github-actions github-actions bot added the fix label Jul 15, 2023
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 16, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.08% ⚠️

Comparison is base (3f5dae9) 47.17% compared to head (4814550) 47.09%.
Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1094      +/-   ##
==========================================
- Coverage   47.17%   47.09%   -0.08%     
==========================================
  Files          84       85       +1     
  Lines        7728     7792      +64     
==========================================
+ Hits         3646     3670      +24     
- Misses       3748     3787      +39     
- Partials      334      335       +1     
Files Changed Coverage Δ
caser/caser.go 91.78% <100.00%> (+0.80%) ⬆️

... and 7 files with indirect coverage changes

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

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 16, 2023

⏱️ Benchmark results

Comparing with 8a6a2cc

  • Glob-8 ns/op: 98.79 ⬇️ 0.90% decrease vs. 8a6a2cc

Copy link
Copy Markdown
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @ankur0904, do you mind adding some unit tests to it? There's a caser/caser_test.go file you can use

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah I will add unit test.

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah
If I am adding this {Camel: "Test Space", Snake: "test_space"} in Test_ToSnake case. Then test got fail

                Error:          Not equal: 
                                expected: "test_space"
                                actual  : "test__space"

After observing I come to know when I remove this word = strings.ReplaceAll(word, " ", "_") line from caser.go file the error become

                                expected: "test_space"
                                actual  : "test _space"

According to my observation
word = strings.ReplaceAll(word, " ", "") should be added to caser.go this will passing the test case.

PASS
ok      github.com/cloudquery/plugin-sdk/v4/caser       0.012s

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah What are your thoughts?

@erezrokah
Copy link
Copy Markdown
Member

@erezrokah What are your thoughts?

Hi @ankur0904 thank you for looking into it, will that solution work for the following test {Camel: "Hello Wor ld", Snake: "hello_wor_ld"}?

I think we might need to first replace spaces on the entire string, then split the words, WDYT?

You can also use https://textedit.tools/snakecase to test

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah You are absolutely right {Camel: "Hello Wor ld", Snake: "hello_wor_ld"} gives the error.

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah I have updated the code, in this code I first replace spaces on the entire string then if there is __ then replace it with _. And also added some additional corner cases like e.g. leading trailing spaces h e l l o, h e l l o , h e l l o

Unit test I added

{Camel: "Hello World", Snake: "hello_world"},
{Camel: "Hello Wor ld", Snake: "hello_wor_ld"},
{Camel: "H e l l o", Snake: "h_e_l_l_o"},
{Camel: "H e l l o ", Snake: "h_e_l_l_o"},
{Camel: " H e l l o", Snake: "h_e_l_l_o"},
{Camel: " H e l l o ", Snake: "h_e_l_l_o"},

After running

$ go test
PASS
ok      github.com/cloudquery/plugin-sdk/v4/caser       0.006s

@erezrokah
Copy link
Copy Markdown
Member

Thanks for following up @ankur0904, looks better but I think it's missing {Camel: "Hello World", Snake: "hello_wor_ld"}, (see the multiple spaces).

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah Apologize, I have forgotten to mention that.

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah Does anything else we need to change?

@erezrokah
Copy link
Copy Markdown
Member

@erezrokah Does anything else we need to change?

Sorry for the late reply. I missed your last commit. I'll take a look tomorrow.

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah

@erezrokah Does anything else we need to change?

Sorry for the late reply. I missed your last commit. I'll take a look tomorrow.

Can this PR be merged?

@erezrokah
Copy link
Copy Markdown
Member

Can this PR be merged?

Hi @ankur0904, let me look at this again. I think this could be simplified to replace the spaces once before splitting the words.

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah Should I need to change the code?

@erezrokah
Copy link
Copy Markdown
Member

@erezrokah Should I need to change the code?

Not yet, I'll comment on the PR if that's needed

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah OK

@erezrokah
Copy link
Copy Markdown
Member

Hi @ankur0904 I opened #1148 as a draft with a different approach to get rid of the spaces. I think that's more consistent with the current code (doesn't use regular expressions). Please let me know what you think

@ankur0904
Copy link
Copy Markdown
Author

@erezrokah #1148 That looks fine and more consistent.

kodiakhq bot pushed a commit that referenced this pull request Sep 4, 2023
#### Summary

Fixes #728

Another approach to #1094.
Instead of regular expressions we use `strings.Fields` to get rid of the spaces and replace them with `_`. Then we use `_` as a word separator.
@candiduslynx
Copy link
Copy Markdown
Contributor

Closing, as #1198 was released as part of v4.6.4

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.

bug(caser): ToSnake does not replace spaces with _

3 participants