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

Normalize enum member name #2839

Open
wants to merge 108 commits into
base: main
Choose a base branch
from
Open

Conversation

MaryGao
Copy link
Member

@MaryGao MaryGao commented Sep 25, 2024

fixes #2832
fixes #2958

Background

For generated enum we need enum member name and value, and the typespec member name may not be a valid typescript name so we need to normalize them and also normalization could help produce more ts-style SDKs.

Unlike property or model name enum member could be any chars e.g *&^-, so it would require more sanitize logic during normalization. And to be consistant I apply all these rules to other type normalization also. So we could see some breakings in model name and property.

Design consideration

  • There is no perfect rule without knowing the context. From codegen side we would ensure the naming style is consistant and also ensure the generated code are compile-pass.
  • If the name is starting with digital number or is a numeric literal name we will add Num prefix and report warning to remind renaming for customers
  • We could disable normalization by turning on the option ignoreEnumMemberNameNormalize: true at package level
  • We could bypass normalization with adding prefix $DO_NOT_NORMALIZE$ at member level, e.g $DO_NOT_NORMALIZE$VALIDATION_NOT_REQUIRED => VALIDATION_NOT_REQUIRED
  • The breakings between HLC and modular should be limited, if existing we could leverage clientName or above prefix to avoid that
  • The normalization would also impact other norm and for property the main impact would remove the special chars in property e.g $filter to filter

Cases

// case 1: pure alphabets
["pascal", "Pascal"],
["pascalCase", "PascalCase"],
["PascalCase", "PascalCase"],
["pascalcase", "Pascalcase"],
["Pascalcase", "Pascalcase"],

// case 2: alphabets + special chars e.g .-*/
["pascal_case", "PascalCase"],
["pascal_case_", "PascalCase"],
["_pascal_case", "PascalCase"],
["pascal, case", "PascalCase"],
["MAX_of_MLD", "MAXOfMld"],

// case 3: alphabets + special chars + digits
["___pascal____case6666", "PascalCase6666"],
["_10Min", "Num_10Min"],

// case 4: digits but not a numeric name
["090", "Num090"],

// case 5: digits but valid numeric name
["10", "Num10"],
["-1.1", "Num-1.1"],

Relevant issues

@MaryGao MaryGao requested a review from qiaozha November 22, 2024 05:54
@@ -1008,6 +1010,21 @@ describe("model type", () => {
);
});

it("number enum and not ignore warnings", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

need to keep this case.

@MaryGao MaryGao changed the title Normalize enum member name in Modular Normalize enum member name Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants