Skip to content

Commit

Permalink
Complete add(*) as a method for use when you know nothing about the c…
Browse files Browse the repository at this point in the history
…lass

#1
  • Loading branch information
dblevins committed Jan 7, 2025
1 parent fe9d3dd commit 0bb49ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ home.street=823 Roosevelt Street
home.city=River Falls
home.state=WI
home.zipcode=54022
home.color?=blue
home.country?=USA
----

=== Strict Validation for Accurate Configurations
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/tomitribe/pixie/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static class Builder<T> {
private final Class<T> type;
private final String name;
private boolean warnOnUnusedProperties = false;
private System.Declaration<T> declaration;

public Builder(final Class<T> type) {
this(type, "instance");
Expand All @@ -54,6 +55,7 @@ public Builder(final Class<T> type, final String name) {
this.type = type;
this.name = name;
properties.put(this.name, "new://" + type.getName());
declaration = new System().new Declaration<>(name, type);
}

/**
Expand Down Expand Up @@ -116,6 +118,8 @@ public Builder<T> comp(final String name, final String refName) {
* @param value The object instance we anticipate may be useful to the created instance.
*/
public Builder<T> add(final Object value) {


final String name = "unnamed$" + value.getClass().getSimpleName() + refs.incrementAndGet();
objects.put(name, value);
return this;
Expand All @@ -135,7 +139,17 @@ public Builder<T> add(final Object value) {
* @param value The object instance we anticipate may be useful to the created instance.
*/
public Builder<T> add(final String name, final Object value) {
objects.put(name, value);

if (declaration.getOption(name) != null) {

properties.put(this.name + "." + name, value);

} else if (declaration.getReference(name) != null) {

comp(name, value);

}

return this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/tomitribe/pixie/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ private Declaration createDeclaration(final Class<?> clazz, final String key) {
try {
final Declaration declaration = new Declaration(key, clazz);

loadAnnotatedDefaults(declaration);

issues.addAll(checkForNullableWithDefault(declaration));

applyImplicitOverrides(declaration);
Expand Down Expand Up @@ -545,7 +543,7 @@ private Class<?> loadComponentClass(final Map.Entry<String, String> entry) {
return loadDeclarationClass(className);
}

private <T> void loadAnnotatedDefaults(final Declaration<T> declaration) {
private static <T> void loadAnnotatedDefaults(final Declaration<T> declaration) {
for (final Parameter parameter : declaration.constructor.getParameters()) {
final String defaultValue = getDefault(parameter);
final boolean isNullable = parameter.isAnnotationPresent(Nullable.class);
Expand All @@ -566,7 +564,7 @@ private <T> void loadAnnotatedDefaults(final Declaration<T> declaration) {
}
}

private String getDefault(final Parameter parameter) {
private static String getDefault(final Parameter parameter) {
final Default annotation = parameter.getAnnotation(Default.class);
return annotation == null ? null : annotation.value();
}
Expand All @@ -585,6 +583,8 @@ public Declaration(final String name, final Class clazz) {
this.clazz = clazz;
this.constructor = Constructors.findConstructor(this.clazz);
this.sortingName = (name != null) ? name : clazz.getSimpleName() + java.lang.System.nanoTime();

loadAnnotatedDefaults(this);
}

public String getReferenceId() {
Expand Down
26 changes: 25 additions & 1 deletion src/test/java/org/tomitribe/pixie/InstanceBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,36 @@ public void component() throws Exception {
assertEquals("USA", address.getCountry());

}

@Test
public void componentRefByType() throws Exception {

final Person person = Instance.builder(Person.class)
.option("age", "37")
.add("anything", new Address("820 Roosevelt Street", "River Falls", State.WI, 54022, "USA"))
.add(new Address("820 Roosevelt Street", "River Falls", State.WI, 54022, "USA"))
.build();

assertNotNull(person);
assertEquals("instance", person.getName());
assertEquals(37, person.getAge().intValue());

final Address address = person.getAddress();
assertNotNull(address);
assertEquals("820 Roosevelt Street", address.getStreet());
assertEquals("River Falls", address.getCity());
assertEquals(State.WI, address.getState());
assertEquals(54022, address.getZipcode());
assertEquals("USA", address.getCountry());

}

@Test
public void addOptionAndComponent() throws Exception {

final Person person = Instance.builder(Person.class)
.add("age", "37")
.add("address", new Address("820 Roosevelt Street", "River Falls", State.WI, 54022, "USA"))
.add("direction", new Address("820 Roosevelt Street", "River Falls", State.WI, 54022, "USA"))
.build();

assertNotNull(person);
Expand Down

0 comments on commit 0bb49ca

Please sign in to comment.