-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for making none-constructor fields explicitly mandatory
This should close #11
- Loading branch information
1 parent
7ea0bf3
commit a208f7d
Showing
17 changed files
with
388 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
annotations/src/main/java/io/jonasg/bob/MandatoryFieldMissingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.jonasg.bob; | ||
|
||
public final class MandatoryFieldMissingException extends RuntimeException { | ||
public MandatoryFieldMissingException(String fieldName, String typeName) { | ||
super("Mandatory field (" + fieldName + ") not set when building type (" + typeName + ")"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
annotations/src/test/java/io/jonasg/bob/RequiredFieldTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.jonasg.bob; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.assertj.core.api.ThrowableAssert.ThrowingCallable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class RequiredFieldTest { | ||
|
||
@Test | ||
void throwIllegalBuildExceptionWhenFieldValueIsNotSet() { | ||
// given | ||
RequiredField<String> nameField = RequiredField.ofNameWithinType("name", "Person"); | ||
|
||
// when | ||
ThrowingCallable whenOrElseThrowIsCalled = nameField::orElseThrow; | ||
|
||
// then | ||
Assertions.assertThatThrownBy(whenOrElseThrowIsCalled) | ||
.isInstanceOf(MandatoryFieldMissingException.class) | ||
.hasMessage("Mandatory field (name) not set when building type (Person)"); | ||
} | ||
|
||
@Test | ||
void returnFieldValue() { | ||
// given | ||
RequiredField<String> nameField = RequiredField.ofNameWithinType("name", "Person"); | ||
nameField.set("John"); | ||
|
||
// when | ||
String value = nameField.orElseThrow(); | ||
|
||
// then | ||
Assertions.assertThat(value) | ||
.isEqualTo("John"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
processor/src/main/java/io/jonasg/bob/definitions/FieldDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
package io.jonasg.bob.definitions; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.type.TypeMirror; | ||
|
||
public record FieldDefinition(String name, TypeMirror type) { | ||
public record FieldDefinition(String name, | ||
List<? extends AnnotationMirror> annotations, | ||
TypeMirror type) { | ||
public <T> boolean isAnnotatedWith(Class<T> type) { | ||
return annotations.stream() | ||
.anyMatch(a -> Objects.equals(type.getName().replaceAll("\\$", "."), | ||
a.getAnnotationType().asElement().asType().toString())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.