From 8a75fd703eff03696fa58a7776e552852d2d4e66 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:16:40 +0100 Subject: [PATCH 01/18] chore: create parameter array metadata Signed-off-by: Otavio Santana --- .../lite/mapping/entities/record/Room.java | 2 + .../parameter_array_metadata.mustache | 150 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java create mode 100644 jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java new file mode 100644 index 00000000..1f05f3da --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java @@ -0,0 +1,2 @@ +package org.eclipse.jnosql.lite.mapping.entities.record;public record Room() { +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache new file mode 100644 index 00000000..6936e1e3 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2023 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package {{packageName}}; + +import java.lang.annotation.Annotation; + +import org.eclipse.jnosql.communication.Value; +import jakarta.nosql.AttributeConverter; +import org.eclipse.jnosql.mapping.metadata.ArrayParameterMetaData; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.communication.TypeSupplier; +import org.eclipse.jnosql.communication.TypeReference; + +import javax.annotation.processing.Generated; +import java.util.Map; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@Generated(value= "JNoSQL Lite ArrayParameterMetaData Generator", date = "{{now}}") +public final class {{className}} implements ArrayParameterMetaData { + + private static final TypeReference> TYPE_SUPPLIER = new TypeReference<>() {}; + + private final AttributeConverter converter; + + private final Class> typeConverter; + + private final Map, String> valueByAnnotation; + + private final TypeSupplier<{{{supplierElement}}}> typeSupplier = new TypeReference<>(){}; + + public {{className}}() { + this.converter = {{converter}}; + this.typeConverter = {{typeConverter}}; + {{#valueByAnnotation.isEmpty}} + this.valueByAnnotation = java.util.Collections.emptyMap(); + {{/valueByAnnotation.isEmpty}} + {{^valueByAnnotation.isEmpty}} + this.valueByAnnotation = new java.util.HashMap<>(); + {{/valueByAnnotation.isEmpty}} + {{#valueByAnnotation}} + this.valueByAnnotation.put({{key}}, "{{value}}"); + {{/valueByAnnotation}} + } + + @Override + public boolean isId() { + return {{id}}; + } + + @Override + public Optional udt() { + return {{udt}}; + } + + @Override + public Optional value(Class type) { + Objects.requireNonNull(type, "type is required"); + return Optional.ofNullable(this.valueByAnnotation.get(type)); + } + + @Override + public > Optional> converter() { + return Optional.ofNullable((Class) typeConverter); + } + + @Override + public > Optional newConverter() { + return (Optional) Optional.ofNullable(converter); + } + + @Override + public MappingType mappingType() { + return {{mappingType}}; + } + + @Override + public String name() { + return "{{name}}"; + } + + @Override + public String fieldName() { + return "{{fieldName}}"; + } + + @Override + public Object value(Value value) { + Objects.requireNonNull(value, "value is required"); + if(value.get() instanceof Iterable) { + return value.get(TYPE_SUPPLIER).toArray(); + } else { + return Value.of(java.util.Collections.singletonList(value.get())).get(TYPE_SUPPLIER); + } + } + + @Override + public void write(Object bean, Object value) { + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} + } + + @Override + public Object read(Object bean) { + return (({{entity}}) bean).{{reader}}(); + } + + @Override + public Class type() { + return {{type}}.class; + } + + @Override + public boolean isEmbeddable() { + return {{embeddable}}; + } + + @Override + public Class elementType() { + return {{elementType}}; + } + + @Override + public Object arrayInstance(java.util.Collection collection) { + var array = new {{newArrayInstance}}; + int index = 0; + for (Object item : collection) { + array[index++] = ({{arrayElement}}) item; + } + return array; + } + +} From 1f93d8ce6a61ad5c272118580a8f4e6cbd057096 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:16:56 +0100 Subject: [PATCH 02/18] chore: create collection metadata Signed-off-by: Otavio Santana --- .../parameter_collection_metadata.mustache | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache new file mode 100644 index 00000000..5a171158 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2023 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package {{packageName}}; + +import java.lang.annotation.Annotation; + +import org.eclipse.jnosql.communication.Value; +import jakarta.nosql.AttributeConverter; +import org.eclipse.jnosql.mapping.metadata.CollectionParameterMetaData; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.communication.TypeSupplier; +import org.eclipse.jnosql.communication.TypeReference; + +import javax.annotation.processing.Generated; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +@Generated(value= "JNoSQL Lite CollectionParameterMetaData Generator", date = "{{now}}") +public final class {{className}} implements CollectionParameterMetaData { + + private final AttributeConverter converter; + + private final Class> typeConverter; + + private final Map, String> valueByAnnotation; + + private final TypeSupplier<{{{supplierElement}}}> typeSupplier = new TypeReference<>(){}; + + public {{className}}() { + this.converter = {{converter}}; + this.typeConverter = {{typeConverter}}; + {{#valueByAnnotation.isEmpty}} + this.valueByAnnotation = java.util.Collections.emptyMap(); + {{/valueByAnnotation.isEmpty}} + {{^valueByAnnotation.isEmpty}} + this.valueByAnnotation = new java.util.HashMap<>(); + {{/valueByAnnotation.isEmpty}} + {{#valueByAnnotation}} + this.valueByAnnotation.put({{key}}, "{{value}}"); + {{/valueByAnnotation}} + } + + @Override + public boolean isId() { + return {{id}}; + } + + @Override + public Optional udt() { + return {{udt}}; + } + + @Override + public Optional value(Class type) { + Objects.requireNonNull(type, "type is required"); + return Optional.ofNullable(this.valueByAnnotation.get(type)); + } + + @Override + public > Optional> converter() { + return Optional.ofNullable((Class) typeConverter); + } + + @Override + public > Optional newConverter() { + return (Optional) Optional.ofNullable(converter); + } + + @Override + public MappingType mappingType() { + return {{mappingType}}; + } + + @Override + public String name() { + return "{{name}}"; + } + + @Override + public String fieldName() { + return "{{fieldName}}"; + } + + @Override + public Object value(Value value) { + Objects.requireNonNull(value, "value is required"); + if(value.get() instanceof Iterable) { + return value.get(typeSupplier); + } else { + return Value.of(java.util.Collections.singletonList(value.get())).get(typeSupplier); + } + } + + @Override + public void write(Object bean, Object value) { + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} + } + + @Override + public Object read(Object bean) { + return (({{entity}}) bean).{{reader}}(); + } + + @Override + public Class type() { + return {{type}}.class; + } + + @Override + public boolean isEmbeddable() { + return {{embeddable}}; + } + + @Override + public Class elementType() { + return {{elementType}}; + } + + @Override + public java.util.Collection collectionInstance() { + return {{{collectionInstance}}}; + } + +} From a07aa0a95d623149e47ff492b96578f842f8fd0b Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:17:13 +0100 Subject: [PATCH 03/18] chore: create map parameter Signed-off-by: Otavio Santana --- .../resources/paremeter_map_metadata.mustache | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache new file mode 100644 index 00000000..570c9c87 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package {{packageName}}; + +import java.lang.annotation.Annotation; + +import org.eclipse.jnosql.communication.Value; +import jakarta.nosql.AttributeConverter; +import org.eclipse.jnosql.mapping.metadata.MapParameterMetaData; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.communication.TypeSupplier; +import org.eclipse.jnosql.communication.TypeReference; + +import javax.annotation.processing.Generated; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +@Generated(value= "JNoSQL Lite MapParameterMetaData Generator", date = "{{now}}") +public final class {{className}} implements MapParameterMetaData { + + private final AttributeConverter converter; + + private final Class> typeConverter; + + private final Map, String> valueByAnnotation; + + private final TypeSupplier<{{{supplierElement}}}> typeSupplier = new TypeReference<>(){}; + + public {{className}}() { + this.converter = {{converter}}; + this.typeConverter = {{typeConverter}}; + {{#valueByAnnotation.isEmpty}} + this.valueByAnnotation = java.util.Collections.emptyMap(); + {{/valueByAnnotation.isEmpty}} + {{^valueByAnnotation.isEmpty}} + this.valueByAnnotation = new java.util.HashMap<>(); + {{/valueByAnnotation.isEmpty}} + {{#valueByAnnotation}} + this.valueByAnnotation.put({{key}}, "{{value}}"); + {{/valueByAnnotation}} + } + + @Override + public boolean isId() { + return {{id}}; + } + + @Override + public Optional udt() { + return {{udt}}; + } + + @Override + public Optional value(Class type) { + Objects.requireNonNull(type, "type is required"); + return Optional.ofNullable(this.valueByAnnotation.get(type)); + } + + @Override + public > Optional> converter() { + return Optional.ofNullable((Class) typeConverter); + } + + @Override + public > Optional newConverter() { + return (Optional) Optional.ofNullable(converter); + } + + @Override + public MappingType mappingType() { + return {{mappingType}}; + } + + @Override + public String name() { + return "{{name}}"; + } + + @Override + public String fieldName() { + return "{{fieldName}}"; + } + + @Override + public Object value(Value value) { + Objects.requireNonNull(value, "value is required"); + return value.get(typeSupplier); + } + + @Override + public void write(Object bean, Object value) { + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} + } + + @Override + public Object read(Object bean) { + return (({{entity}}) bean).{{reader}}(); + } + + @Override + public Class type() { + return {{type}}.class; + } + + @Override + public boolean isEmbeddable() { + return {{embeddable}}; + } + + @Override + public Class keyType() { + return {{elementType}}; + } + + @Override + public Class valueType() { + return {{valueType}}; + } +} From 4dfdc647161d2ceb38317a5816292070dda39022 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:18:21 +0100 Subject: [PATCH 04/18] chore: create parameter mustache template Signed-off-by: Otavio Santana --- .../resources/parameter_metadata.mustache | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache new file mode 100644 index 00000000..8df24c32 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2023 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package {{packageName}}; + +import java.lang.annotation.Annotation; + +import org.eclipse.jnosql.communication.Value; +import jakarta.nosql.AttributeConverter; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; +import org.eclipse.jnosql.mapping.metadata.MappingType; + +import javax.annotation.processing.Generated; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +@Generated(value= "JNoSQL Lite ParameterMetaData Generator", date = "{{now}}") +public final class {{className}} implements ParameterMetaData { + + private final AttributeConverter converter; + + private final Class> typeConverter; + + private final Map, String> valueByAnnotation; + + public {{className}}() { + this.converter = {{converter}}; + this.typeConverter = {{typeConverter}}; + {{#valueByAnnotation.isEmpty}} + this.valueByAnnotation = java.util.Collections.emptyMap(); + {{/valueByAnnotation.isEmpty}} + {{^valueByAnnotation.isEmpty}} + this.valueByAnnotation = new java.util.HashMap<>(); + {{/valueByAnnotation.isEmpty}} + {{#valueByAnnotation}} + this.valueByAnnotation.put({{key}}, "{{value}}"); + {{/valueByAnnotation}} + } + + @Override + public boolean isId() { + return {{id}}; + } + + @Override + public Optional udt() { + return {{udt}}; + } + + @Override + public Optional value(Class type) { + Objects.requireNonNull(type, "type is required"); + return Optional.ofNullable(this.valueByAnnotation.get(type)); + } + + @Override + public > Optional> converter() { + return Optional.ofNullable((Class) typeConverter); + } + + @Override + public > Optional newConverter() { + return (Optional) Optional.ofNullable(converter); + } + + @Override + public MappingType mappingType() { + return {{mappingType}}; + } + + @Override + public String name() { + return "{{name}}"; + } + + @Override + public String fieldName() { + return "{{fieldName}}"; + } + + @Override + public Object value(Value value) { + Objects.requireNonNull(value, "value is required"); + return value.get({{type}}.class); + } + + @Override + public void write(Object bean, Object value) { + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} + + } + + @Override + public Object read(Object bean) { + return (({{entity}}) bean).{{reader}}(); + } + + @Override + public Class type() { + return {{type}}.class; + } + +} From b9a441adcdcc658cd8beeeba22ceda13a18a2e0c Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:18:56 +0100 Subject: [PATCH 05/18] chore: create validation when there is not setter Signed-off-by: Otavio Santana --- .../src/main/resources/field_array_metadata.mustache | 11 ++++++++--- .../main/resources/field_collection_metadata.mustache | 11 ++++++++--- .../src/main/resources/field_map_metadata.mustache | 11 ++++++++--- .../src/main/resources/field_metadata.mustache | 8 +++++++- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache index 9499c5af..d519279d 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache @@ -29,7 +29,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -@Generated(value= "JNoSQL Lite FieldMetadata Generator", date = "{{now}}") +@Generated(value= "JNoSQL Lite ArrayFieldMetadata Generator", date = "{{now}}") public final class {{className}} implements ArrayFieldMetadata { private static final TypeReference> TYPE_SUPPLIER = new TypeReference<>() {}; @@ -109,7 +109,12 @@ public final class {{className}} implements ArrayFieldMetadata { @Override public void write(Object bean, Object value) { - (({{entity}}) bean).{{writer}}(({{type}})value); + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} } @Override @@ -142,4 +147,4 @@ public final class {{className}} implements ArrayFieldMetadata { return array; } -} \ No newline at end of file +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache index 8521767b..5fd72647 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache @@ -28,7 +28,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -@Generated(value= "JNoSQL Lite FieldMetadata Generator", date = "{{now}}") +@Generated(value= "JNoSQL Lite CollectionFieldMetadata Generator", date = "{{now}}") public final class {{className}} implements CollectionFieldMetadata { private final AttributeConverter converter; @@ -106,7 +106,12 @@ public final class {{className}} implements CollectionFieldMetadata { @Override public void write(Object bean, Object value) { - (({{entity}}) bean).{{writer}}(({{type}})value); + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} } @Override @@ -134,4 +139,4 @@ public final class {{className}} implements CollectionFieldMetadata { return {{{collectionInstance}}}; } -} \ No newline at end of file +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache index a6e93af3..4b9aae0e 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache @@ -28,7 +28,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -@Generated(value= "JNoSQL Lite FieldMetadata Generator", date = "{{now}}") +@Generated(value= "JNoSQL Lite MapFieldMetadata Generator", date = "{{now}}") public final class {{className}} implements MapFieldMetadata { private final AttributeConverter converter; @@ -102,7 +102,12 @@ public final class {{className}} implements MapFieldMetadata { @Override public void write(Object bean, Object value) { - (({{entity}}) bean).{{writer}}(({{type}})value); + {{#writer}} + (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} } @Override @@ -129,4 +134,4 @@ public final class {{className}} implements MapFieldMetadata { public Class valueType() { return {{valueType}}; } -} \ No newline at end of file +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache index 5d0ec82f..bafca712 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache @@ -98,7 +98,13 @@ public final class {{className}} implements FieldMetadata { @Override public void write(Object bean, Object value) { + {{#writer}} (({{entity}}) bean).{{writer}}(({{type}})value); + {{/writer}} + {{^writer}} + throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); + {{/writer}} + } @Override @@ -111,4 +117,4 @@ public final class {{className}} implements FieldMetadata { return {{type}}.class; } -} \ No newline at end of file +} From eeb1dc8f323c14cfd9e15a265889ca0d6f788a11 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 09:19:51 +0100 Subject: [PATCH 06/18] feat: create validation to field supporting record Signed-off-by: Otavio Santana --- .../jnosql/lite/mapping/FieldAnalyzer.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/FieldAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/FieldAnalyzer.java index 57708eb3..542043ca 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/FieldAnalyzer.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/FieldAnalyzer.java @@ -45,10 +45,11 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.logging.Logger; import java.util.stream.Collectors; class FieldAnalyzer implements Supplier { - + private static final Logger LOGGER = Logger.getLogger(FieldAnalyzer.class.getName()); private static final String DEFAULT_TEMPLATE = "field_metadata.mustache"; private static final String COLLECTION_TEMPLATE = "field_collection_metadata.mustache"; private static final String MAP_TEMPLATE = "field_map_metadata.mustache"; @@ -110,9 +111,11 @@ private JavaFileObject getFileObject(FieldModel metadata, Filer filer) { private FieldModel getMetaData() { final String fieldName = field.getSimpleName().toString(); + LOGGER.finest("Processing the field: " + fieldName); final Predicate validName = el -> el.getSimpleName().toString() .contains(ProcessorUtil.capitalize(fieldName)); final Predicate hasGetterName = el -> el.equals("get" + ProcessorUtil.capitalize(fieldName)); + final Predicate hasRecordStyleName = el -> el.equals(fieldName); final Predicate hasSetterName = el -> el.equals("set" + ProcessorUtil.capitalize(fieldName)); final Predicate hasIsName = el -> el.equals("is" + ProcessorUtil.capitalize(fieldName)); @@ -167,9 +170,9 @@ private FieldModel getMetaData() { className = typeMirror.toString(); } - Column column = field.getAnnotation(Column.class); + var column = field.getAnnotation(Column.class); Id id = field.getAnnotation(Id.class); - Convert convert = field.getAnnotation(Convert.class); + var convert = field.getAnnotation(Convert.class); List valueAnnotationModels = new ArrayList<>(); for (AnnotationMirror annotationMirror : field.getAnnotationMirrors()) { @@ -197,16 +200,23 @@ private FieldModel getMetaData() { final String name = getName(fieldName, column, id); final String udt = column != null ? column.udt() : null; - final String getMethod = accessors.stream() + var isRecord = !this.entity.getRecordComponents().isEmpty(); + final String getMethod; + + if(isRecord) { + getMethod = fieldName; + } else { + getMethod = accessors.stream() .map(ELEMENT_TO_STRING) - .filter(hasGetterName) + .filter(hasGetterName.or(hasRecordStyleName)) .findFirst().orElseThrow(generateGetterError(fieldName, packageName, entityName, "There is not valid getter method to the field: ")); + } + final String setMethod = accessors.stream() .map(ELEMENT_TO_STRING) .filter(hasSetterName.or(hasIsName)) - .findFirst().orElseThrow(generateGetterError(fieldName, packageName, entityName, - "There is not valid setter method to the field: ")); + .findFirst().orElse(null); return FieldModel.builder() .packageName(packageName) From 4190d93ebd0bcf9e1af253b62e47aa240c767f4a Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 18:11:33 +0100 Subject: [PATCH 07/18] feat: create parameter constructor Signed-off-by: Otavio Santana --- .../jnosql/lite/mapping/ClassAnalyzer.java | 24 +- .../lite/mapping/ParameterAnalyzer.java | 269 ++++++++++++++++++ .../jnosql/lite/mapping/ParameterModel.java | 266 +++++++++++++++++ .../resources/field_array_metadata.mustache | 3 - .../field_collection_metadata.mustache | 3 - .../resources/field_map_metadata.mustache | 3 - .../main/resources/field_metadata.mustache | 4 - ...stache => parameter_map_metadata.mustache} | 0 .../resources/parameter_metadata.mustache | 49 ---- 9 files changed, 557 insertions(+), 64 deletions(-) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterModel.java rename jnosql-lite/mapping-lite-processor/src/main/resources/{paremeter_map_metadata.mustache => parameter_map_metadata.mustache} (100%) diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java index 230e5f98..1bab5a8a 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java @@ -17,16 +17,17 @@ import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; -import jakarta.nosql.Entity; import jakarta.nosql.DiscriminatorColumn; import jakarta.nosql.DiscriminatorValue; import jakarta.nosql.Embeddable; +import jakarta.nosql.Entity; import jakarta.nosql.Inheritance; import jakarta.nosql.MappedSuperclass; import javax.annotation.processing.Filer; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; @@ -43,6 +44,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.eclipse.jnosql.lite.mapping.ParameterAnalyzer.INJECT_CONSTRUCTOR; + class ClassAnalyzer implements Supplier { private static final Logger LOGGER = Logger.getLogger(ClassAnalyzer.class.getName()); @@ -103,9 +106,25 @@ private String analyze(TypeElement typeElement) throws IOException { .map(FieldAnalyzer::get) .collect(Collectors.toList()); + var constructor = processingEnv.getElementUtils().getAllMembers(typeElement) + .stream() + .filter(EntityProcessor.IS_CONSTRUCTOR.and(EntityProcessor.HAS_ACCESS) + .and(INJECT_CONSTRUCTOR)).findFirst(); + + if (constructor.isPresent()) { + ExecutableElement executableElement = (ExecutableElement) constructor.get(); + List parametersClasses = executableElement.getParameters().stream() + .map(p -> new ParameterAnalyzer(p, processingEnv, typeElement)) + .map(ParameterAnalyzer::get) + .toList(); + LOGGER.finest("Found the parameters: " + parametersClasses); + } EntityModel metadata = getMetadata(typeElement, fields); createClass(entity, metadata); LOGGER.info("Found the fields: " + fields); + + + return metadata.getQualified(); } @@ -134,7 +153,8 @@ private EntityModel getMetadata(TypeElement element, List fields) { .filter(v -> !v.isBlank()) .orElse(sourceClassName); String inheritanceParameter = null; - boolean notConcrete = element.getModifiers().contains(Modifier.ABSTRACT); + boolean notConcrete = element.getModifiers().contains(Modifier.ABSTRACT) + || !element.getRecordComponents().isEmpty(); if (superclass.getAnnotation(Inheritance.class) != null) { inheritanceParameter = getInheritanceParameter(element, superclass); Entity superEntity = superclass.getAnnotation(Entity.class); diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java new file mode 100644 index 00000000..4f92f6a5 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2020 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping; + +import com.github.mustachejava.DefaultMustacheFactory; +import com.github.mustachejava.Mustache; +import com.github.mustachejava.MustacheFactory; +import jakarta.nosql.Column; +import jakarta.nosql.Convert; +import jakarta.nosql.Embeddable; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; +import org.eclipse.jnosql.mapping.metadata.MappingType; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.ArrayType; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeMirror; +import javax.tools.JavaFileObject; +import java.io.IOException; +import java.io.Writer; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.logging.Logger; + +class ParameterAnalyzer implements Supplier { + + private static final Predicate ID_PARAMETER = v -> v.getAnnotation(Id.class) != null; + private static final Predicate COLUMN_PARAMETER = v -> v.getAnnotation(Column.class) != null; + + static final Predicate INJECT_CONSTRUCTOR = e -> { + ExecutableElement executableElement = (ExecutableElement) e; + return executableElement.getParameters().stream().anyMatch(ID_PARAMETER.or(COLUMN_PARAMETER)); + }; + + + private static final Logger LOGGER = Logger.getLogger(ParameterAnalyzer.class.getName()); + private static final String DEFAULT_TEMPLATE = "parameter_metadata.mustache"; + private static final String COLLECTION_TEMPLATE = "parameter_collection_metadata.mustache"; + private static final String MAP_TEMPLATE = "parameter_map_metadata.mustache"; + private static final String ARRAY_TEMPLATE = "parameter_array_metadata.mustache"; + private static final String NULL = "null"; + private final VariableElement parameter; + private final Mustache template; + + private final Mustache collectionTemplate; + private final Mustache mapTemplate; + private final Mustache arrayTemplate; + private final ProcessingEnvironment processingEnv; + private final TypeElement entity; + + ParameterAnalyzer(VariableElement parameter, ProcessingEnvironment processingEnv, + TypeElement entity) { + this.parameter = parameter; + this.processingEnv = processingEnv; + this.entity = entity; + this.template = createTemplate(DEFAULT_TEMPLATE); + this.collectionTemplate = createTemplate(COLLECTION_TEMPLATE); + this.mapTemplate = createTemplate(MAP_TEMPLATE); + this.arrayTemplate = createTemplate(ARRAY_TEMPLATE); + } + + @Override + public String get() { + var metadata = getMetaData(); + Filer filer = processingEnv.getFiler(); + JavaFileObject fileObject = getFileObject(metadata, filer); + try (Writer writer = fileObject.openWriter()) { + if (NULL.equals(metadata.getElementType())) { + template.execute(writer, metadata); + } else if (metadata.getType().contains("Map")) { + mapTemplate.execute(writer, metadata); + } else if(metadata.getType().contains("[]")) { + arrayTemplate.execute(writer, metadata); + } else { + collectionTemplate.execute(writer, metadata); + } + } catch (IOException exception) { + throw new ValidationException("An error to compile the class: " + + metadata.getQualified(), exception); + } + return metadata.getQualified(); + } + + private JavaFileObject getFileObject(ParameterModel metadata, Filer filer) { + try { + return filer.createSourceFile(metadata.getQualified(), entity); + } catch (IOException exception) { + throw new ValidationException("An error to create the class: " + + metadata.getQualified(), exception); + } + + } + + private ParameterModel getMetaData() { + final String fieldName = parameter.getSimpleName().toString(); + LOGGER.finest("Processing the parameter: " + fieldName); + + final TypeMirror typeMirror = parameter.asType(); + String className; + String elementType = NULL; + String valuetype = NULL; + boolean embeddable = false; + String collectionInstance = CollectionUtil.DEFAULT; + MappingType mappingType = MappingType.DEFAULT; + String supplierElement = null; + String newArrayInstance = null; + String arrayElement = null; + + + if (typeMirror instanceof DeclaredType declaredType) { + Element element = declaredType.asElement(); + className = element.toString(); + supplierElement = typeMirror.toString(); + embeddable = isEmbeddable(declaredType); + collectionInstance = CollectionUtil.INSTANCE.apply(className); + elementType = elementType(declaredType); + valuetype = valuetype(declaredType); + mappingType = of(element, collectionInstance); + + } else if (typeMirror instanceof ArrayType arrayType) { + TypeMirror componentType = arrayType.getComponentType(); + mappingType = MappingType.ARRAY; + className = typeMirror.toString(); + collectionInstance = CollectionUtil.DEFAULT; + + if (componentType instanceof DeclaredType declaredType) { + var element = declaredType.asElement(); + supplierElement = componentType.toString(); + embeddable = element.getAnnotation(Entity.class) != null || element.getAnnotation(Embeddable.class) != null; + elementType = element + ".class"; + arrayElement = element.toString(); + newArrayInstance = className.replace("[]", "[collection.size()]"); + } else { + className = typeMirror.toString(); + supplierElement = typeMirror.toString(); + arrayElement = componentType.toString(); + elementType = componentType + ".class"; + newArrayInstance = className.replace("[]", "[collection.size()]"); + } + } else { + className = typeMirror.toString(); + } + + var column = parameter.getAnnotation(Column.class); + Id id = parameter.getAnnotation(Id.class); + var convert = parameter.getAnnotation(Convert.class); + + for (AnnotationMirror annotationMirror : parameter.getAnnotationMirrors()) { + DeclaredType annotationType = annotationMirror.getAnnotationType(); + Map elementValues = annotationMirror.getElementValues(); + Predicate> isValueMethod = + e -> e.getKey().toString().equals("value()"); + + List defaultJNoSQL = List.of(Column.class.getName(), + Id.class.getName(), + Convert.class.getName()); + Predicate> isNotDefaultAnnotation = e -> ! + defaultJNoSQL.contains(annotationType.toString()); + elementValues.entrySet().stream().filter(isNotDefaultAnnotation.and(isValueMethod)).findFirst().ifPresent(e -> { + }); + } + + final boolean isId = id != null; + final String packageName = ProcessorUtil.getPackageName(entity); + final String entityName = ProcessorUtil.getSimpleNameAsString(this.entity); + final String name = getName(fieldName, column, id); + final String udt = column != null ? column.udt() : null; + + return ParameterModel.builder() + .packageName(packageName) + .name(name) + .type(className) + .entity(entityName) + .fieldName(fieldName) + .udt(udt) + .id(isId) + .elementType(elementType) + .valueType(valuetype) + .converter(convert) + .embeddable(embeddable) + .mappingType("MappingType." + mappingType.name()) + .collectionInstance(collectionInstance) + .supplierElement(supplierElement) + .newArrayInstance(newArrayInstance) + .arrayElement(arrayElement) + .build(); + } + + private String getName(String fieldName, Column column, Id id) { + if (id == null) { + return column.value().isBlank() ? fieldName : column.value(); + } else { + return id.value().isBlank() ? fieldName : id.value(); + } + } + + private Mustache createTemplate(String template) { + MustacheFactory factory = new DefaultMustacheFactory(); + return factory.compile(template); + } + + private static MappingType of(Element element, String collection) { + if (element.getAnnotation(Embeddable.class) != null) { + var type = element.getAnnotation(Embeddable.class).value(); + return Embeddable.EmbeddableType.FLAT.equals(type) ? MappingType.EMBEDDED : MappingType.EMBEDDED_GROUP; + } + if (element.getAnnotation(Entity.class) != null) { + return MappingType.ENTITY; + } + if (!collection.equals(CollectionUtil.DEFAULT)) { + return MappingType.COLLECTION; + } + if (element.toString().equals("java.util.Map")) { + return MappingType.MAP; + } + return MappingType.DEFAULT; + } + + private String elementType(DeclaredType declaredType) { + Optional genericMirrorOptional = declaredType.getTypeArguments().stream().findFirst(); + if (genericMirrorOptional.isPresent()) { + TypeMirror genericMirror = genericMirrorOptional.get(); + return genericMirror + ".class"; + } else { + return NULL; + } + } + + private String valuetype(DeclaredType declaredType) { + Optional genericMirrorOptional = declaredType.getTypeArguments().stream().skip(1L).findFirst(); + if (genericMirrorOptional.isPresent()) { + TypeMirror genericMirror = genericMirrorOptional.get(); + return genericMirror + ".class"; + } else { + return NULL; + } + } + + private boolean isEmbeddable(DeclaredType declaredType) { + return declaredType.getTypeArguments().stream() + .filter(DeclaredType.class::isInstance).map(DeclaredType.class::cast) + .map(DeclaredType::asElement).findFirst().map(e -> e.getAnnotation(Embeddable.class) != null || + e.getAnnotation(Entity.class) != null).orElse(false); + } + +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterModel.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterModel.java new file mode 100644 index 00000000..02f55c57 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterModel.java @@ -0,0 +1,266 @@ +/* + * Copyright (c) 2020 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping; + +import jakarta.nosql.Convert; + +import javax.lang.model.type.MirroredTypeException; +import javax.lang.model.type.TypeMirror; +import java.util.Objects; + +final class ParameterModel extends BaseMappingModel { + + private String packageName; + private String name; + private String type; + private String entity; + private String fieldName; + private boolean id; + private String converter; + + private String mappingType; + + private String typeConverter; + + private String elementType; + private String valueType; + private boolean embeddable; + + private String collectionInstance; + + private String supplierElement; + + private String udt; + + private String newArrayInstance; + + private String arrayElement; + + private ParameterModel() { + } + + public String getPackageName() { + return packageName; + } + + public String getName() { + return name; + } + + public String getType() { + return type; + } + + public String getEntity() { + return entity; + } + + public String getQualified() { + return packageName + "." + getClassName(); + } + + public String getClassName() { + return entity + ProcessorUtil.capitalize(fieldName) + "ParameterMetaData"; + } + + public String getFieldName() { + return fieldName; + } + + public boolean isId() { + return id; + } + + public String getValueType() { + return valueType; + } + + public String getConverter() { + return converter; + } + + public String getMappingType() { + return mappingType; + } + + public String getTypeConverter() { + return typeConverter; + } + + public String getElementType() { + return elementType; + } + + public boolean isEmbeddable() { + return embeddable; + } + + public String getCollectionInstance() { + return collectionInstance; + } + + public String getSupplierElement() { + return supplierElement; + } + + public String getUdt() { + return udt; + } + + public String getNewArrayInstance() { + return newArrayInstance; + } + + public String getArrayElement() { + return arrayElement; + } + + @Override + public String toString() { + return "ParameterModel{" + + "packageName='" + packageName + '\'' + + ", name='" + name + '\'' + + ", type='" + type + '\'' + + ", entity='" + entity + '\'' + + ", fieldName='" + fieldName + '\'' + + ", id=" + id + + ", converter='" + converter + '\'' + + ", mappingType='" + mappingType + '\'' + + ", typeConverter='" + typeConverter + '\'' + + ", elementType='" + elementType + '\'' + + ", valueType='" + valueType + '\'' + + ", embeddable=" + embeddable + + ", collectionInstance='" + collectionInstance + '\'' + + ", supplierElement='" + supplierElement + '\'' + + ", udt='" + udt + '\'' + + ", newArrayInstance='" + newArrayInstance + '\'' + + '}'; + } + + public static ParameterMetaDataBuilder builder() { + return new ParameterMetaDataBuilder(); + } + + + public static class ParameterMetaDataBuilder { + + private final ParameterModel fieldModel; + + private ParameterMetaDataBuilder() { + this.fieldModel = new ParameterModel(); + this.fieldModel.converter = "null"; + this.fieldModel.mappingType = "null"; + this.fieldModel.typeConverter = "null"; + this.fieldModel.udt = "Optional.empty()"; + } + + public ParameterMetaDataBuilder packageName(String packageName) { + this.fieldModel.packageName = packageName; + return this; + } + + public ParameterMetaDataBuilder name(String name) { + this.fieldModel.name = name; + return this; + } + + public ParameterMetaDataBuilder type(String type) { + this.fieldModel.type = type; + return this; + } + + public ParameterMetaDataBuilder entity(String entity) { + this.fieldModel.entity = entity; + return this; + } + + public ParameterMetaDataBuilder fieldName(String fieldName) { + this.fieldModel.fieldName = fieldName; + return this; + } + + public ParameterMetaDataBuilder udt(String udt) { + if(udt != null && !udt.isEmpty() && !udt.isBlank()) { + this.fieldModel.udt = String.format("Optional.of(\"%s\")", udt); + } + return this; + } + + public ParameterMetaDataBuilder id(boolean id) { + this.fieldModel.id = id; + return this; + } + + public ParameterMetaDataBuilder converter(Convert converter) { + if (Objects.nonNull(converter)) { + try { + this.fieldModel.converter = String.format("new %s();", converter.value().getName()); + this.fieldModel.typeConverter = converter.value().getName().concat(".class"); + } catch (MirroredTypeException exception) { + TypeMirror typeMirror = exception.getTypeMirror(); + this.fieldModel.converter = String.format("new %s()", typeMirror); + this.fieldModel.typeConverter = typeMirror.toString().concat(".class"); + } + + } + return this; + } + + public ParameterMetaDataBuilder mappingType(String mappingType) { + this.fieldModel.mappingType = mappingType; + return this; + } + + public ParameterMetaDataBuilder elementType(String elementType) { + this.fieldModel.elementType = elementType; + return this; + } + + public ParameterMetaDataBuilder valueType(String valueType) { + this.fieldModel.valueType = valueType; + return this; + } + + public ParameterMetaDataBuilder embeddable(boolean embeddable) { + this.fieldModel.embeddable = embeddable; + return this; + } + + public ParameterMetaDataBuilder collectionInstance(String collectionInstance) { + this.fieldModel.collectionInstance = collectionInstance; + return this; + } + + public ParameterMetaDataBuilder supplierElement(String supplierElement) { + this.fieldModel.supplierElement = supplierElement; + return this; + } + + public ParameterMetaDataBuilder newArrayInstance(String newArrayInstance) { + this.fieldModel.newArrayInstance = newArrayInstance; + return this; + } + + public ParameterMetaDataBuilder arrayElement(String arrayElement) { + this.fieldModel.arrayElement = arrayElement; + return this; + } + + + public ParameterModel build() { + return fieldModel; + } + } +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache index d519279d..3b3186ba 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_array_metadata.mustache @@ -112,9 +112,6 @@ public final class {{className}} implements ArrayFieldMetadata { {{#writer}} (({{entity}}) bean).{{writer}}(({{type}})value); {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} } @Override diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache index 5fd72647..dc2a67b5 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_collection_metadata.mustache @@ -109,9 +109,6 @@ public final class {{className}} implements CollectionFieldMetadata { {{#writer}} (({{entity}}) bean).{{writer}}(({{type}})value); {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} } @Override diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache index 4b9aae0e..14230c34 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_map_metadata.mustache @@ -105,9 +105,6 @@ public final class {{className}} implements MapFieldMetadata { {{#writer}} (({{entity}}) bean).{{writer}}(({{type}})value); {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} } @Override diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache index bafca712..7a4232f0 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/field_metadata.mustache @@ -101,10 +101,6 @@ public final class {{className}} implements FieldMetadata { {{#writer}} (({{entity}}) bean).{{writer}}(({{type}})value); {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} - } @Override diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache similarity index 100% rename from jnosql-lite/mapping-lite-processor/src/main/resources/paremeter_map_metadata.mustache rename to jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache index 8df24c32..190b8508 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache @@ -33,20 +33,10 @@ public final class {{className}} implements ParameterMetaData { private final Class> typeConverter; - private final Map, String> valueByAnnotation; public {{className}}() { this.converter = {{converter}}; this.typeConverter = {{typeConverter}}; - {{#valueByAnnotation.isEmpty}} - this.valueByAnnotation = java.util.Collections.emptyMap(); - {{/valueByAnnotation.isEmpty}} - {{^valueByAnnotation.isEmpty}} - this.valueByAnnotation = new java.util.HashMap<>(); - {{/valueByAnnotation.isEmpty}} - {{#valueByAnnotation}} - this.valueByAnnotation.put({{key}}, "{{value}}"); - {{/valueByAnnotation}} } @Override @@ -54,17 +44,6 @@ public final class {{className}} implements ParameterMetaData { return {{id}}; } - @Override - public Optional udt() { - return {{udt}}; - } - - @Override - public Optional value(Class type) { - Objects.requireNonNull(type, "type is required"); - return Optional.ofNullable(this.valueByAnnotation.get(type)); - } - @Override public > Optional> converter() { return Optional.ofNullable((Class) typeConverter); @@ -85,36 +64,8 @@ public final class {{className}} implements ParameterMetaData { return "{{name}}"; } - @Override - public String fieldName() { - return "{{fieldName}}"; - } - - @Override - public Object value(Value value) { - Objects.requireNonNull(value, "value is required"); - return value.get({{type}}.class); - } - - @Override - public void write(Object bean, Object value) { - {{#writer}} - (({{entity}}) bean).{{writer}}(({{type}})value); - {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} - - } - - @Override - public Object read(Object bean) { - return (({{entity}}) bean).{{reader}}(); - } - @Override public Class type() { return {{type}}.class; } - } From 7e3b272ed6d93eb43e4069880537f934dbf04cae Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 26 Oct 2024 18:42:29 +0100 Subject: [PATCH 08/18] feat: create default constructor Signed-off-by: Otavio Santana --- .../jnosql/lite/mapping/ClassAnalyzer.java | 24 +++++++-- .../lite/mapping/ConstructorMetamodel.java | 53 +++++++++++++++++++ .../jnosql/lite/mapping/EntityModel.java | 18 +++++-- ...a.java => DefaultConstructorMetadata.java} | 14 ++--- .../resources/constructor_metadata.mustache | 44 +++++++++++++++ .../main/resources/entity_metadata.mustache | 9 +++- 6 files changed, 146 insertions(+), 16 deletions(-) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java rename jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/{LiteConstructorMetadata.java => DefaultConstructorMetadata.java} (73%) create mode 100644 jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java index 1bab5a8a..1e0bf7bc 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java @@ -50,18 +50,21 @@ class ClassAnalyzer implements Supplier { private static final Logger LOGGER = Logger.getLogger(ClassAnalyzer.class.getName()); private static final String NEW_INSTANCE = "entity_metadata.mustache"; + private static final String INJECTABLE_CONSTRUCTOR = "constructor_metadata.mustache"; private final Element entity; private final ProcessingEnvironment processingEnv; private final Mustache template; + private final Mustache constructorTemplate; ClassAnalyzer(Element entity, ProcessingEnvironment processingEnv) { this.entity = entity; this.processingEnv = processingEnv; MustacheFactory factory = new DefaultMustacheFactory(); this.template = factory.compile(NEW_INSTANCE); + this.constructorTemplate = factory.compile(INJECTABLE_CONSTRUCTOR); } @Override @@ -110,6 +113,7 @@ private String analyze(TypeElement typeElement) throws IOException { .stream() .filter(EntityProcessor.IS_CONSTRUCTOR.and(EntityProcessor.HAS_ACCESS) .and(INJECT_CONSTRUCTOR)).findFirst(); + String constructorClassName = null; if (constructor.isPresent()) { ExecutableElement executableElement = (ExecutableElement) constructor.get(); @@ -118,8 +122,13 @@ private String analyze(TypeElement typeElement) throws IOException { .map(ParameterAnalyzer::get) .toList(); LOGGER.finest("Found the parameters: " + parametersClasses); + var constructorMetamodel = ConstructorMetamodel.of(ProcessorUtil.getPackageName(typeElement), + ProcessorUtil.getSimpleNameAsString(typeElement), parametersClasses); + + createConstructors(entity, constructorMetamodel); + constructorClassName = constructorMetamodel.getQualified(); } - EntityModel metadata = getMetadata(typeElement, fields); + EntityModel metadata = getMetadata(typeElement, fields, constructorClassName); createClass(entity, metadata); LOGGER.info("Found the fields: " + fields); @@ -136,7 +145,15 @@ private void createClass(Element entity, EntityModel metadata) throws IOExceptio } } - private EntityModel getMetadata(TypeElement element, List fields) { + private void createConstructors(Element entity, ConstructorMetamodel metadata) throws IOException { + Filer filer = processingEnv.getFiler(); + JavaFileObject fileObject = filer.createSourceFile(metadata.getQualified(), entity); + try (Writer writer = fileObject.openWriter()) { + constructorTemplate.execute(writer, metadata); + } + } + + private EntityModel getMetadata(TypeElement element, List fields, String constructorClassName) { TypeElement superclass = (TypeElement) ((DeclaredType) element.getSuperclass()).asElement(); @@ -147,7 +164,6 @@ private EntityModel getMetadata(TypeElement element, List fields) { String packageName = ProcessorUtil.getPackageName(element); String sourceClassName = ProcessorUtil.getSimpleNameAsString(element); - String entityName = Optional.ofNullable(annotation) .map(Entity::value) .filter(v -> !v.isBlank()) @@ -163,7 +179,7 @@ private EntityModel getMetadata(TypeElement element, List fields) { inheritanceParameter = getInheritanceParameter(element, element); } return new EntityModel(packageName, sourceClassName, entityName, fields, embedded, notConcrete, - inheritanceParameter, entityAnnotation, hasInheritanceAnnotation); + inheritanceParameter, entityAnnotation, hasInheritanceAnnotation, constructorClassName); } private String getInheritanceParameter(TypeElement element, TypeElement superclass) { diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java new file mode 100644 index 00000000..8bc38b0d --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping; + +import java.util.List; + +public class ConstructorMetamodel { + + private final String packageName; + + private final String className; + + private final List parameters; + + private ConstructorMetamodel(String packageName, String className, List parameters) { + this.packageName = packageName; + this.className = className; + this.parameters = parameters; + } + + public String getPackageName() { + return packageName; + } + + public String getClassName() { + return className + "ConstructorMetadata"; + } + + public List getParameters() { + return parameters; + } + + public String getQualified() { + return packageName + "." + getClassName(); + } + + + public static ConstructorMetamodel of(String packageName, String className, List parameters) { + return new ConstructorMetamodel(packageName, className, parameters); + } +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/EntityModel.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/EntityModel.java index bef3b1cb..ceeb2398 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/EntityModel.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/EntityModel.java @@ -34,10 +34,17 @@ final class EntityModel extends BaseMappingModel { private final boolean entityAnnotation; private final boolean hasInheritanceAnnotation; + + private final String constructorClassName; + EntityModel(String packageName, String entity, String name, - List fields, boolean embedded, boolean notConcrete, - String inheritanceParameter, - boolean entityAnnotation, boolean hasInheritanceAnnotation) { + List fields, + boolean embedded, + boolean notConcrete, + String inheritanceParameter, + boolean entityAnnotation, + boolean hasInheritanceAnnotation, + String constructorClassName) { this.packageName = packageName; this.entity = entity; this.name = name; @@ -47,6 +54,7 @@ final class EntityModel extends BaseMappingModel { this.inheritanceParameter = inheritanceParameter; this.entityAnnotation = entityAnnotation; this.hasInheritanceAnnotation = hasInheritanceAnnotation; + this.constructorClassName = constructorClassName; } public String getPackageName() { @@ -97,6 +105,10 @@ public boolean isHasInheritanceAnnotation() { return hasInheritanceAnnotation; } + public String getConstructorClassName() { + return constructorClassName; + } + @Override public String toString() { return "EntityModel{" + diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/DefaultConstructorMetadata.java similarity index 73% rename from jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java rename to jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/DefaultConstructorMetadata.java index f60da241..bca5d69d 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/DefaultConstructorMetadata.java @@ -26,17 +26,17 @@ /** * The lite implementation of {@link ConstructorMetadata} */ -public class LiteConstructorMetadata implements ConstructorMetadata { +public class DefaultConstructorMetadata implements ConstructorMetadata { /** * The empty instance */ - public static final ConstructorMetadata EMPTY = new LiteConstructorMetadata(true, emptyList()); + public static final ConstructorMetadata EMPTY = new DefaultConstructorMetadata(true, emptyList()); private final boolean defaultConstructor; private final List parameters; - private LiteConstructorMetadata(boolean defaultConstructor, List parameters) { + private DefaultConstructorMetadata(boolean defaultConstructor, List parameters) { this.defaultConstructor = defaultConstructor; this.parameters = parameters; } @@ -52,14 +52,14 @@ public boolean isDefault() { } /** - * Creates a {@link LiteConstructorMetadata} instance + * Creates a {@link DefaultConstructorMetadata} instance * @param defaultConstructor if the constructor is the default * @param parameters the parameters - * @return a {@link LiteConstructorMetadata} instance + * @return a {@link DefaultConstructorMetadata} instance * @throws NullPointerException when there is null parameter */ - public static LiteConstructorMetadata of(boolean defaultConstructor, List parameters) { + public static DefaultConstructorMetadata of(boolean defaultConstructor, List parameters) { Objects.requireNonNull(parameters, "parameters is required"); - return new LiteConstructorMetadata(defaultConstructor, parameters); + return new DefaultConstructorMetadata(defaultConstructor, parameters); } } diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache new file mode 100644 index 00000000..f49c2d93 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2023 Otávio Santana and others +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License v1.0 +* and Apache License v2.0 which accompanies this distribution. +* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html +* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. +* +* You may elect to redistribute this code under either of these licenses. +* +* Contributors: +* +* Otavio Santana +*/ +package {{packageName}}; + +import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; + +import javax.annotation.processing.Generated; +import java.util.List; +import java.util.ArrayList; + +@Generated(value= "JNoSQL Lite ConstructorMetadata Generator", date = "{{now}}") +public final class {{className}} implements ConstructorMetadata { + + private final List parameters = new ArrayList<>(); + + public {{className}}() { + {{#parameters}} + this.parameters.add(new {{.}}()); + {{/parameters}} + } + + @Override + public boolean isDefault() { + return false; + } + + @Override + public List parameters() { + return parameters; + } +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache index 92d712e9..76e432e7 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache @@ -17,7 +17,7 @@ package {{packageName}}; import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.FieldMetadata; import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata; -import org.eclipse.jnosql.lite.mapping.metadata.LiteConstructorMetadata; +import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.lite.mapping.metadata.LiteEntityMetadata; import java.util.ArrayList; @@ -55,7 +55,12 @@ public final class {{className}} implements LiteEntityMetadata { {{^inheritanceParameter}} this.inheritance = null; {{/inheritanceParameter}} + {{#constructorClassName}} + this.constructor = new {{constructorClassName}}(); + {{/constructorClassName}} + {{^constructorClassName}} this.constructor = LiteConstructorMetadata.EMPTY; + {{/constructorClassName}} } @Override @@ -156,4 +161,4 @@ public final class {{className}} implements LiteEntityMetadata { return Optional.ofNullable(inheritance); } -} \ No newline at end of file +} From 2430031eab48fcb42f31dfb648d63ed77ed42dd7 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 07:13:46 +0000 Subject: [PATCH 09/18] feat: create test to constructor Signed-off-by: Otavio Santana --- .../lite/mapping/entities/record/Guest.java | 24 ++++ .../lite/mapping/entities/record/Hotel.java | 11 ++ .../lite/mapping/entities/record/Room.java | 23 ++- .../mapping/entities/record/GuestTest.java | 136 ++++++++++++++++++ .../mapping/entities/record/RoomTest.java | 120 ++++++++++++++++ .../jnosql/lite/mapping/ClassAnalyzer.java | 7 +- .../lite/mapping/ConstructorMetamodel.java | 28 +++- .../lite/mapping/ParameterAnalyzer.java | 7 +- .../jnosql/lite/mapping/ParameterResult.java | 18 +++ .../metadata/LiteConstructorBuilder.java | 52 +++++++ .../LiteConstructorBuilderSupplier.java | 10 +- .../metadata/LiteConstructorMetadata.java | 22 +++ .../resources/constructor_metadata.mustache | 9 +- .../main/resources/entity_metadata.mustache | 4 +- .../parameter_array_metadata.mustache | 46 ------ .../parameter_collection_metadata.mustache | 45 ------ .../resources/parameter_map_metadata.mustache | 36 ----- .../LiteConstructorBuilderSupplierTest.java | 4 +- 18 files changed, 457 insertions(+), 145 deletions(-) create mode 100644 jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java create mode 100644 jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java create mode 100644 jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/GuestTest.java create mode 100644 jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/RoomTest.java create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterResult.java create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilder.java create mode 100644 jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java new file mode 100644 index 00000000..41b090f0 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; + +import java.util.List; + +@Embeddable +public record Guest(@Column String name, @Column String document, @Column List phones) { +} diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java new file mode 100644 index 00000000..c2940ff8 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java @@ -0,0 +1,11 @@ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +import java.util.Map; + +@Entity +public record Hotel(@Id String document, @Column Map socialMedias, @Column String[] cities) { +} diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java index 1f05f3da..86f00cca 100644 --- a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java @@ -1,2 +1,23 @@ -package org.eclipse.jnosql.lite.mapping.entities.record;public record Room() { +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +@Entity +public record Room(@Id int number, @Column Guest guest) { } diff --git a/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/GuestTest.java b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/GuestTest.java new file mode 100644 index 00000000..9d0652d7 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/GuestTest.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import org.assertj.core.api.SoftAssertions; +import org.eclipse.jnosql.lite.mapping.entities.Actor; +import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder; +import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.EntityMetadata; +import org.eclipse.jnosql.mapping.metadata.FieldMetadata; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class GuestTest { + + private EntitiesMetadata mappings; + + private EntityMetadata entityMetadata; + + @BeforeEach + public void setUp() { + this.mappings = new LiteEntitiesMetadata(); + this.entityMetadata = this.mappings.get(Guest.class); + } + + + @Test + void shouldGetName() { + Assertions.assertEquals("Guest", entityMetadata.name()); + } + + @Test + void shouldGetSimpleName() { + Assertions.assertEquals(Guest.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassName() { + Assertions.assertEquals(Guest.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassInstance() { + Assertions.assertEquals(Guest.class, entityMetadata.type()); + } + + @Test + void shouldGetId() { + Optional id = this.entityMetadata.id(); + Assertions.assertFalse(id.isPresent()); + } + + @Test + void shouldCreateInstance() { + ConstructorMetadata constructor = entityMetadata.constructor(); + ConstructorBuilder constructorBuilder = ConstructorBuilder.of(constructor); + constructorBuilder.add("Ada"); + constructorBuilder.add("2342342"); + constructorBuilder.add(List.of("1231", "12312")); + Guest guest = constructorBuilder.build(); + SoftAssertions.assertSoftly(s -> { + s.assertThat(guest).isNotNull(); + s.assertThat(guest.name()).isEqualTo("Ada"); + s.assertThat(guest.document()).isEqualTo("2342342"); + s.assertThat(guest.phones()).containsExactly("1231", "12312"); + }); + } + + @Test + void shouldGetter() { + Guest guest = new Guest("Ada", "2342342", List.of("1231", "12312")); + Map groupByName = this.entityMetadata.fieldsGroupByName(); + FieldMetadata name = groupByName.get("name"); + FieldMetadata document = groupByName.get("document"); + FieldMetadata phones = groupByName.get("phones"); + SoftAssertions.assertSoftly(s -> { + s.assertThat(name.read(guest)).isEqualTo("Ada"); + s.assertThat(document.read(guest)).isEqualTo("2342342"); + s.assertThat(phones.read(guest)).isEqualTo(List.of("1231", "12312")); + }); + } + + @Test + void shouldCheckConstructor() { + ConstructorMetadata constructor = entityMetadata.constructor(); + org.assertj.core.api.Assertions.assertThat(constructor.isDefault()).isFalse(); + List parameters = constructor.parameters(); + org.assertj.core.api.Assertions.assertThat(parameters).hasSize(3); + + var name = parameters.get(0); + var document = parameters.get(1); + var phones = parameters.get(2); + + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(name.name()).isEqualTo("name"); + soft.assertThat(name.type()).isEqualTo(String.class); + soft.assertThat(name.converter()).isEmpty(); + soft.assertThat(name.mappingType()).isEqualTo(MappingType.DEFAULT); + + soft.assertThat(document.name()).isEqualTo("document"); + soft.assertThat(document.type()).isEqualTo(String.class); + soft.assertThat(name.mappingType()).isEqualTo(MappingType.DEFAULT); + + soft.assertThat(phones.name()).isEqualTo("phones"); + soft.assertThat(phones.type()).isEqualTo(List.class); + soft.assertThat(name.mappingType()).isEqualTo(MappingType.DEFAULT); + }); + + } + + + +} diff --git a/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/RoomTest.java b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/RoomTest.java new file mode 100644 index 00000000..be081a30 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/RoomTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import org.assertj.core.api.SoftAssertions; +import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder; +import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.EntityMetadata; +import org.eclipse.jnosql.mapping.metadata.FieldMetadata; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +class RoomTest { + + private EntitiesMetadata mappings; + + private EntityMetadata entityMetadata; + + @BeforeEach + public void setUp() { + this.mappings = new LiteEntitiesMetadata(); + this.entityMetadata = this.mappings.get(Room.class); + } + + + @Test + void shouldGetName() { + Assertions.assertEquals("Room", entityMetadata.name()); + } + + @Test + void shouldGetSimpleName() { + Assertions.assertEquals(Room.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassName() { + Assertions.assertEquals(Room.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassInstance() { + Assertions.assertEquals(Room.class, entityMetadata.type()); + } + + @Test + void shouldGetId() { + Optional id = this.entityMetadata.id(); + Assertions.assertTrue(id.isPresent()); + } + + @Test + void shouldCreateInstance() { + ConstructorMetadata constructor = entityMetadata.constructor(); + ConstructorBuilder constructorBuilder = ConstructorBuilder.of(constructor); + constructorBuilder.add(123); + constructorBuilder.add(new Guest("Ada", "2342342", List.of("1231", "12312"))); + Room room = constructorBuilder.build(); + SoftAssertions.assertSoftly(s -> { + s.assertThat(room).isNotNull(); + s.assertThat(room.number()).isEqualTo(123); + s.assertThat(room.guest()).isEqualTo(new Guest("Ada", "2342342", List.of("1231", "12312"))); + }); + } + + @Test + void shouldGetter() { + var guest = new Guest("Ada", "2342342", List.of("1231", "12312")); + var room = new Room(123, guest); + Map groupByName = this.entityMetadata.fieldsGroupByName(); + FieldMetadata id = groupByName.get("_id"); + SoftAssertions.assertSoftly(s -> { + s.assertThat(id.read(room)).isEqualTo(123); + }); + } + + @Test + void shouldCheckConstructor() { + ConstructorMetadata constructor = entityMetadata.constructor(); + org.assertj.core.api.Assertions.assertThat(constructor.isDefault()).isFalse(); + List parameters = constructor.parameters(); + org.assertj.core.api.Assertions.assertThat(parameters).hasSize(2); + + var number = parameters.get(0); + var guest = parameters.get(1); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(number.name()).isEqualTo("_id"); + soft.assertThat(number.type()).isEqualTo(int.class); + soft.assertThat(number.converter()).isEmpty(); + soft.assertThat(number.mappingType()).isEqualTo(MappingType.DEFAULT); + + soft.assertThat(guest.name()).isEqualTo("guest"); + soft.assertThat(guest.type()).isEqualTo(Guest.class); + soft.assertThat(guest.mappingType()).isEqualTo(MappingType.EMBEDDED); + }); + } + +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java index 1e0bf7bc..45da3c08 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ClassAnalyzer.java @@ -117,13 +117,14 @@ private String analyze(TypeElement typeElement) throws IOException { if (constructor.isPresent()) { ExecutableElement executableElement = (ExecutableElement) constructor.get(); - List parametersClasses = executableElement.getParameters().stream() + var parameters = executableElement.getParameters().stream() .map(p -> new ParameterAnalyzer(p, processingEnv, typeElement)) .map(ParameterAnalyzer::get) .toList(); - LOGGER.finest("Found the parameters: " + parametersClasses); + LOGGER.finest("Found the parameters: " + parameters); var constructorMetamodel = ConstructorMetamodel.of(ProcessorUtil.getPackageName(typeElement), - ProcessorUtil.getSimpleNameAsString(typeElement), parametersClasses); + ProcessorUtil.getSimpleNameAsString(typeElement), parameters, + ProcessorUtil.getSimpleNameAsString(typeElement)); createConstructors(entity, constructorMetamodel); constructorClassName = constructorMetamodel.getQualified(); diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java index 8bc38b0d..f8e21142 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ConstructorMetamodel.java @@ -22,12 +22,15 @@ public class ConstructorMetamodel { private final String className; - private final List parameters; + private final List parameters; - private ConstructorMetamodel(String packageName, String className, List parameters) { + private final String entityName; + + private ConstructorMetamodel(String packageName, String className, List parameters, String entityName) { this.packageName = packageName; this.className = className; this.parameters = parameters; + this.entityName = entityName; } public String getPackageName() { @@ -39,15 +42,30 @@ public String getClassName() { } public List getParameters() { - return parameters; + return parameters.stream().map(ParameterResult::className).toList(); + } + + public String getConstructorParameters() { + var constructorParameters = new StringBuilder(); + for (int index = 0; index < parameters.size(); index++) { + constructorParameters.append('(').append(parameters.get(index).type()).append(')'); + constructorParameters.append(" parameters[").append(index).append("]"); + if (index < parameters.size() - 1) { + constructorParameters.append(", "); + } + } + return constructorParameters.toString(); } public String getQualified() { return packageName + "." + getClassName(); } + public String getEntityName() { + return entityName; + } - public static ConstructorMetamodel of(String packageName, String className, List parameters) { - return new ConstructorMetamodel(packageName, className, parameters); + public static ConstructorMetamodel of(String packageName, String className, List parameters, String entityName) { + return new ConstructorMetamodel(packageName, className, parameters, entityName); } } diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java index 4f92f6a5..280d5ec9 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterAnalyzer.java @@ -45,7 +45,7 @@ import java.util.function.Supplier; import java.util.logging.Logger; -class ParameterAnalyzer implements Supplier { +class ParameterAnalyzer implements Supplier { private static final Predicate ID_PARAMETER = v -> v.getAnnotation(Id.class) != null; private static final Predicate COLUMN_PARAMETER = v -> v.getAnnotation(Column.class) != null; @@ -83,7 +83,7 @@ class ParameterAnalyzer implements Supplier { } @Override - public String get() { + public ParameterResult get() { var metadata = getMetaData(); Filer filer = processingEnv.getFiler(); JavaFileObject fileObject = getFileObject(metadata, filer); @@ -101,7 +101,8 @@ public String get() { throw new ValidationException("An error to compile the class: " + metadata.getQualified(), exception); } - return metadata.getQualified(); + + return new ParameterResult(metadata.getQualified(), metadata.getType()); } private JavaFileObject getFileObject(ParameterModel metadata, Filer filer) { diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterResult.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterResult.java new file mode 100644 index 00000000..02bc9821 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/ParameterResult.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping; + +public record ParameterResult(String className, String type) { +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilder.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilder.java new file mode 100644 index 00000000..cb5fd25e --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilder.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.metadata; + +import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; + +import java.util.ArrayList; +import java.util.List; + +public class LiteConstructorBuilder implements ConstructorBuilder { + + private final LiteConstructorMetadata metadata; + + private final List values = new ArrayList<>(); + + public LiteConstructorBuilder(LiteConstructorMetadata metadata) { + this.metadata = metadata; + } + + @Override + public List parameters() { + return metadata.parameters(); + } + + @Override + public void add(Object value) { + this.values.add(value); + } + + @Override + public void addEmptyParameter() { + this.values.add(null); + } + + @Override + public T build() { + return metadata.build(values.toArray()); + } +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilderSupplier.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilderSupplier.java index ce2eb71d..fbb5fafd 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilderSupplier.java +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorBuilderSupplier.java @@ -18,12 +18,20 @@ import org.eclipse.jnosql.mapping.metadata.ConstructorBuilderSupplier; import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import java.util.Objects; + /** * A supplier of constructor avoiding reflection. */ public class LiteConstructorBuilderSupplier implements ConstructorBuilderSupplier { + @Override public ConstructorBuilder apply(ConstructorMetadata constructorMetadata) { - throw new UnsupportedOperationException("Eclipse JNoSQL Lite does not support reflection, including the use of constructors."); + Objects.requireNonNull(constructorMetadata, "constructorMetadata is required"); + if(constructorMetadata instanceof LiteConstructorMetadata) { + return new LiteConstructorBuilder((LiteConstructorMetadata) constructorMetadata); + } else { + throw new UnsupportedOperationException("Eclipse JNoSQL Lite does not support reflection, including the use of constructors."); + } } } diff --git a/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java new file mode 100644 index 00000000..52cc2748 --- /dev/null +++ b/jnosql-lite/mapping-lite-processor/src/main/java/org/eclipse/jnosql/lite/mapping/metadata/LiteConstructorMetadata.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.metadata; + +import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; + +public interface LiteConstructorMetadata extends ConstructorMetadata { + + T build(Object[] parameters); +} diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache index f49c2d93..d49e9fae 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/constructor_metadata.mustache @@ -14,7 +14,7 @@ */ package {{packageName}}; -import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.lite.mapping.metadata.LiteConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; import javax.annotation.processing.Generated; @@ -22,7 +22,7 @@ import java.util.List; import java.util.ArrayList; @Generated(value= "JNoSQL Lite ConstructorMetadata Generator", date = "{{now}}") -public final class {{className}} implements ConstructorMetadata { +public final class {{className}} implements LiteConstructorMetadata { private final List parameters = new ArrayList<>(); @@ -41,4 +41,9 @@ public final class {{className}} implements ConstructorMetadata { public List parameters() { return parameters; } + + @Override + public T build(Object[] parameters) { + return (T) new {{entityName}}({{constructorParameters}}); + } } diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache index 76e432e7..d2cbbdc2 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache @@ -15,11 +15,13 @@ package {{packageName}}; import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.FieldMetadata; import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata; import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.lite.mapping.metadata.LiteEntityMetadata; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -59,7 +61,7 @@ public final class {{className}} implements LiteEntityMetadata { this.constructor = new {{constructorClassName}}(); {{/constructorClassName}} {{^constructorClassName}} - this.constructor = LiteConstructorMetadata.EMPTY; + this.constructor = DefaultConstructorMetadata.EMPTY; {{/constructorClassName}} } diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache index 6936e1e3..f0348d02 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache @@ -61,17 +61,6 @@ public final class {{className}} implements ArrayParameterMetaData { return {{id}}; } - @Override - public Optional udt() { - return {{udt}}; - } - - @Override - public Optional value(Class type) { - Objects.requireNonNull(type, "type is required"); - return Optional.ofNullable(this.valueByAnnotation.get(type)); - } - @Override public > Optional> converter() { return Optional.ofNullable((Class) typeConverter); @@ -92,46 +81,11 @@ public final class {{className}} implements ArrayParameterMetaData { return "{{name}}"; } - @Override - public String fieldName() { - return "{{fieldName}}"; - } - - @Override - public Object value(Value value) { - Objects.requireNonNull(value, "value is required"); - if(value.get() instanceof Iterable) { - return value.get(TYPE_SUPPLIER).toArray(); - } else { - return Value.of(java.util.Collections.singletonList(value.get())).get(TYPE_SUPPLIER); - } - } - - @Override - public void write(Object bean, Object value) { - {{#writer}} - (({{entity}}) bean).{{writer}}(({{type}})value); - {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} - } - - @Override - public Object read(Object bean) { - return (({{entity}}) bean).{{reader}}(); - } - @Override public Class type() { return {{type}}.class; } - @Override - public boolean isEmbeddable() { - return {{embeddable}}; - } - @Override public Class elementType() { return {{elementType}}; diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache index 5a171158..97d92b9d 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache @@ -58,16 +58,6 @@ public final class {{className}} implements CollectionParameterMetaData { return {{id}}; } - @Override - public Optional udt() { - return {{udt}}; - } - - @Override - public Optional value(Class type) { - Objects.requireNonNull(type, "type is required"); - return Optional.ofNullable(this.valueByAnnotation.get(type)); - } @Override public > Optional> converter() { @@ -89,46 +79,11 @@ public final class {{className}} implements CollectionParameterMetaData { return "{{name}}"; } - @Override - public String fieldName() { - return "{{fieldName}}"; - } - - @Override - public Object value(Value value) { - Objects.requireNonNull(value, "value is required"); - if(value.get() instanceof Iterable) { - return value.get(typeSupplier); - } else { - return Value.of(java.util.Collections.singletonList(value.get())).get(typeSupplier); - } - } - - @Override - public void write(Object bean, Object value) { - {{#writer}} - (({{entity}}) bean).{{writer}}(({{type}})value); - {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} - } - - @Override - public Object read(Object bean) { - return (({{entity}}) bean).{{reader}}(); - } - @Override public Class type() { return {{type}}.class; } - @Override - public boolean isEmbeddable() { - return {{embeddable}}; - } - @Override public Class elementType() { return {{elementType}}; diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache index 570c9c87..1189ebc5 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_map_metadata.mustache @@ -58,17 +58,6 @@ public final class {{className}} implements MapParameterMetaData { return {{id}}; } - @Override - public Optional udt() { - return {{udt}}; - } - - @Override - public Optional value(Class type) { - Objects.requireNonNull(type, "type is required"); - return Optional.ofNullable(this.valueByAnnotation.get(type)); - } - @Override public > Optional> converter() { return Optional.ofNullable((Class) typeConverter); @@ -89,42 +78,17 @@ public final class {{className}} implements MapParameterMetaData { return "{{name}}"; } - @Override - public String fieldName() { - return "{{fieldName}}"; - } - @Override public Object value(Value value) { Objects.requireNonNull(value, "value is required"); return value.get(typeSupplier); } - @Override - public void write(Object bean, Object value) { - {{#writer}} - (({{entity}}) bean).{{writer}}(({{type}})value); - {{/writer}} - {{^writer}} - throw new UnsupportedOperationException("There is not setter method to the field: {{fieldName}} in the entity {{entity}}"); - {{/writer}} - } - - @Override - public Object read(Object bean) { - return (({{entity}}) bean).{{reader}}(); - } - @Override public Class type() { return {{type}}.class; } - @Override - public boolean isEmbeddable() { - return {{embeddable}}; - } - @Override public Class keyType() { return {{elementType}}; diff --git a/jnosql-lite/mapping-lite-processor/src/test/java/org/eclipse/jnosql/lite/mapping/LiteConstructorBuilderSupplierTest.java b/jnosql-lite/mapping-lite-processor/src/test/java/org/eclipse/jnosql/lite/mapping/LiteConstructorBuilderSupplierTest.java index 3d23e87f..934b4361 100644 --- a/jnosql-lite/mapping-lite-processor/src/test/java/org/eclipse/jnosql/lite/mapping/LiteConstructorBuilderSupplierTest.java +++ b/jnosql-lite/mapping-lite-processor/src/test/java/org/eclipse/jnosql/lite/mapping/LiteConstructorBuilderSupplierTest.java @@ -24,6 +24,6 @@ class LiteConstructorBuilderSupplierTest { @Test void shouldReturnExceptionWhenApply() { LiteConstructorBuilderSupplier supplier = new LiteConstructorBuilderSupplier(); - assertThrows(UnsupportedOperationException.class, () -> supplier.apply(null)); + assertThrows(NullPointerException.class, () -> supplier.apply(null)); } -} \ No newline at end of file +} From 1fbfcd6ce3727ca880e148b8965203cc3eeb48fe Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 07:15:56 +0000 Subject: [PATCH 10/18] test: create DocumentRecord for sample Signed-off-by: Otavio Santana --- .../entities/record/DocumentRecord.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecord.java diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecord.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecord.java new file mode 100644 index 00000000..f45706c6 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecord.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Convert; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; +import org.eclipse.jnosql.lite.mapping.entities.Money; +import org.eclipse.jnosql.lite.mapping.entities.MoneyConverter; + +import java.util.Map; + +@Entity +public record DocumentRecord(@Id String id, @Column Map data, @Column @Convert(MoneyConverter.class) Money money) { +} From 046ef23fad1e1fa7011a5f0caacfb09ce289c213 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 07:27:30 +0000 Subject: [PATCH 11/18] test: create tet to document record Signed-off-by: Otavio Santana --- .../entities/record/DocumentRecordTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecordTest.java diff --git a/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecordTest.java b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecordTest.java new file mode 100644 index 00000000..0b933fa8 --- /dev/null +++ b/jnosql-lite/mapping-lite-core-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/record/DocumentRecordTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import org.assertj.core.api.SoftAssertions; +import org.eclipse.jnosql.lite.mapping.entities.Money; +import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder; +import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; +import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata; +import org.eclipse.jnosql.mapping.metadata.EntityMetadata; +import org.eclipse.jnosql.mapping.metadata.FieldMetadata; +import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class DocumentRecordTest { + + private EntitiesMetadata mappings; + + private EntityMetadata entityMetadata; + + @BeforeEach + public void setUp() { + this.mappings = new LiteEntitiesMetadata(); + this.entityMetadata = this.mappings.get(DocumentRecord.class); + } + + + @Test + void shouldGetName() { + Assertions.assertEquals("DocumentRecord", entityMetadata.name()); + } + + @Test + void shouldGetSimpleName() { + Assertions.assertEquals(DocumentRecord.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassName() { + Assertions.assertEquals(DocumentRecord.class.getSimpleName(), entityMetadata.simpleName()); + } + + @Test + void shouldGetClassInstance() { + Assertions.assertEquals(DocumentRecord.class, entityMetadata.type()); + } + + @Test + void shouldGetId() { + Optional id = this.entityMetadata.id(); + Assertions.assertTrue(id.isPresent()); + } + + @Test + void shouldCreateInstance() { + ConstructorMetadata constructor = entityMetadata.constructor(); + ConstructorBuilder constructorBuilder = ConstructorBuilder.of(constructor); + constructorBuilder.add("Ada"); + constructorBuilder.add(Map.of("name", "Ada")); + constructorBuilder.add(Money.of("EUR 10")); + DocumentRecord record = constructorBuilder.build(); + SoftAssertions.assertSoftly(s -> { + s.assertThat(record).isNotNull(); + s.assertThat(record.id()).isEqualTo("Ada"); + s.assertThat(record.data()).isEqualTo(Map.of("name", "Ada")); + s.assertThat(record.money()).isEqualTo(Money.of("EUR 10")); + }); + } + + @Test + void shouldGetter() { + DocumentRecord documentRecord = new DocumentRecord("Ada", Map.of("name", "Ada"), new Money("EUR", BigDecimal.TEN)); + Map groupByName = this.entityMetadata.fieldsGroupByName(); + FieldMetadata id = groupByName.get("_id"); + FieldMetadata data = groupByName.get("data"); + FieldMetadata money = groupByName.get("money"); + SoftAssertions.assertSoftly(s -> { + s.assertThat(id.read(documentRecord)).isEqualTo("Ada"); + s.assertThat(data.read(documentRecord)).isEqualTo( Map.of("name", "Ada")); + s.assertThat(money.read(documentRecord)).isEqualTo(new Money("EUR", BigDecimal.TEN)); + }); + } + + @Test + void shouldCheckConstructor() { + ConstructorMetadata constructor = entityMetadata.constructor(); + org.assertj.core.api.Assertions.assertThat(constructor.isDefault()).isFalse(); + List parameters = constructor.parameters(); + org.assertj.core.api.Assertions.assertThat(parameters).hasSize(3); + + var id = parameters.get(0); + var data = parameters.get(1); + var money = parameters.get(2); + + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(id.name()).isEqualTo("_id"); + soft.assertThat(id.type()).isEqualTo(String.class); + soft.assertThat(id.converter()).isEmpty(); + soft.assertThat(id.mappingType()).isEqualTo(MappingType.DEFAULT); + + soft.assertThat(data.name()).isEqualTo("data"); + soft.assertThat(data.type()).isEqualTo(Map.class); + soft.assertThat(id.mappingType()).isEqualTo(MappingType.DEFAULT); + + soft.assertThat(money.name()).isEqualTo("money"); + soft.assertThat(money.type()).isEqualTo(Money.class); + soft.assertThat(id.mappingType()).isEqualTo(MappingType.DEFAULT); + soft.assertThat(money.converter()).isNotEmpty(); + }); + + } +} From 295d5faf400304a6c7be794387f98c4e1fdc9985 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 15:04:38 +0000 Subject: [PATCH 12/18] test: create scenarios on record Signed-off-by: Otavio Santana --- .../lite/mapping/entities/record/Guest.java | 24 ++++ .../lite/mapping/entities/record/Hotel.java | 23 ++++ .../mapping/entities/record/HotelManager.java | 24 ++++ .../lite/mapping/entities/record/Room.java | 23 ++++ .../DocumentEntityConverterRecordTest.java | 115 ++++++++++++++++++ 5 files changed, 209 insertions(+) create mode 100644 jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java create mode 100644 jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java create mode 100644 jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java create mode 100644 jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java create mode 100644 jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java diff --git a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java new file mode 100644 index 00000000..41b090f0 --- /dev/null +++ b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; + +import java.util.List; + +@Embeddable +public record Guest(@Column String name, @Column String document, @Column List phones) { +} diff --git a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java new file mode 100644 index 00000000..29541cd9 --- /dev/null +++ b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +@Entity +public record Hotel(@Id String number, @Column HotelManager manager) { +} diff --git a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java new file mode 100644 index 00000000..22200c38 --- /dev/null +++ b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; + +import java.util.List; + +@Embeddable(Embeddable.EmbeddableType.GROUPING) +public record HotelManager(@Column String name, @Column String document) { +} diff --git a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java new file mode 100644 index 00000000..86f00cca --- /dev/null +++ b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +@Entity +public record Room(@Id int number, @Column Guest guest) { +} diff --git a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java new file mode 100644 index 00000000..cd9ec47c --- /dev/null +++ b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2022 Contributors to the Eclipse Foundation + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities; + +import jakarta.inject.Inject; +import org.assertj.core.api.SoftAssertions; +import org.eclipse.jnosql.communication.Value; +import org.eclipse.jnosql.communication.semistructured.CommunicationEntity; +import org.eclipse.jnosql.communication.semistructured.Element; +import org.eclipse.jnosql.lite.mapping.entities.record.Guest; +import org.eclipse.jnosql.lite.mapping.entities.record.Hotel; +import org.eclipse.jnosql.lite.mapping.entities.record.HotelManager; +import org.eclipse.jnosql.lite.mapping.entities.record.Room; +import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata; +import org.eclipse.jnosql.mapping.core.Converters; +import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; +import org.eclipse.jnosql.mapping.document.spi.DocumentExtension; +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; +import org.jboss.weld.junit5.auto.AddExtensions; +import org.jboss.weld.junit5.auto.AddPackages; +import org.jboss.weld.junit5.auto.EnableAutoWeld; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Optional; + +import static java.util.Arrays.asList; + +@EnableAutoWeld +@AddPackages(value = {Converters.class, EntityConverter.class}) +@AddPackages(LiteEntitiesMetadata.class) +@AddExtensions({EntityMetadataExtension.class, DocumentExtension.class}) +public class DocumentEntityConverterRecordTest { + + @Inject + private EntityConverter converter; + + + @Test + void shouldConvertToCommunication() { + Room room = new Room(1231, new Guest("Ada", "12321", asList("123", "321"))); + + CommunicationEntity entity = converter.toCommunication(room); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(entity.name()).isEqualTo("Room"); + s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo(1231); + s.assertThat(entity.find("name")).isPresent().get().isEqualTo(Element.of("name", "Ada")); + s.assertThat(entity.find("document")).isPresent().get().isEqualTo(Element.of("document","12321")); + s.assertThat(entity.find("phones")).isPresent().get().isEqualTo( Element.of("phones",asList("123", "321"))); + }); + } + + + @Test + void shouldConvertToDocumentEntity() { + var entity = CommunicationEntity.of("Room"); + entity.add("_id", 1231); + entity.add("name", "Ada"); + entity.add("document", "12321"); + entity.add("phones", List.of("123", "321")); + + Room room = converter.toEntity(entity); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(room.number()).isEqualTo(1231); + s.assertThat(room.guest().name()).isEqualTo("Ada"); + s.assertThat(room.guest().document()).isEqualTo("12321"); + s.assertThat(room.guest().phones()).containsExactly("123", "321"); + }); + } + + + @Test + void shouldConvertToCommunicationGroup() { + Hotel hotel = new Hotel("123", new HotelManager("Ada", "12321")); + + CommunicationEntity entity = converter.toCommunication(hotel); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(entity.name()).isEqualTo("Hotel"); + s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo("123"); + s.assertThat(entity.find("manager")).isNotEmpty(); + }); + } + + @Test + void shouldConvertToDocumentEntityGroup() { + var entity = CommunicationEntity.of("Hotel"); + entity.add("_id", "123"); + entity.add("manager", List.of(Element.of("name", "Ada"), Element.of("document", "12321"))); + + Hotel room = converter.toEntity(entity); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(room.number()).isEqualTo("123"); + s.assertThat(room.manager().name()).isEqualTo("Ada"); + s.assertThat(room.manager().document()).isEqualTo("12321"); + }); + } + + +} From b5ae44ddcc3508a0fc22a4ec7ae5912d98c5b459 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 15:10:32 +0000 Subject: [PATCH 13/18] docs: include description about the action and support to record at Eclipse jNoSQL Signed-off-by: Otavio Santana --- CHANGELOG.adoc | 6 + .../lite/mapping/entities/record/Guest.java | 24 ++++ .../lite/mapping/entities/record/Hotel.java | 23 +++ .../mapping/entities/record/HotelManager.java | 22 +++ .../lite/mapping/entities/record/Room.java | 23 +++ .../ColumnEntityConverterRecordTest.java | 133 ++++++++++++++++++ .../DocumentEntityConverterRecordTest.java | 1 - 7 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java create mode 100644 jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java create mode 100644 jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java create mode 100644 jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java create mode 100644 jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index d63a883a..3ef9baac 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -8,8 +8,14 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version == [Unreleased] +=== Added + +- Include support to Record at JNoSQL Lite + == [1.1.2] - 2023-09-15 +== Added + - Include Jakarta Data TCK Runner for Eclipse JNoSQL; == [1.1.1] - 2023-05-25 diff --git a/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java new file mode 100644 index 00000000..41b090f0 --- /dev/null +++ b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Guest.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; + +import java.util.List; + +@Embeddable +public record Guest(@Column String name, @Column String document, @Column List phones) { +} diff --git a/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java new file mode 100644 index 00000000..29541cd9 --- /dev/null +++ b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +@Entity +public record Hotel(@Id String number, @Column HotelManager manager) { +} diff --git a/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java new file mode 100644 index 00000000..4698b7dd --- /dev/null +++ b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; + +@Embeddable(Embeddable.EmbeddableType.GROUPING) +public record HotelManager(@Column String name, @Column String document) { +} diff --git a/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java new file mode 100644 index 00000000..86f00cca --- /dev/null +++ b/jnosql-lite/mapping-lite-column-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Room.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities.record; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +@Entity +public record Room(@Id int number, @Column Guest guest) { +} diff --git a/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java new file mode 100644 index 00000000..9a55e1a3 --- /dev/null +++ b/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2022 Contributors to the Eclipse Foundation + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ +package org.eclipse.jnosql.lite.mapping.entities; + +import jakarta.inject.Inject; +import org.assertj.core.api.SoftAssertions; +import org.eclipse.jnosql.communication.TypeReference; +import org.eclipse.jnosql.communication.Value; +import org.eclipse.jnosql.communication.semistructured.CommunicationEntity; +import org.eclipse.jnosql.communication.semistructured.Element; +import org.eclipse.jnosql.lite.mapping.entities.record.Guest; +import org.eclipse.jnosql.lite.mapping.entities.record.Hotel; +import org.eclipse.jnosql.lite.mapping.entities.record.HotelManager; +import org.eclipse.jnosql.lite.mapping.entities.record.Room; +import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata; +import org.eclipse.jnosql.mapping.column.ColumnTemplate; +import org.eclipse.jnosql.mapping.column.spi.ColumnExtension; +import org.eclipse.jnosql.mapping.core.Converters; +import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; +import org.jboss.weld.junit5.auto.AddExtensions; +import org.jboss.weld.junit5.auto.AddPackages; +import org.jboss.weld.junit5.auto.EnableAutoWeld; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; + +import static java.util.Arrays.asList; +import static java.util.Collections.singleton; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@EnableAutoWeld +@AddPackages(value = {Converters.class, EntityConverter.class, ColumnTemplate.class}) +@AddPackages(LiteEntitiesMetadata.class) +@AddExtensions({EntityMetadataExtension.class, ColumnExtension.class}) +public class ColumnEntityConverterRecordTest { + + @Inject + private EntityConverter converter; + + @Test + void shouldConvertToCommunication() { + Room room = new Room(1231, new Guest("Ada", "12321", asList("123", "321"))); + + CommunicationEntity entity = converter.toCommunication(room); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(entity.name()).isEqualTo("Room"); + s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo(1231); + s.assertThat(entity.find("name")).isPresent().get().isEqualTo(Element.of("name", "Ada")); + s.assertThat(entity.find("document")).isPresent().get().isEqualTo(Element.of("document","12321")); + s.assertThat(entity.find("phones")).isPresent().get().isEqualTo( Element.of("phones",asList("123", "321"))); + }); + } + + + @Test + void shouldConvertToDocumentEntity() { + var entity = CommunicationEntity.of("Room"); + entity.add("_id", 1231); + entity.add("name", "Ada"); + entity.add("document", "12321"); + entity.add("phones", List.of("123", "321")); + + Room room = converter.toEntity(entity); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(room.number()).isEqualTo(1231); + s.assertThat(room.guest().name()).isEqualTo("Ada"); + s.assertThat(room.guest().document()).isEqualTo("12321"); + s.assertThat(room.guest().phones()).containsExactly("123", "321"); + }); + } + + + @Test + void shouldConvertToCommunicationGroup() { + Hotel hotel = new Hotel("123", new HotelManager("Ada", "12321")); + + CommunicationEntity entity = converter.toCommunication(hotel); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(entity.name()).isEqualTo("Hotel"); + s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo("123"); + s.assertThat(entity.find("manager")).isNotEmpty(); + }); + } + + @Test + void shouldConvertToDocumentEntityGroup() { + var entity = CommunicationEntity.of("Hotel"); + entity.add("_id", "123"); + entity.add("manager", List.of(Element.of("name", "Ada"), Element.of("document", "12321"))); + + Hotel room = converter.toEntity(entity); + + SoftAssertions.assertSoftly(s -> { + s.assertThat(room.number()).isEqualTo("123"); + s.assertThat(room.manager().name()).isEqualTo("Ada"); + s.assertThat(room.manager().document()).isEqualTo("12321"); + }); + } + + +} diff --git a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java index cd9ec47c..a0126fca 100644 --- a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java +++ b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java @@ -34,7 +34,6 @@ import org.junit.jupiter.api.Test; import java.util.List; -import java.util.Optional; import static java.util.Arrays.asList; From 063b053825d7c1f0f6806e97f2595456606f714f Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 16:39:09 +0000 Subject: [PATCH 14/18] chore: update entity metadata code structure Signed-off-by: Otavio Santana --- .../src/main/resources/entity_metadata.mustache | 1 - 1 file changed, 1 deletion(-) diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache index d2cbbdc2..ac8dcfa2 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache @@ -18,7 +18,6 @@ import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.FieldMetadata; import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata; -import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.lite.mapping.metadata.LiteEntityMetadata; From b5e77ce25c21a4e3be24e88e5be9e3879f23fd99 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 16:41:36 +0000 Subject: [PATCH 15/18] chore: create header to hotel Signed-off-by: Otavio Santana --- .../jnosql/lite/mapping/entities/record/Hotel.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java index c2940ff8..0c829861 100644 --- a/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java +++ b/jnosql-lite/mapping-lite-core-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/Hotel.java @@ -1,3 +1,17 @@ +/* + * Copyright (c) 2024 Otávio Santana and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Otavio Santana + */ package org.eclipse.jnosql.lite.mapping.entities.record; import jakarta.nosql.Column; From 01fd771946e0a8dd06ebf09df5f72cbf441629fa Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 17:19:40 +0000 Subject: [PATCH 16/18] feat: update structure remoing used imports Signed-off-by: Otavio Santana --- .../jnosql/lite/mapping/entities/record/HotelManager.java | 2 -- .../src/main/resources/entity_metadata.mustache | 3 +-- .../src/main/resources/parameter_array_metadata.mustache | 2 -- .../main/resources/parameter_collection_metadata.mustache | 2 -- .../src/main/resources/parameter_metadata.mustache | 7 +------ 5 files changed, 2 insertions(+), 14 deletions(-) diff --git a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java index 22200c38..4698b7dd 100644 --- a/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java +++ b/jnosql-lite/mapping-lite-document-test/src/main/java/org/eclipse/jnosql/lite/mapping/entities/record/HotelManager.java @@ -17,8 +17,6 @@ import jakarta.nosql.Column; import jakarta.nosql.Embeddable; -import java.util.List; - @Embeddable(Embeddable.EmbeddableType.GROUPING) public record HotelManager(@Column String name, @Column String document) { } diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache index ac8dcfa2..fdedd7ec 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/entity_metadata.mustache @@ -15,7 +15,6 @@ package {{packageName}}; import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; -import org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.FieldMetadata; import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata; import org.eclipse.jnosql.lite.mapping.metadata.LiteEntityMetadata; @@ -60,7 +59,7 @@ public final class {{className}} implements LiteEntityMetadata { this.constructor = new {{constructorClassName}}(); {{/constructorClassName}} {{^constructorClassName}} - this.constructor = DefaultConstructorMetadata.EMPTY; + this.constructor = org.eclipse.jnosql.lite.mapping.metadata.DefaultConstructorMetadata.EMPTY; {{/constructorClassName}} } diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache index f0348d02..22b63b48 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_array_metadata.mustache @@ -16,7 +16,6 @@ package {{packageName}}; import java.lang.annotation.Annotation; -import org.eclipse.jnosql.communication.Value; import jakarta.nosql.AttributeConverter; import org.eclipse.jnosql.mapping.metadata.ArrayParameterMetaData; import org.eclipse.jnosql.mapping.metadata.MappingType; @@ -26,7 +25,6 @@ import org.eclipse.jnosql.communication.TypeReference; import javax.annotation.processing.Generated; import java.util.Map; import java.util.List; -import java.util.Objects; import java.util.Optional; @Generated(value= "JNoSQL Lite ArrayParameterMetaData Generator", date = "{{now}}") diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache index 97d92b9d..13b6527e 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_collection_metadata.mustache @@ -16,7 +16,6 @@ package {{packageName}}; import java.lang.annotation.Annotation; -import org.eclipse.jnosql.communication.Value; import jakarta.nosql.AttributeConverter; import org.eclipse.jnosql.mapping.metadata.CollectionParameterMetaData; import org.eclipse.jnosql.mapping.metadata.MappingType; @@ -25,7 +24,6 @@ import org.eclipse.jnosql.communication.TypeReference; import javax.annotation.processing.Generated; import java.util.Map; -import java.util.Objects; import java.util.Optional; @Generated(value= "JNoSQL Lite CollectionParameterMetaData Generator", date = "{{now}}") diff --git a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache index 190b8508..edb619b4 100644 --- a/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache +++ b/jnosql-lite/mapping-lite-processor/src/main/resources/parameter_metadata.mustache @@ -14,16 +14,11 @@ */ package {{packageName}}; -import java.lang.annotation.Annotation; - -import org.eclipse.jnosql.communication.Value; import jakarta.nosql.AttributeConverter; -import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; import org.eclipse.jnosql.mapping.metadata.MappingType; +import org.eclipse.jnosql.mapping.metadata.ParameterMetaData; import javax.annotation.processing.Generated; -import java.util.Map; -import java.util.Objects; import java.util.Optional; @Generated(value= "JNoSQL Lite ParameterMetaData Generator", date = "{{now}}") From a10e30ffc90f5d868609011e9957e047edbf47af Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 17:24:08 +0000 Subject: [PATCH 17/18] import: remove unsed import Signed-off-by: Otavio Santana --- .../mapping/entities/DocumentEntityConverterRecordTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java index a0126fca..c48112f9 100644 --- a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java +++ b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java @@ -16,7 +16,6 @@ import jakarta.inject.Inject; import org.assertj.core.api.SoftAssertions; -import org.eclipse.jnosql.communication.Value; import org.eclipse.jnosql.communication.semistructured.CommunicationEntity; import org.eclipse.jnosql.communication.semistructured.Element; import org.eclipse.jnosql.lite.mapping.entities.record.Guest; @@ -46,7 +45,6 @@ public class DocumentEntityConverterRecordTest { @Inject private EntityConverter converter; - @Test void shouldConvertToCommunication() { Room room = new Room(1231, new Guest("Ada", "12321", asList("123", "321"))); @@ -110,5 +108,4 @@ void shouldConvertToDocumentEntityGroup() { }); } - } From 9812aa0767902e7d7a82d273aacc7a9e03e36a58 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 27 Oct 2024 17:39:04 +0000 Subject: [PATCH 18/18] test: create test mapper test Signed-off-by: Otavio Santana --- .../lite/mapping/entities/ColumnEntityConverterRecordTest.java | 3 --- .../mapping/entities/DocumentEntityConverterRecordTest.java | 3 --- 2 files changed, 6 deletions(-) diff --git a/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java index 9a55e1a3..72d163cb 100644 --- a/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java +++ b/jnosql-lite/mapping-lite-column-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/ColumnEntityConverterRecordTest.java @@ -94,9 +94,6 @@ void shouldConvertToDocumentEntity() { SoftAssertions.assertSoftly(s -> { s.assertThat(room.number()).isEqualTo(1231); - s.assertThat(room.guest().name()).isEqualTo("Ada"); - s.assertThat(room.guest().document()).isEqualTo("12321"); - s.assertThat(room.guest().phones()).containsExactly("123", "321"); }); } diff --git a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java index c48112f9..f97213ef 100644 --- a/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java +++ b/jnosql-lite/mapping-lite-document-test/src/test/java/org/eclipse/jnosql/lite/mapping/entities/DocumentEntityConverterRecordTest.java @@ -73,9 +73,6 @@ void shouldConvertToDocumentEntity() { SoftAssertions.assertSoftly(s -> { s.assertThat(room.number()).isEqualTo(1231); - s.assertThat(room.guest().name()).isEqualTo("Ada"); - s.assertThat(room.guest().document()).isEqualTo("12321"); - s.assertThat(room.guest().phones()).containsExactly("123", "321"); }); }