From 0bb49caf56e5d52b16cb2651974f58783cf81f9c Mon Sep 17 00:00:00 2001 From: David Blevins Date: Mon, 6 Jan 2025 19:54:21 -0800 Subject: [PATCH] Complete add(*) as a method for use when you know nothing about the class https://github.com/tomitribe/pixie/issues/1 --- README.adoc | 2 ++ .../java/org/tomitribe/pixie/Instance.java | 16 +++++++++++- src/main/java/org/tomitribe/pixie/System.java | 8 +++--- .../tomitribe/pixie/InstanceBuilderTest.java | 26 ++++++++++++++++++- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.adoc b/README.adoc index e14caa0..d240478 100644 --- a/README.adoc +++ b/README.adoc @@ -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 diff --git a/src/main/java/org/tomitribe/pixie/Instance.java b/src/main/java/org/tomitribe/pixie/Instance.java index 96ad0cd..cd6f206 100644 --- a/src/main/java/org/tomitribe/pixie/Instance.java +++ b/src/main/java/org/tomitribe/pixie/Instance.java @@ -45,6 +45,7 @@ public static class Builder { private final Class type; private final String name; private boolean warnOnUnusedProperties = false; + private System.Declaration declaration; public Builder(final Class type) { this(type, "instance"); @@ -54,6 +55,7 @@ public Builder(final Class type, final String name) { this.type = type; this.name = name; properties.put(this.name, "new://" + type.getName()); + declaration = new System().new Declaration<>(name, type); } /** @@ -116,6 +118,8 @@ public Builder comp(final String name, final String refName) { * @param value The object instance we anticipate may be useful to the created instance. */ public Builder add(final Object value) { + + final String name = "unnamed$" + value.getClass().getSimpleName() + refs.incrementAndGet(); objects.put(name, value); return this; @@ -135,7 +139,17 @@ public Builder add(final Object value) { * @param value The object instance we anticipate may be useful to the created instance. */ public Builder 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; } diff --git a/src/main/java/org/tomitribe/pixie/System.java b/src/main/java/org/tomitribe/pixie/System.java index b15ee03..45bfa30 100644 --- a/src/main/java/org/tomitribe/pixie/System.java +++ b/src/main/java/org/tomitribe/pixie/System.java @@ -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); @@ -545,7 +543,7 @@ private Class loadComponentClass(final Map.Entry entry) { return loadDeclarationClass(className); } - private void loadAnnotatedDefaults(final Declaration declaration) { + private static void loadAnnotatedDefaults(final Declaration declaration) { for (final Parameter parameter : declaration.constructor.getParameters()) { final String defaultValue = getDefault(parameter); final boolean isNullable = parameter.isAnnotationPresent(Nullable.class); @@ -566,7 +564,7 @@ private void loadAnnotatedDefaults(final Declaration 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(); } @@ -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() { diff --git a/src/test/java/org/tomitribe/pixie/InstanceBuilderTest.java b/src/test/java/org/tomitribe/pixie/InstanceBuilderTest.java index 5540917..e683fca 100644 --- a/src/test/java/org/tomitribe/pixie/InstanceBuilderTest.java +++ b/src/test/java/org/tomitribe/pixie/InstanceBuilderTest.java @@ -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);