From 0a2b3ad8b43546c891b1a4e3e4850448fd936768 Mon Sep 17 00:00:00 2001 From: Arne H Juul Date: Thu, 9 Dec 2021 12:17:22 +0000 Subject: add extra convenience method to generated builder * takes a lambda that gets a builder to modify * makes it possible to get rid of some boilerplate code as you don't have to "new" the inner builder on the outside. --- .../com/yahoo/config/codegen/BuilderGenerator.java | 12 ++++++++ configgen/src/test/resources/allfeatures.reference | 36 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/configgen/src/main/java/com/yahoo/config/codegen/BuilderGenerator.java b/configgen/src/main/java/com/yahoo/config/codegen/BuilderGenerator.java index 548bd14ccb3..bbbb8676e3f 100644 --- a/configgen/src/main/java/com/yahoo/config/codegen/BuilderGenerator.java +++ b/configgen/src/main/java/com/yahoo/config/codegen/BuilderGenerator.java @@ -157,6 +157,18 @@ public class BuilderGenerator { "}\n" + // "\n" + // "/**\n" + // + " * Make a new builder and run the supplied function on it before adding it to the list\n" + // + " * @param __func lambda that modifies the given builder\n" + // + " * @return this builder\n" + // + " */\n" + // + "public Builder " + n.getName() + "(java.util.function.Consumer<" + builderType(n) + "> __func) {\n" + // + " " + builderType(n) + " __inner = new " + builderType(n) +"();\n" + // + " __func.accept(__inner);\n" + // + " " + n.getName() + ".add(__inner);\n" + // + " return this;\n" + // + "}\n" + // + "\n" + // + "/**\n" + // " * Set the given list as this builder's list of " + nodeClass(n) + " builders\n" + // " * @param __builders a list of builders\n" + // " * @return this builder\n" + // diff --git a/configgen/src/test/resources/allfeatures.reference b/configgen/src/test/resources/allfeatures.reference index e0b6176efa2..08d8aa122c9 100644 --- a/configgen/src/test/resources/allfeatures.reference +++ b/configgen/src/test/resources/allfeatures.reference @@ -581,6 +581,18 @@ public final class AllfeaturesConfig extends ConfigInstance { return this; } + /** + * Make a new builder and run the supplied function on it before adding it to the list + * @param __func lambda that modifies the given builder + * @return this builder + */ + public Builder myArray(java.util.function.Consumer __func) { + MyArray.Builder __inner = new MyArray.Builder(); + __func.accept(__inner); + myArray.add(__inner); + return this; + } + /** * Set the given list as this builder's list of MyArray builders * @param __builders a list of builders @@ -1615,6 +1627,18 @@ public final class AllfeaturesConfig extends ConfigInstance { return this; } + /** + * Make a new builder and run the supplied function on it before adding it to the list + * @param __func lambda that modifies the given builder + * @return this builder + */ + public Builder anotherArray(java.util.function.Consumer __func) { + AnotherArray.Builder __inner = new AnotherArray.Builder(); + __func.accept(__inner); + anotherArray.add(__inner); + return this; + } + /** * Set the given list as this builder's list of AnotherArray builders * @param __builders a list of builders @@ -1913,6 +1937,18 @@ public final class AllfeaturesConfig extends ConfigInstance { return this; } + /** + * Make a new builder and run the supplied function on it before adding it to the list + * @param __func lambda that modifies the given builder + * @return this builder + */ + public Builder anotherArray(java.util.function.Consumer __func) { + AnotherArray.Builder __inner = new AnotherArray.Builder(); + __func.accept(__inner); + anotherArray.add(__inner); + return this; + } + /** * Set the given list as this builder's list of AnotherArray builders * @param __builders a list of builders -- cgit v1.2.3