diff options
author | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-15 16:03:57 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-15 16:19:46 +0100 |
commit | 31559085319ebec30a91869bef313c98856e9865 (patch) | |
tree | 0d7242536b6abe80235c48a422c43913b195c4cb /container-core | |
parent | ebba2fa2e5d4a8e59430c2d5ae563e99e10912f5 (diff) |
Add fluent builder
Diffstat (limited to 'container-core')
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/Json.java | 59 | ||||
-rw-r--r-- | container-core/src/test/java/com/yahoo/restapi/JsonTest.java | 31 |
2 files changed, 90 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/restapi/Json.java b/container-core/src/main/java/com/yahoo/restapi/Json.java index f6800c05668..94c931e8ad4 100644 --- a/container-core/src/main/java/com/yahoo/restapi/Json.java +++ b/container-core/src/main/java/com/yahoo/restapi/Json.java @@ -1,5 +1,6 @@ package com.yahoo.restapi; +import com.yahoo.slime.Cursor; import com.yahoo.slime.Inspector; import com.yahoo.slime.ObjectTraverser; import com.yahoo.slime.Slime; @@ -171,4 +172,62 @@ public class Json implements Iterable<Json> { ", path='" + path + '\'' + '}'; } + + /** Provides a fluent API for building a {@link Slime} instance. */ + public static class Builder { + protected final Cursor cursor; + + public static Builder.Array newArray() { return new Builder.Array(new Slime().setArray()); } + public static Builder.Object newObject() { return new Builder.Object(new Slime().setObject()); } + + private Builder(Cursor cursor) { this.cursor = cursor; } + + public static class Array extends Builder { + private Array(Cursor cursor) { super(cursor); } + + public Builder.Array add(Builder.Array array) { + SlimeUtils.copyArray(array.cursor, cursor.addArray()); return this; + } + public Builder.Array add(Builder.Object object) { + SlimeUtils.copyObject(object.cursor, cursor.addObject()); return this; + } + public Builder.Array add(Json json) { + SlimeUtils.addValue(json.inspector, cursor.addObject()); return this; + } + /** Note: does not return {@code this}! */ + public Builder.Array addArray() { return new Array(cursor.addArray()); } + /** Note: does not return {@code this}! */ + public Builder.Object addObject() { return new Object(cursor.addObject()); } + + public Builder.Array add(String value) { cursor.addString(value); return this; } + public Builder.Array add(long value) { cursor.addLong(value); return this; } + public Builder.Array add(double value) { cursor.addDouble(value); return this; } + public Builder.Array add(boolean value) { cursor.addBool(value); return this; } + } + + public static class Object extends Builder { + private Object(Cursor cursor) { super(cursor); } + + public Builder.Object set(String field, Builder.Array array) { + SlimeUtils.copyArray(array.cursor, cursor.setArray(field)); return this; + } + public Builder.Object set(String field, Builder.Object object) { + SlimeUtils.copyObject(object.cursor, cursor.setObject(field)); return this; + } + public Builder.Object set(String field, Json json) { + SlimeUtils.setObjectEntry(json.inspector, field, cursor); return this; + } + /** Note: does not return {@code this}! */ + public Builder.Array setArray(String field) { return new Array(cursor.setArray(field)); } + /** Note: does not return {@code this}! */ + public Builder.Object setObject(String field) { return new Object(cursor.setObject(field)); } + + public Builder.Object set(String field, String value) { cursor.setString(field, value); return this; } + public Builder.Object set(String field, long value) { cursor.setLong(field, value); return this; } + public Builder.Object set(String field, double value) { cursor.setDouble(field, value); return this; } + public Builder.Object set(String field, boolean value) { cursor.setBool(field, value); return this; } + } + + public Json build() { return Json.of(cursor); } + } } diff --git a/container-core/src/test/java/com/yahoo/restapi/JsonTest.java b/container-core/src/test/java/com/yahoo/restapi/JsonTest.java index 276c9b55ea4..0ef4872c908 100644 --- a/container-core/src/test/java/com/yahoo/restapi/JsonTest.java +++ b/container-core/src/test/java/com/yahoo/restapi/JsonTest.java @@ -92,4 +92,35 @@ class JsonTest { assertEquals("bar", json.f("string").asString("foo")); assertEquals("bar", json.f("string").asOptionalString().orElse("foo")); } + + @Test + void builds_expected_json() { + var expected = + """ + { + "string": "bar", + "integer": 42, + "floaty": 8.25, + "bool": true, + "array": [ + 1, + 2, + 3 + ], + "quux": { + "corge": "grault" + } + } + """; + var json = Json.Builder.newObject() + .set("string", "bar") + .set("integer", 42) + .set("floaty", 8.25) + .set("bool", true) + .set("array", Json.Builder.newArray().add(1).add(2).add(3)) + .set("quux", Json.Builder.newObject().set("corge", "grault")) + .build() + .toJson(true); + assertEquals(expected, json); + } } |