Skip to content

Commit

Permalink
fix: Enum generation (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored Oct 8, 2020
1 parent 9264d8f commit dd438f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,15 @@ function schemeExists(collection, scheme){
return _.some(collection, {'scheme': scheme});
};
filter.schemeExists = schemeExists;

function createEnum(val){
let result;
let withoutNonWordChars = val.replace(/[^A-Z^a-z^0-9]/g, "_");
if ((new RegExp('^[^A-Z^a-z]', 'i')).test(withoutNonWordChars)) {
result = '_' + withoutNonWordChars;
} else {
result = withoutNonWordChars;
}
return result;
};
filter.createEnum = createEnum;
2 changes: 1 addition & 1 deletion template/src/main/java/com/asyncapi/model/$$schema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class {{schemaName | camelCase | upperFirst}} {
public enum {{propName | camelCase | upperFirst}}Enum {
{% for e in prop.enum() %}
{%- if prop.type() === 'string'%}
{{e | upper | replace(' ', '_')}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %}
{{e | upper | createEnum}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %}
{%- else %}
NUMBER_{{e}}({{e}}){% if not loop.last %},{% else %};{% endif %}
{%- endif %}
Expand Down

0 comments on commit dd438f3

Please sign in to comment.