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

unexpected changes in camelToSnake() at v1.176.1 #1048

Closed
mishio-n opened this issue May 30, 2024 · 7 comments · Fixed by #1052 · May be fixed by habyyb/CD-AdvancedDigitalIQ#3
Closed

unexpected changes in camelToSnake() at v1.176.1 #1048

mishio-n opened this issue May 30, 2024 · 7 comments · Fixed by #1052 · May be fixed by habyyb/CD-AdvancedDigitalIQ#3
Labels

Comments

@mishio-n
Copy link

Probrem

I noticed that the behavior of the camelToSnake() changed with the fix in #1046.

Specifically, the behavior has changed when the string to be converted contains numbers.
For example, if the package name includes a version like v1, it is affected.

With the following code:

package sample.v1;
  • Prior to version 1.176.0, the generated code was:
export const SAMPLE_V1_PACKAGE_NAME = "sample.v1";
  • In version 1.176.1, the generated code is now:
export const SAMPLE_V_1_PACKAGE_NAME = "sample.v1";

As a result, the generated constant name has changed, causing build errors.

To clarify, these codes are generated when using the nestJs=true option.

Upon reviewing the PR, it seems this behavior change was not intended.
This unexpected change in a patch version has caused a lot of confusion.


That said, the changes in #1046 appear reasonable.
Reverting the commit does not seem to be a good solution.

I reviewed the documentation and code of the case-anything package, which is used internally by camelToSnake(), and it seems difficult to control the handling of numbers externally.

Moreover, there is no set specification on how to handle numbers in snake_case, and there are multiple opinions on the matter. Therefore, I don't think the behavior in v1.176.1 is incorrect.

How do you plan to address this issue?

If the behavior in v1.176.1 is considered correct, I believe it should be clearly documented, and a minor version update should be made.

Sorry I can't offer any concrete suggestions for improvement.

I am always grateful for this package. Thank you very much.


Environment Used for Testing

The information about the environment I used for testing is as follows:

├── proto
│   └── sample
│       └── v1
│           └── sample.proto
├── buf.gen.yaml

buf.gen.yaml

version: v1
plugins:
  - name: ts
    out: gen/ts
    opt:
      - nestJs=true
    path: node_modules/ts-proto/protoc-gen-ts_proto

sample.proto

syntax = "proto3";

package sample.v1;

service SampleService {
  rpc GetSample(GetSampleRequest) returns (GetSampleResponse) {}
}

message GetSampleRequest {
  string id = 1;
}

message GetSampleResponse {
  string id = 1;
  string name = 2;
}
@daeteck
Copy link

daeteck commented Jun 3, 2024

Same problema here. This is a breaking change that is given us a lot of problems.

@stephenh
Copy link
Owner

stephenh commented Jun 3, 2024

Hey guys, thanks for the report! Hopefully it won't be too bad to craft a regex that achieves both goals--I'll take a look at this in a few days/next weekend, unless @BrandonArmand you can take a look/beat me to it. Thanks!

@BrandonArmand
Copy link
Contributor

Hey yeah i actually noticed this today as well. I will be happy to whip up some regex.

@mishio-n
Copy link
Author

mishio-n commented Jun 4, 2024

Thanks.

I think, the original cause was two-fold:

  1. the consecutive capital letters were not being treated as independent words.
  2. metacharacter \w contains capital letters.

For 1.
Consider a series of uppercase letters as a word, and consider that they can be separated by one before they change to lowercase letters.

For 2.
Use [a-z0-9] insted of \w.

So, I came up with the following regular expression.

export function camelToSnake(s: string): string {
  return s.replace(/(?<!^)([a-z0-9][A-Z]|([A-Z][A-Z])[a-z0-9])/g, (m) => m.length === 2 ? m[0] + "_" + m[1] :  m[0] + "_" + m[1] + m[2]).toUpperCase();
}

// getAPIValueV1Beta1 => GET_API_VALUE_V1_BETA1

I have passed the test case, but I feel it is not very elegant.

If you have any good ideas, please let me know.

@stephenh
Copy link
Owner

stephenh commented Jun 4, 2024

I believe #1052 is a good approach, and at least fixes all current test cases 😅 , I went ahead and merged the fix, let us know if there are other exceptions. 🤞

stephenh pushed a commit that referenced this issue Jun 4, 2024
## [1.176.2](v1.176.1...v1.176.2) (2024-06-04)

### Bug Fixes

* Fix snake casing numbers. ([#1052](#1052)) ([f85a2f1](f85a2f1)), closes [#1048](#1048)
@stephenh
Copy link
Owner

stephenh commented Jun 4, 2024

🎉 This issue has been resolved in version 1.176.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@mishio-n
Copy link
Author

mishio-n commented Jun 4, 2024

@stephenh Thank you so much for the fix !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants