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

feat: replace array with List, add type object processing #100

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ function defineType(prop, propName) {
if (prop.type() === 'object') {
return _.upperFirst(_.camelCase(prop.uid()));
} else if (prop.type() === 'array') {
if (prop.items().format()) {
return toJavaType(prop.items().format()) + '[]';
if (prop.items().type() === 'object') {
return 'List<' + _.upperFirst(_.camelCase(prop.items().uid())) + '>';
} else if (prop.items().format()) {
return 'List<' + toClass(toJavaType(prop.items().format())) + '>';
} else {
return toJavaType(prop.items().type()) + '[]';
return 'List<' + toClass(toJavaType(prop.items().type())) + '>';
}
} else if (prop.enum() && (prop.type() === 'string' || prop.type() === 'integer')) {
return _.upperFirst(_.camelCase(propName)) + 'Enum';
Expand Down Expand Up @@ -39,6 +41,24 @@ function defineType(prop, propName) {
}
filter.defineType = defineType;

function toClass(couldBePrimitive) {
switch(couldBePrimitive) {
case 'int':
return 'Integer';
case 'long':
return 'Long';
case 'boolean':
return 'Boolean';
case 'float':
return 'Float';
case 'double':
return 'Double';
default:
return couldBePrimitive;
}
}
filter.toClass = toClass;

function toJavaType(str){
switch(str) {
case 'integer':
Expand Down
8 changes: 5 additions & 3 deletions template/src/main/java/com/asyncapi/model/$$message$$.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package {{ params['userJavaPackage'] }}.model;

import java.util.Objects;
import javax.validation.Valid;

import java.util.Objects;
import java.util.List;

{% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %}
* {{ line | safe}}{% endfor %}{% if message.examples() %}
* Examples: {{message.examples() | examplesToString | safe}}{% endif %}
Expand Down Expand Up @@ -34,10 +36,10 @@ public class {{payloadName}} {
{%- for obj in message.payload().allOf() %}
{%- set varName = obj.uid() | camelCase %}
{%- set className = obj.uid() | camelCase | upperFirst %}
{%- set propType = obj | defineType(obj.uid()) %}
{%- set propType = obj | defineType(obj.uid()) | safe %}

{%- if obj.type() === 'array' %}
{%- set varName = obj.uid() | camelCase + 'Array' %}
{%- set varName = obj.uid() | camelCase + 'List' %}
{%- endif %}
private @Valid {{propType}} {{varName}};

Expand Down
26 changes: 14 additions & 12 deletions template/src/main/java/com/asyncapi/model/$$objectSchema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

{% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %}
Expand All @@ -19,10 +19,12 @@ public class {{schemaName | camelCase | upperFirst}} {
{%- if prop.type() === 'object' %}
private @Valid {{prop.uid() | camelCase | upperFirst}} {{propName | camelCase}};
{%- elif prop.type() === 'array' %}
{%- if prop.items().format() %}
private @Valid {{prop.items().format() | toJavaType}}[] {{propName | camelCase}}Array;
{%- if prop.items().type() === 'object' %}
private @Valid List<{{prop.items().uid() | camelCase | upperFirst}}> {{propName | camelCase}}List;
{%- elif prop.items().format() %}
private @Valid List<{{prop.items().format() | toJavaType | toClass}}> {{propName | camelCase}}List;
{%- else %}
private @Valid {{prop.items().type() | toJavaType}}[] {{propName | camelCase}}Array;
private @Valid List<{{prop.items().type() | toJavaType | toClass}}> {{propName | camelCase}}List;
{%- endif %}
{%- elif prop.enum() and (prop.type() === 'string' or prop.type() === 'integer') %}
public enum {{propName | camelCase | upperFirst}}Enum {
Expand Down Expand Up @@ -88,10 +90,10 @@ public class {{allName}} {
{%- for obj in prop.allOf() %}
{%- set varName = obj.uid() | camelCase %}
{%- set className = obj.uid() | camelCase | upperFirst %}
{%- set propType = obj | defineType(obj.uid()) %}
{%- set propType = obj | defineType(obj.uid()) | safe %}

{%- if obj.type() === 'array' %}
{%- set varName = obj.uid() | camelCase + 'Array' %}
{%- set varName = obj.uid() | camelCase + 'List' %}
{%- endif %}
private @Valid {{propType}} {{varName}};

Expand All @@ -118,10 +120,10 @@ public class {{allName}} {
{% for propName, prop in schema.properties() %}
{%- set varName = propName | camelCase %}
{%- set className = propName | camelCase | upperFirst %}
{%- set propType = prop | defineType(propName) %}
{%- set propType = prop | defineType(propName) | safe %}

{%- if prop.type() === 'array' %}
{%- set varName = propName | camelCase + 'Array' %}
{%- set varName = propName | camelCase + 'List' %}
{%- endif %}

{% if prop.description() or prop.examples()%}/**{% for line in prop.description() | splitByLines %}
Expand Down Expand Up @@ -151,19 +153,19 @@ public boolean equals(Object o) {
return false;
}
{{schemaName | camelCase | upperFirst}} {{schemaName | camelCase}} = ({{schemaName | camelCase | upperFirst}}) o;
return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'Array' %}{% endif %}
{% if prop.type() === 'array' %}Arrays{% else %}Objects{% endif %}.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %}
return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'List' %}{% endif %}
Objects.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %}
}

@Override
public int hashCode() {
return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if prop.type() === 'array' %}Array{% endif %}{% if not loop.last %}, {% endif %}{% endfor %});
return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if prop.type() === 'array' %}List{% endif %}{% if not loop.last %}, {% endif %}{% endfor %});
}{% endif %}

@Override
public String toString() {
return "class {{schemaName | camelCase | upperFirst}} {\n" +
{% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'Array' %}{% endif %}
{% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'List' %}{% endif %}
" {{varName}}: " + toIndentedString({{varName}}) + "\n" +{% endfor %}
"}";
}
Expand Down