Fix nim compilation in case a edge case of a schema with enum (#20780)

If enum constraint contains one value which is not a valid nim
identifier, we must surround this identifier with backtick.
This commit is contained in:
sandwoodK 2025-05-20 09:38:58 +02:00 committed by GitHub
parent 78b54b9283
commit 13c95f1dd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -355,15 +355,21 @@ public class NimClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
private boolean isValidIdentifier(String identifier) {
//see https://nim-lang.org/docs/manual.html#lexical-analysis-identifiers-amp-keywords
return identifier.matches("^(?:[A-Z]|[a-z]|[\\x80-\\xff])(_?(?:[A-Z]|[a-z]|[\\x80-\\xff]|[0-9]))*$");
}
@Override
public String toEnumVarName(String name, String datatype) {
name = name.replace(" ", "_");
name = StringUtils.camelize(name);
if (name.matches("\\d.*")) { // starts with number
return "`" + name + "`";
} else {
// starts with number or contains any character not allowed,see
if (isValidIdentifier(name)) {
return name;
} else {
return "`" + name + "`";
}
}