Skip to content

Commit

Permalink
Merge pull request #557 from eclipse/fix-flat-embeeded-issue
Browse files Browse the repository at this point in the history
Fix flat embeddable in the constructor builder
  • Loading branch information
otaviojava authored Oct 15, 2024
2 parents e86469c + 39a7a0d commit 8419d35
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version

== [Unreleased]

=== Fixed

- Fix flat embeddable in the constructor builder

== [1.1.2] - 2023-09-15

=== Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,18 @@ protected <T> T toEntity(Class<T> type, List<Element> elements) {
private <T> T convertEntityByConstructor(List<Element> elements, EntityMetadata mapping) {
ConstructorBuilder builder = ConstructorBuilder.of(mapping.constructor());
for (ParameterMetaData parameter : builder.parameters()) {
Predicate<Element> matchName = c -> c.name().equals(parameter.name());
boolean isFlatEmbedded = EMBEDDED.equals(parameter.mappingType());
Optional<Element> element = elements.stream()
.filter(c -> c.name().equals(parameter.name()))
.filter(matchName.or(e -> isFlatEmbedded))
.findFirst();
element.ifPresentOrElse(c -> {
ParameterConverter converter = ParameterConverter.of(parameter, entities());
converter.convert(this, c, parameter, builder);
var converter = ParameterConverter.of(parameter, entities());
if (isFlatEmbedded) {
converter.convert(this, Element.of("_", elements), parameter, builder);
} else {
converter.convert(this, c, parameter, builder);
}
}, builder::addEmptyParameter);
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static ParameterConverter of(ParameterMetaData parameter, EntitiesMetadata entit
case COLLECTION -> collectionConverter(parameter, entities);
case ARRAY -> arrayConverter(parameter, entities);
case MAP -> MAP;
case ENTITY, EMBEDDED_GROUP -> ENTITY;
case ENTITY, EMBEDDED_GROUP, EMBEDDED -> ENTITY;
default -> DEFAULT;
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.mapping.semistructured;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

@Entity
public class Course {

@Id
private String id;

@Column
private Student student;

public Course(String id, Student student) {
this.id = id;
this.student = student;
}

public Course() {
}

public String getId() {
return id;
}

public Course setId(String id) {
this.id = id;
return this;
}

public Student getStudent() {
return student;
}

public Course setStudent(Student student) {
this.student = student;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.BookBag;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.BookUser;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.Computer;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.Guest;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.PetOwner;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.Room;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.SocialMediaFollowers;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.SocialMediaFollowersRecord;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.SuperHero;
Expand Down Expand Up @@ -361,4 +363,35 @@ void shouldConvertArrayTypes() {

}

@Test
void shouldConvertFromFlatCommunicationFromEntity() {

CommunicationEntity communication = CommunicationEntity.of(Room.class.getSimpleName());
communication.add("_id", 12);
communication.add("documentNumber", "123");
communication.add("name", "Ada");
Room entity = converter.toEntity(communication);

SoftAssertions.assertSoftly(softly->{
softly.assertThat(entity).isNotNull();
softly.assertThat(entity.guest()).isNotNull();
softly.assertThat(entity.guest().documentNumber()).isEqualTo("123");
softly.assertThat(entity.guest().name()).isEqualTo("Ada");
softly.assertThat(entity.number()).isEqualTo(12);
});
}

@Test
void shouldConvertFromFlatCommunicationFromEntityToCommunication() {
var room = new Room(12, new Guest("123", "Ada"));
CommunicationEntity communication = converter.toCommunication(room);

SoftAssertions.assertSoftly(softly->{
softly.assertThat(communication).isNotNull();
softly.assertThat(communication.find("_id").orElseThrow().get()).isEqualTo(12);
softly.assertThat(communication.find("documentNumber").orElseThrow().get()).isEqualTo("123");
softly.assertThat(communication.find("name").orElseThrow().get()).isEqualTo("Ada");
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.eclipse.jnosql.mapping.semistructured.entities.WorkflowStep;
import org.eclipse.jnosql.mapping.semistructured.entities.ZipCode;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.BookBag;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.Guest;
import org.eclipse.jnosql.mapping.semistructured.entities.constructor.Room;
import org.jboss.weld.junit5.auto.AddExtensions;
import org.jboss.weld.junit5.auto.AddPackages;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
Expand Down Expand Up @@ -936,6 +938,37 @@ void shouldConvertEntityFromRecordEntityWithColumnArray() {

}

@Test
void shouldConvertFromFlatCommunicationFromEntity() {

CommunicationEntity communication = CommunicationEntity.of(Course.class.getSimpleName());
communication.add("_id", 12);
communication.add("studentId", "123");
communication.add("fullName", "Ada");
Course entity = converter.toEntity(communication);

SoftAssertions.assertSoftly(softly->{
softly.assertThat(entity).isNotNull();
softly.assertThat(entity.getStudent()).isNotNull();
softly.assertThat(entity.getStudent().getStudentId()).isEqualTo("123");
softly.assertThat(entity.getStudent().getFullName()).isEqualTo("Ada");
softly.assertThat(entity.getId()).isEqualTo("12");
});
}

@Test
void shouldConvertFromFlatCommunicationFromEntityToCommunication() {
var course = new Course("12", new Student("123", "Ada"));
CommunicationEntity communication = converter.toCommunication(course);

SoftAssertions.assertSoftly(softly->{
softly.assertThat(communication).isNotNull();
softly.assertThat(communication.find("_id").orElseThrow().get()).isEqualTo("12");
softly.assertThat(communication.find("studentId").orElseThrow().get()).isEqualTo("123");
softly.assertThat(communication.find("fullName").orElseThrow().get()).isEqualTo("Ada");
});
}


private Object getValue(Optional<Element> column) {
return column.map(Element::value).map(Value::get).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.mapping.semistructured;

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

@Embeddable
public class Student {

@Column
private String studentId;

@Column
private String fullName;

public Student(String studentId, String fullName) {
this.studentId = studentId;
this.fullName = fullName;
}

public Student() {
}

public String getStudentId() {
return studentId;
}

public Student setStudentId(String studentId) {
this.studentId = studentId;
return this;
}

public String getFullName() {
return fullName;
}

public Student setFullName(String fullName) {
this.fullName = fullName;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.mapping.semistructured.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

@Embeddable
public record Guest (@Column String documentNumber, @Column String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.mapping.semistructured.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

@Entity
public record Room (@Id int number, @Column Guest guest) {
}

0 comments on commit 8419d35

Please sign in to comment.