From 01249f5b5457235dc032453139383229df03ad1b Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Sat, 17 Jul 2021 16:43:01 +0200 Subject: [PATCH] added models generator --- examples/de/nclazz/users/v0/models/Role.java | 57 ++ .../de/nclazz/users/v0/models/RoleForm.java | 38 + examples/de/nclazz/users/v0/models/User.java | 57 ++ .../de/nclazz/users/v0/models/UserForm.java | 42 + .../de/nclazz/users/v0/models/UserLogin.java | 47 + .../nclazz/users/v0/models/UserLoginForm.java | 42 + .../de/nclazz/users/v0/models/UserStatus.java | 47 + .../users/v0/models/UserStatusForm.java | 39 + examples/example.json | 944 ++++++++++++++++++ package-lock.json | 5 + package.json | 5 +- src/generators/java-models-generator.js | 36 + src/index.js | 22 + src/java-model.js | 3 + templates/lombok-enum.java.mustache | 40 + templates/lombok-model.java.mustache | 43 + 16 files changed, 1465 insertions(+), 2 deletions(-) create mode 100644 examples/de/nclazz/users/v0/models/Role.java create mode 100644 examples/de/nclazz/users/v0/models/RoleForm.java create mode 100644 examples/de/nclazz/users/v0/models/User.java create mode 100644 examples/de/nclazz/users/v0/models/UserForm.java create mode 100644 examples/de/nclazz/users/v0/models/UserLogin.java create mode 100644 examples/de/nclazz/users/v0/models/UserLoginForm.java create mode 100644 examples/de/nclazz/users/v0/models/UserStatus.java create mode 100644 examples/de/nclazz/users/v0/models/UserStatusForm.java create mode 100644 examples/example.json create mode 100644 src/generators/java-models-generator.js create mode 100644 src/index.js create mode 100644 templates/lombok-enum.java.mustache create mode 100644 templates/lombok-model.java.mustache diff --git a/examples/de/nclazz/users/v0/models/Role.java b/examples/de/nclazz/users/v0/models/Role.java new file mode 100644 index 0000000..c372b66 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/Role.java @@ -0,0 +1,57 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + +import java.util.UUID; +import java.time.LocalDateTime; +import de.nclazz.users.v0.models.UserStatus; + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class Role { + + /** Role assigned to a user. **/ + @NotNull + private UUID id; + + /** Role assigned to a user. **/ + @NotNull + private String name; + + /** Role assigned to a user. **/ + @NotNull + private LocalDateTime created; + + /** Role assigned to a user. **/ + @NotNull + private LocalDateTime updated; + + /** Role assigned to a user. **/ + @NotNull + private UserStatus status; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/RoleForm.java b/examples/de/nclazz/users/v0/models/RoleForm.java new file mode 100644 index 0000000..a00767a --- /dev/null +++ b/examples/de/nclazz/users/v0/models/RoleForm.java @@ -0,0 +1,38 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class RoleForm { + + /** Form for creating and updating a new user role. **/ + @NotNull + private String name; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/User.java b/examples/de/nclazz/users/v0/models/User.java new file mode 100644 index 0000000..885e364 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/User.java @@ -0,0 +1,57 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + +import de.nclazz.users.v0.models.UserStatus; +import de.nclazz.users.v0.models.Role; +import java.util.List; + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class User { + + /** ID of the user as a 32 char long hex string. **/ + @NotNull + private String id; + + /** Name of the user, which can be the email address. **/ + @NotNull + private String name; + + /** Email of the user. **/ + @NotNull + private String email; + + /** Status of the user as described in the user_status enum. **/ + @NotNull + private UserStatus status; + + /** Roles assigned to the user. **/ + @NotNull + private List roles; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/UserForm.java b/examples/de/nclazz/users/v0/models/UserForm.java new file mode 100644 index 0000000..4cd88bf --- /dev/null +++ b/examples/de/nclazz/users/v0/models/UserForm.java @@ -0,0 +1,42 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class UserForm { + + /** Name of the user to be created. **/ + @NotNull + private String name; + + /** Email of the user to be created. **/ + @NotNull + private String email; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/UserLogin.java b/examples/de/nclazz/users/v0/models/UserLogin.java new file mode 100644 index 0000000..841c1b7 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/UserLogin.java @@ -0,0 +1,47 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + +import de.nclazz.users.v0.models.User; + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class UserLogin { + + /** Information and Tokens for the logged in user for authentication. **/ + @NotNull + private User user; + + /** Information and Tokens for the logged in user for authentication. **/ + @NotNull + private String token; + + /** Information and Tokens for the logged in user for authentication. **/ + @NotNull + private String refreshToken; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/UserLoginForm.java b/examples/de/nclazz/users/v0/models/UserLoginForm.java new file mode 100644 index 0000000..b9ceff6 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/UserLoginForm.java @@ -0,0 +1,42 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class UserLoginForm { + + /** Login Form for username:password authentication. **/ + @NotNull + private String username; + + /** Login Form for username:password authentication. **/ + @NotNull + private String password; + + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/UserStatus.java b/examples/de/nclazz/users/v0/models/UserStatus.java new file mode 100644 index 0000000..387bc01 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/UserStatus.java @@ -0,0 +1,47 @@ +/** + * Auto-generated from apibuilder.io service spec. + * apidoc-version : 0.15.4 + * organisation : nclazz + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package de.nclazz.users.v0.models; + +public enum UserStatus { + + /** User is active and has access to the system. **/ + ACTIVE("active"), + + /** User is not active and has no access to the system. **/ + INACTIVE("inactive"), + + /** User is created but not has not yet access to the system. **/ + CREATED("created"), + + /** User is deleted and can not access the system. Deleted user will stay in the system for a while, before completely removed. **/ + DELETED("deleted"); + + + + private final String value; + + UserStatus(String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } + + public static UserStatus forValue(String value) { + for(UserStatus userStatus : UserStatus.values()) { + if(userStatus.value.equals(value)) { + return userStatus; + } + } + throw new IllegalArgumentException("UserStatus '" + value + "' does not exist!"); + } + +} \ No newline at end of file diff --git a/examples/de/nclazz/users/v0/models/UserStatusForm.java b/examples/de/nclazz/users/v0/models/UserStatusForm.java new file mode 100644 index 0000000..bd87f23 --- /dev/null +++ b/examples/de/nclazz/users/v0/models/UserStatusForm.java @@ -0,0 +1,39 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : 0.15.4 + * organisation : nclazz + * service-version : 0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * + * Documentation at: + * https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging + * +**/ +package ; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + +import de.nclazz.users.v0.models.UserStatus; + +/** + * + * See https://app.apibuilder.io/nclazz/users/0.0.0-dev-30-gbd50cdf-bd9390b2-staging#model- + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class UserStatusForm { + + /** Form for updating the status of a user. **/ + @NotNull + private UserStatus status; + + +} \ No newline at end of file diff --git a/examples/example.json b/examples/example.json new file mode 100644 index 0000000..898bdcf --- /dev/null +++ b/examples/example.json @@ -0,0 +1,944 @@ +{ + "apidoc": { + "version": "0.15.4" + }, + "name": "users", + "organization": { + "key": "nclazz" + }, + "application": { + "key": "users" + }, + "namespace": "de.nclazz.users.v0", + "version": "0.0.0-dev-30-gbd50cdf-bd9390b2-staging", + "info": { + "contact": { + "name": "Niclas Thobaben", + "url": "https://nclazz.de", + "email": "info@nclazz.de" + } + }, + "headers": [], + "imports": [ + { + "uri": "https://app.apibuilder.io/nclazz/common/latest/service.json", + "namespace": "de.nclazz.common.v0", + "organization": { + "key": "nclazz" + }, + "application": { + "key": "common" + }, + "version": "0.0.0-dev", + "enums": [], + "interfaces": [], + "unions": [], + "models": [ + "genericError", + "healthcheck" + ], + "annotations": [ + { + "name": "personal_data", + "description": "Identifies a field that contains Personal Data, as defined by GDPR." + } + ] + }, + { + "uri": "https://app.apibuilder.io/nclazz/errors/latest/service.json", + "namespace": "de.nclazz.errors.v0", + "organization": { + "key": "nclazz" + }, + "application": { + "key": "errors" + }, + "version": "0.0.0-dev-24-g9ca2bf7-bd9390b2-staging", + "enums": [], + "interfaces": [ + "error" + ], + "unions": [], + "models": [ + "genericError" + ], + "annotations": [ + { + "name": "personal_data", + "description": "Identifies a field that contains Personal Data, as defined by GDPR." + } + ] + } + ], + "enums": [ + { + "name": "userStatus", + "plural": "userStatuses", + "values": [ + { + "name": "active", + "attributes": [], + "description": "User is active and has access to the system." + }, + { + "name": "inactive", + "attributes": [], + "description": "User is not active and has no access to the system." + }, + { + "name": "created", + "attributes": [], + "description": "User is created but not has not yet access to the system." + }, + { + "name": "deleted", + "attributes": [], + "description": "User is deleted and can not access the system. Deleted user will stay in the system for a while, before completely removed." + } + ], + "attributes": [], + "description": "Status of a given user or user role." + } + ], + "interfaces": [], + "unions": [], + "models": [ + { + "name": "role", + "plural": "roles", + "fields": [ + { + "name": "id", + "type": "uuid", + "required": true, + "attributes": [], + "annotations": [] + }, + { + "name": "name", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "minimum": 4, + "maximum": 64 + }, + { + "name": "created", + "type": "date-time-iso8601", + "required": true, + "attributes": [], + "annotations": [] + }, + { + "name": "updated", + "type": "date-time-iso8601", + "required": true, + "attributes": [], + "annotations": [] + }, + { + "name": "status", + "type": "userStatus", + "required": true, + "attributes": [], + "annotations": [] + } + ], + "attributes": [], + "interfaces": [], + "description": "Role assigned to a user." + }, + { + "name": "role_form", + "plural": "role_forms", + "fields": [ + { + "name": "name", + "type": "string", + "required": true, + "attributes": [], + "annotations": [] + } + ], + "attributes": [], + "interfaces": [], + "description": "Form for creating and updating a new user role." + }, + { + "name": "user", + "plural": "users", + "fields": [ + { + "name": "id", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "description": "ID of the user as a 32 char long hex string.", + "minimum": 32, + "maximum": 32 + }, + { + "name": "name", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "description": "Name of the user, which can be the email address.", + "minimum": 8, + "maximum": 32 + }, + { + "name": "email", + "type": "string", + "required": true, + "attributes": [], + "annotations": [ + "personal_data" + ], + "description": "Email of the user.", + "minimum": 4, + "maximum": 64 + }, + { + "name": "status", + "type": "userStatus", + "required": true, + "attributes": [], + "annotations": [], + "description": "Status of the user as described in the user_status enum." + }, + { + "name": "roles", + "type": "[role]", + "required": true, + "attributes": [], + "annotations": [], + "description": "Roles assigned to the user." + } + ], + "attributes": [], + "interfaces": [], + "description": "A user defined by an id and an username." + }, + { + "name": "user_form", + "plural": "user_forms", + "fields": [ + { + "name": "name", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "description": "Name of the user to be created.", + "minimum": 8, + "maximum": 32 + }, + { + "name": "email", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "description": "Email of the user to be created.", + "minimum": 4, + "maximum": 64 + } + ], + "attributes": [], + "interfaces": [], + "description": "Form for creating and updating a new user." + }, + { + "name": "userLogin", + "plural": "userLogins", + "fields": [ + { + "name": "user", + "type": "user", + "required": true, + "attributes": [], + "annotations": [] + }, + { + "name": "token", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "minimum": 64, + "maximum": 64 + }, + { + "name": "refreshToken", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "minimum": 64, + "maximum": 64 + } + ], + "attributes": [], + "interfaces": [], + "description": "Information and Tokens for the logged in user for authentication." + }, + { + "name": "userLogin_form", + "plural": "userLogin_forms", + "fields": [ + { + "name": "username", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "minimum": 8, + "maximum": 32 + }, + { + "name": "password", + "type": "string", + "required": true, + "attributes": [], + "annotations": [], + "minimum": 8, + "maximum": 64 + } + ], + "attributes": [], + "interfaces": [], + "description": "Login Form for username:password authentication." + }, + { + "name": "userStatus_form", + "plural": "userStatus_forms", + "fields": [ + { + "name": "status", + "type": "userStatus", + "required": true, + "attributes": [], + "annotations": [] + } + ], + "attributes": [], + "interfaces": [], + "description": "Form for updating the status of a user." + } + ], + "resources": [ + { + "type": "de.nclazz.common.v0.models.healthcheck", + "plural": "healthchecks", + "operations": [ + { + "method": "GET", + "path": "/_internal_/healthcheck/healthcheck", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "de.nclazz.common.v0.models.healthcheck", + "description": "Get the current health status of the service.", + "attributes": [] + } + ], + "attributes": [], + "description": "Simple healthcheck endpoint to test the status of the service." + } + ], + "attributes": [], + "path": "/_internal_/healthcheck", + "description": "Simple healthcheck resource for monitoring purposes." + }, + { + "type": "role", + "plural": "roles", + "operations": [ + { + "method": "GET", + "path": "/roles/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "[role]", + "description": "OK.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get all defined user roles." + }, + { + "method": "GET", + "path": "/roles/:id/:other", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + }, + { + "name": "other", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "role", + "description": "User role found.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "User role not found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get a user role for id." + }, + { + "method": "POST", + "path": "/roles/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 201 + } + }, + "type": "user", + "description": "User role successfully created.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Create a new user role.", + "body": { + "type": "role_form", + "attributes": [] + } + }, + { + "method": "PUT", + "path": "/roles/:id/status", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "userStatus", + "description": "User role status updated successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Update the status of a user role.", + "body": { + "type": "userStatus_form", + "attributes": [] + } + }, + { + "method": "DELETE", + "path": "/roles/:id", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "unit", + "description": "User role deleted successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Mark a user role as deleted. The user role cannot be accessed by the api, but will stay in the db for a while, before completely removed." + } + ], + "attributes": [], + "path": "/roles", + "description": "Managing user roles." + }, + { + "type": "user", + "plural": "users", + "operations": [ + { + "method": "GET", + "path": "/users/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "[user]", + "description": "Users found.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get all users." + }, + { + "method": "GET", + "path": "/users/:id", + "parameters": [ + { + "name": "id", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "user", + "description": "User found.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not found.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get a user for given id." + }, + { + "method": "POST", + "path": "/users/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 201 + } + }, + "type": "user", + "description": "User successfully created.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Create a new user.", + "body": { + "type": "user_form", + "attributes": [] + } + }, + { + "method": "PUT", + "path": "/users/:id", + "parameters": [ + { + "name": "id", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "user", + "description": "User updated successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Update user data.", + "body": { + "type": "user_form", + "attributes": [] + } + }, + { + "method": "PUT", + "path": "/users/:id/status", + "parameters": [ + { + "name": "id", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "userStatus", + "description": "User status updated successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Update the status of the user.", + "body": { + "type": "userStatus_form", + "attributes": [] + } + }, + { + "method": "DELETE", + "path": "/users/:id", + "parameters": [ + { + "name": "id", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "unit", + "description": "User deleted successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Mark a user as deleted. The user cannot be accessed by the api, but will stay in the db for a while, before completely removed." + } + ], + "attributes": [], + "path": "/users", + "description": "Access to the user management." + }, + { + "type": "userLogin", + "plural": "userLogins", + "operations": [ + { + "method": "POST", + "path": "/login/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "userLogin", + "description": "User authenticated successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Login user with username + password.", + "body": { + "type": "userLogin_form", + "attributes": [] + } + } + ], + "attributes": [], + "path": "/login", + "description": "Resource for authenticating users." + } + ], + "attributes": [], + "annotations": [ + { + "name": "personal_data", + "description": "Identifies a field that contains Personal Data, as defined by GDPR." + } + ], + "description": "Service for user management and authentication." + } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 367a65c..cb66196 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,11 @@ "version": "6.2.0", "resolved": "https://nexus.nclazz.de/repository/npm_public/camelcase/-/camelcase-6.2.0.tgz", "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==" + }, + "mustache": { + "version": "4.2.0", + "resolved": "https://nexus.nclazz.de/repository/npm_public/mustache/-/mustache-4.2.0.tgz", + "integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==" } } } diff --git a/package.json b/package.json index 4cfd8b9..8d69dcc 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,14 @@ "name": "java-apibuilder-generator", "version": "1.0.0", "description": "Java Generators for apibuilder.io service specs.", - "main": "index.js", + "main": "src/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Niclas Thobaben", "license": "MIT", "dependencies": { - "camelcase": "^6.2.0" + "camelcase": "^6.2.0", + "mustache": "^4.2.0" } } diff --git a/src/generators/java-models-generator.js b/src/generators/java-models-generator.js new file mode 100644 index 0000000..917c02a --- /dev/null +++ b/src/generators/java-models-generator.js @@ -0,0 +1,36 @@ +const javaModels = require('../java-model') +const Mustache = require('Mustache') +const fs = require('fs') +const path = require('path') + + + +const templateModel = fs.readFileSync(path.resolve(path.join('templates', 'lombok-model.java.mustache')), 'utf-8') +const templateEnum = fs.readFileSync(path.resolve(path.join('templates', 'lombok-enum.java.mustache')), 'utf-8') + +module.exports = (service) => { + + const models = javaModels.mapJavaModels(service.models, service.namespace) + const enums = javaModels.mapJavaEnums(service.enums, service.namespace) + + const modelFiles = models.map(model => { + return { + name: `${model.name}.java`, + dir: model.dir, + contents: Mustache.render(templateModel, { service, ...model }) + } + }) + const enumFiles = enums.map(e => { + return { + name: `${e.name}.java`, + dir: e.dir, + contents: Mustache.render(templateEnum, { service, ...e }) + } + }) + return [ + ...modelFiles, + ...enumFiles + ] + + +} \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..6cf0bb9 --- /dev/null +++ b/src/index.js @@ -0,0 +1,22 @@ +const util = require('util') +const fs = require('fs') +const path = require('path') +const modelGen = require('./generators/java-models-generator') + + + +const service = require('../examples/example.json') +const models = modelGen(service) + +// console.log(util.inspect(models, { depth: 8 })) + +const out = 'examples' + +models.forEach(file => { + let outPath = path.join(out, file.dir) + if(!fs.existsSync(outPath)) { + fs.mkdirSync(outPath, { recursive: true }) + } + outPath = path.join(outPath, file.name) + fs.writeFileSync(outPath, file.contents) +}) \ No newline at end of file diff --git a/src/java-model.js b/src/java-model.js index a6ca523..79f1e29 100644 --- a/src/java-model.js +++ b/src/java-model.js @@ -7,6 +7,7 @@ module.exports = { return models.map(model => { model.name = utils.pascalcase(model.name) model.package = `${namespace}.models`; + model.dir = model.package.replace(/\./g, '/') model.fields = model.fields.map(field => { field.name = utils.camelcase(field.name) field.type = javaType.mapJavaType(field.type, namespace) @@ -28,8 +29,10 @@ module.exports = { mapJavaEnums: (enums, namespace) => { return enums.map(e => { e['values'][ e['values'].length - 1 ].last = true; + e.paramName = e.name e.name = utils.pascalcase(e.name) e.package = `${namespace}.models` + e.dir = e.package.replace(/\./g, '/') e.values = e.values.map(value => { value.value = value.value || value.name value.name = value.name.toUpperCase() diff --git a/templates/lombok-enum.java.mustache b/templates/lombok-enum.java.mustache new file mode 100644 index 0000000..3685638 --- /dev/null +++ b/templates/lombok-enum.java.mustache @@ -0,0 +1,40 @@ +/** + * Auto-generated from apibuilder.io service spec. + * apidoc-version : {{service.apidoc.version}} + * organisation : {{service.organization.key}} + * + * Documentation at: + * https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}} + * +**/ +package {{package}}; + +public enum {{name}} { + + {{#values}} + {{#description}}/** {{.}} **/{{/description}} + {{name}}("{{value}}"){{#last}}; {{/last}}{{^last}}, {{/last}} + + {{/values}} + + + private final String value; + + {{name}}(String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } + + public static {{name}} forValue(String value) { + for({{name}} {{paramName}} : {{name}}.values()) { + if({{paramName}}.value.equals(value)) { + return {{paramName}}; + } + } + throw new IllegalArgumentException("{{name}} '" + value + "' does not exist!"); + } + +} \ No newline at end of file diff --git a/templates/lombok-model.java.mustache b/templates/lombok-model.java.mustache new file mode 100644 index 0000000..c0762ab --- /dev/null +++ b/templates/lombok-model.java.mustache @@ -0,0 +1,43 @@ +/** + * Auto-generated from apibuilder.io service specification. + * apidoc-version : {{service.apidoc.version}} + * organisation : {{service.organization.key}} + * service-version : {{service.version}} + * + * Documentation at: + * https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}} + * +**/ +package {{model.package}}; + +import lombok.Builder; +import lombok.With; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NotNull; + +{{#imports}} +import {{{.}}}; +{{/imports}} + +/** + * {{model.description}} + * See https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}}#model-{{model.name}} + * +**/ +@With +@Getter +@Setter +@EqualsAndHashCode +@Builder(toBuilder = true) +@NoArgsConstructor +public class {{name}} { + + {{#fields}} + {{#description}}/** {{.}} **/{{/description}} + {{#required}}@NotNull{{/required}} + private {{{type.name}}} {{name}}; + + {{/fields}} + +} \ No newline at end of file