From 34819f42c41ecfb25c28a79fcb55742301f659c1 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Thu, 1 Aug 2024 13:43:41 +0200 Subject: [PATCH] Use Hson.Struct instead of Hson.Object to prevent confusion with java.lang.Object. (#9080) --- .../ConfigMetadataCodegenExtension.java | 8 +- .../metadata/codegen/ConfiguredType.java | 18 +-- .../processor/ConfigMetadataHandler.java | 8 +- .../metadata/processor/ConfiguredType.java | 18 +-- .../java/io/helidon/metadata/hson/Hson.java | 120 +++++++++--------- .../io/helidon/metadata/hson/HsonArray.java | 4 +- .../io/helidon/metadata/hson/HsonParser.java | 38 +++--- .../hson/{HsonObject.java => HsonStruct.java} | 32 ++--- .../helidon/metadata/hson/package-info.java | 8 +- .../metadata/hson/ExistingTypesTest.java | 26 ++-- .../io/helidon/metadata/hson/HsonTest.java | 70 +++++----- .../codegen/HelidonMetaInfServices.java | 6 +- .../service/metadata/DescriptorMetadata.java | 2 +- .../metadata/DescriptorMetadataImpl.java | 6 +- .../helidon/service/metadata/Descriptors.java | 4 +- .../registry/CoreServiceDiscovery.java | 2 +- 16 files changed, 185 insertions(+), 185 deletions(-) rename metadata/hson/src/main/java/io/helidon/metadata/hson/{HsonObject.java => HsonStruct.java} (92%) diff --git a/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfigMetadataCodegenExtension.java b/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfigMetadataCodegenExtension.java index a607ab29e64..9c29b6cf301 100644 --- a/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfigMetadataCodegenExtension.java +++ b/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfigMetadataCodegenExtension.java @@ -92,16 +92,16 @@ private Stream typesToProcess(Set typeNames) { } private void storeMetadata() { - List root = new ArrayList<>(); + List root = new ArrayList<>(); for (var module : moduleTypes.entrySet()) { String moduleName = module.getKey(); var types = module.getValue(); - List typeArray = new ArrayList<>(); + List typeArray = new ArrayList<>(); types.forEach(it -> newOptions.get(it).write(typeArray)); - root.add(Hson.objectBuilder() + root.add(Hson.structBuilder() .set("module", moduleName) - .setObjects("types", typeArray) + .setStructs("types", typeArray) .build()); } diff --git a/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfiguredType.java b/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfiguredType.java index 64ec47fcc4f..6801a0e28ff 100644 --- a/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfiguredType.java +++ b/config/metadata/codegen/src/main/java/io/helidon/config/metadata/codegen/ConfiguredType.java @@ -81,8 +81,8 @@ String prefix() { return configured.prefix().orElse(null); } - void write(List typeArray) { - var typeObject = Hson.Object.builder(); + void write(List typeArray) { + var typeObject = Hson.Struct.builder(); typeObject.set("type", targetClass()); typeObject.set("annotatedType", annotatedClass()); @@ -108,11 +108,11 @@ void write(List typeArray) { .collect(Collectors.toList())); } - List options = new ArrayList<>(); + List options = new ArrayList<>(); for (ConfiguredProperty property : allProperties) { writeProperty(options, "", property); } - typeObject.setObjects("options", options); + typeObject.setStructs("options", options); typeArray.add(typeObject.build()); } @@ -132,11 +132,11 @@ private static String paramsToString(List params) { .collect(Collectors.joining(", ")); } - private void writeProperty(List optionsBuilder, + private void writeProperty(List optionsBuilder, String prefix, ConfiguredProperty property) { - var optionBuilder = Hson.Object.builder(); + var optionBuilder = Hson.Struct.builder(); if (property.key() != null && !property.key.isBlank()) { optionBuilder.set("key", prefix(prefix, property.key())); } @@ -181,10 +181,10 @@ private void writeProperty(List optionsBuilder, .forEach(it -> writeProperty(optionsBuilder, finalPrefix, it)); } if (!property.allowedValues.isEmpty()) { - List allowedValues = new ArrayList<>(); + List allowedValues = new ArrayList<>(); for (ConfiguredOptionData.AllowedValue allowedValue : property.allowedValues) { - var allowedJson = Hson.Object.builder() + var allowedJson = Hson.Struct.builder() .set("value", allowedValue.value()); if (!allowedValue.description().isBlank()) { allowedJson.set("description", allowedValue.description().trim()); @@ -192,7 +192,7 @@ private void writeProperty(List optionsBuilder, allowedValues.add(allowedJson.build()); } - optionBuilder.setObjects("allowedValues", allowedValues); + optionBuilder.setStructs("allowedValues", allowedValues); } optionsBuilder.add(optionBuilder.build()); diff --git a/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfigMetadataHandler.java b/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfigMetadataHandler.java index bfa8f1fce1d..6cb879f0e9f 100644 --- a/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfigMetadataHandler.java +++ b/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfigMetadataHandler.java @@ -187,16 +187,16 @@ private void storeMetadata() { This is to allow merging of files - such as when we would want to create on-the-fly JSON for a project with only its dependencies. */ - List moduleArray = new ArrayList<>(); + List moduleArray = new ArrayList<>(); for (var module : moduleTypes.entrySet()) { String moduleName = module.getKey(); var types = module.getValue(); - List typeArray = new ArrayList<>(); + List typeArray = new ArrayList<>(); types.forEach(it -> newOptions.get(it).write(typeArray)); - moduleArray.add(Hson.Object.builder() + moduleArray.add(Hson.Struct.builder() .set("module", moduleName) - .setObjects("types", typeArray) + .setStructs("types", typeArray) .build()); } diff --git a/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfiguredType.java b/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfiguredType.java index 6f37eae5edc..936efe2bbc3 100644 --- a/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfiguredType.java +++ b/config/metadata/processor/src/main/java/io/helidon/config/metadata/processor/ConfiguredType.java @@ -81,8 +81,8 @@ String prefix() { return configured.prefix().orElse(null); } - void write(List typeArray) { - var typeObject = Hson.Object.builder(); + void write(List typeArray) { + var typeObject = Hson.Struct.builder(); typeObject.set("type", targetClass()); typeObject.set("annotatedType", annotatedClass()); @@ -108,11 +108,11 @@ void write(List typeArray) { .collect(Collectors.toList())); } - List options = new ArrayList<>(); + List options = new ArrayList<>(); for (ConfiguredProperty property : allProperties) { writeProperty(options, "", property); } - typeObject.setObjects("options", options); + typeObject.setStructs("options", options); typeArray.add(typeObject.build()); } @@ -132,11 +132,11 @@ private static String paramsToString(List params) { .collect(Collectors.joining(", ")); } - private void writeProperty(List optionsBuilder, + private void writeProperty(List optionsBuilder, String prefix, ConfiguredProperty property) { - var optionBuilder = Hson.Object.builder(); + var optionBuilder = Hson.Struct.builder(); if (property.key() != null && !property.key.isBlank()) { optionBuilder.set("key", prefix(prefix, property.key())); } @@ -181,16 +181,16 @@ private void writeProperty(List optionsBuilder, .forEach(it -> writeProperty(optionsBuilder, finalPrefix, it)); } if (!property.allowedValues.isEmpty()) { - List allowedValues = new ArrayList<>(); + List allowedValues = new ArrayList<>(); for (ConfiguredOptionData.AllowedValue allowedValue : property.allowedValues) { - allowedValues.add(Hson.Object.builder() + allowedValues.add(Hson.Struct.builder() .set("value", allowedValue.value()) .set("description", allowedValue.description()) .build()); } - optionBuilder.setObjects("allowedValues", allowedValues); + optionBuilder.setStructs("allowedValues", allowedValues); } optionsBuilder.add(optionBuilder.build()); diff --git a/metadata/hson/src/main/java/io/helidon/metadata/hson/Hson.java b/metadata/hson/src/main/java/io/helidon/metadata/hson/Hson.java index 5b42360b912..02baf720b5b 100644 --- a/metadata/hson/src/main/java/io/helidon/metadata/hson/Hson.java +++ b/metadata/hson/src/main/java/io/helidon/metadata/hson/Hson.java @@ -37,7 +37,7 @@ private Hson() { * @param inputStream stream to parse * @return a value * @see io.helidon.metadata.hson.Hson.Value#type() - * @see io.helidon.metadata.hson.Hson.Value#asObject() + * @see io.helidon.metadata.hson.Hson.Value#asStruct() * @see io.helidon.metadata.hson.Hson.Value#asArray() */ public static Value parse(InputStream inputStream) { @@ -45,12 +45,12 @@ public static Value parse(InputStream inputStream) { } /** - * A new fluent API builder to construct an HSON Object. + * A new fluent API builder to construct na HSON Struct. * * @return a new builder */ - public static Object.Builder objectBuilder() { - return Object.builder(); + public static Struct.Builder structBuilder() { + return Struct.builder(); } /** @@ -74,9 +74,9 @@ public enum Type { */ NULL, /** - * Nested object value. + * Nested struct value. */ - OBJECT, + STRUCT, /** * Array value. */ @@ -84,34 +84,34 @@ public enum Type { } /** - * HSON Object. + * HSON Struct. *

- * A representation of an object, with possible child values. + * A representation of a struct, with possible child values. This is similar to {@code JsonObject} in JSON-P. */ - public sealed interface Object extends Value permits HsonObject { + public sealed interface Struct extends Value permits HsonStruct { /** - * A new fluent API builder to construct a HSON Object. + * A new fluent API builder to construct a HSON Struct. * * @return a new builder */ static Builder builder() { - return new HsonObject.Builder(); + return new HsonStruct.Builder(); } /** - * Create an empty object. + * Create an empty struct. * * @return new empty instance */ - static Object create() { + static Struct create() { return builder().build(); } /** * Get a value. * - * @param key key under this object + * @param key key under this struct * @return value of that key, or empty if not present; may return value that represents null * @see io.helidon.metadata.hson.Hson.Type */ @@ -120,7 +120,7 @@ static Object create() { /** * Get a boolean value. * - * @param key key under this object + * @param key key under this struct * @return boolean value if present * @throws HsonException in case the key exists, but is not a {@code boolean} */ @@ -129,7 +129,7 @@ static Object create() { /** * Get a boolean value with default if not defined. * - * @param key key under this object + * @param key key under this struct * @param defaultValue default value to use if the key does not exist * @return boolean value, or default value if the key does not exist * @throws HsonException in case the key exists, but is not a @@ -138,19 +138,19 @@ static Object create() { boolean booleanValue(String key, boolean defaultValue); /** - * Get object value. If the value represents {@code null}, returns empty optional. + * Get struct value. If the value represents {@code null}, returns empty optional. * - * @param key key under this object - * @return object value if present + * @param key key under this struct + * @return struct value if present * @throws HsonException in case the key exists, but is not a - * {@link io.helidon.metadata.hson.Hson.Type#OBJECT} + * {@link io.helidon.metadata.hson.Hson.Type#STRUCT} */ - Optional objectValue(String key); + Optional structValue(String key); /** * Get string value. * - * @param key key under this object + * @param key key under this struct * @return string value if present * @throws HsonException in case the key exists, but is not a * {@link io.helidon.metadata.hson.Hson.Type#STRING} @@ -160,7 +160,7 @@ static Object create() { /** * Get a string value with default if not defined. * - * @param key key under this object + * @param key key under this struct * @param defaultValue default value to use if the key does not exist * @return string value, or default value if the key does not exist * @throws HsonException in case the key exists, but is not a @@ -171,7 +171,7 @@ static Object create() { /** * Get int value. * - * @param key key under this object + * @param key key under this struct * @return int value if present, from {@link java.math.BigDecimal#intValue()} * @throws HsonException in case the key exists, but is not a * {@link io.helidon.metadata.hson.Hson.Type#NUMBER} @@ -181,7 +181,7 @@ static Object create() { /** * Get an int value with default if not defined. * - * @param key key under this object + * @param key key under this struct * @param defaultValue default value to use if the key does not exist * @return int value, or default value if the key does not exist * @throws HsonException in case the key exists, but is not a @@ -193,7 +193,7 @@ static Object create() { /** * Get double value. * - * @param key key under this object + * @param key key under this struct * @return double value if present, from {@link java.math.BigDecimal#doubleValue()} * @throws HsonException in case the key exists, but is not a * {@link io.helidon.metadata.hson.Hson.Type#NUMBER} @@ -203,7 +203,7 @@ static Object create() { /** * Get a double value with default if not defined (or null). * - * @param key key under this object + * @param key key under this struct * @param defaultValue default value to use if the key does not exist * @return double value, or default value if the key does not exist * @throws HsonException in case the key exists, but is not a @@ -215,7 +215,7 @@ static Object create() { /** * Get number value. * - * @param key key under this object + * @param key key under this struct * @return big decimal value if present * @throws HsonException in case the key exists, but is not a * {@link io.helidon.metadata.hson.Hson.Type#NUMBER} @@ -225,7 +225,7 @@ static Object create() { /** * Get number value with default if not defined (or null). * - * @param key key under this object + * @param key key under this struct * @param defaultValue default value to use if not present or null * @return big decimal value */ @@ -234,7 +234,7 @@ static Object create() { /** * Get string array value. * - * @param key key under this object + * @param key key under this struct * @return string array value, if the key exists * @throws HsonException in case the key exists, is an array, but elements are not strings * @throws HsonException in case the key exists, but is not an array @@ -242,19 +242,19 @@ static Object create() { Optional> stringArray(String key); /** - * Get object array value. + * Get struct array value. * - * @param key key under this object - * @return object array value, if the key exists - * @throws HsonException in case the key exists, is an array, but elements are not objects + * @param key key under this struct + * @return struct array value, if the key exists + * @throws HsonException in case the key exists, is an array, but elements are not structs * @throws HsonException in case the key exists, but is not an array */ - Optional> objectArray(String key); + Optional> structArray(String key); /** * Get number array value. * - * @param key key under this object + * @param key key under this struct * @return number array value, if the key exists * @throws HsonException in case the key exists, is an array, but elements are not numbers * @throws HsonException in case the key exists, but is not an array @@ -264,7 +264,7 @@ static Object create() { /** * Get boolean array value. * - * @param key key under this object + * @param key key under this struct * @return boolean array value, if the key exists * @throws HsonException in case the key exists, is an array, but elements are not booleans * @throws HsonException in case the key exists, but is not an array @@ -274,18 +274,18 @@ static Object create() { /** * Get array value. * - * @param key key under this object + * @param key key under this struct * @return array value, if the key exists * @throws HsonException in case the key exists, but is not an array */ Optional arrayValue(String key); /** - * Fluent API builder for {@link io.helidon.metadata.hson.Hson.Object}. + * Fluent API builder for {@link io.helidon.metadata.hson.Hson.Struct}. * * @see #build() */ - interface Builder extends io.helidon.common.Builder { + interface Builder extends io.helidon.common.Builder { /** * Unset an existing value assigned to the key. * This method does not care if the key is mapped or not. @@ -385,13 +385,13 @@ interface Builder extends io.helidon.common.Builder { Builder set(String key, Array value); /** - * Set an array of objects. + * Set an array of structs. * * @param key key to set * @param value value to assign to the key * @return updated instance (this instance) */ - Builder setObjects(String key, List value); + Builder setStructs(String key, List value); /** * Set an array of strings. @@ -449,7 +449,7 @@ public sealed interface Value permits HsonValues.StringValue, HsonValues.NumberValue, HsonValues.BooleanValue, HsonValues.NullValue, - Hson.Object, + Struct, Hson.Array { /** * Write the HSON value. @@ -473,33 +473,33 @@ public sealed interface Value permits HsonValues.StringValue, Type type(); /** - * Get an object array from this parsed value. + * Get this parsed value as an array. * - * @return object array, or this object as an array - * @throws HsonException in case this object is not of type - * {@link io.helidon.metadata.hson.Hson.Type#OBJECT} + * @return this value as an array + * @throws HsonException in case this value is not of type + * {@link io.helidon.metadata.hson.Hson.Type#ARRAY} */ default Array asArray() { if (type() != Type.ARRAY) { - throw new HsonException("Attempting to read object of type " + type() + " as an array"); + throw new HsonException("Attempting to read value of type " + type() + " as an array"); } return (Array) this; } /** - * Get an object from this parsed value. + * Get this parsed value as a struct. * - * @return this value as an object - * @throws HsonException in case this object is not of type - * {@link io.helidon.metadata.hson.Hson.Type#OBJECT} + * @return this value as a struct + * @throws HsonException in case this value is not of type + * {@link io.helidon.metadata.hson.Hson.Type#STRUCT} */ - default Hson.Object asObject() { - if (type() != Type.OBJECT) { - throw new HsonException("Attempting to get object of type " + type() + " as an Object"); + default Struct asStruct() { + if (type() != Type.STRUCT) { + throw new HsonException("Attempting to get value of type " + type() + " as a Struct"); } - return (Hson.Object) this; + return (Struct) this; } } @@ -622,11 +622,11 @@ static Array create(float... values) { List getNumbers(); /** - * Assume this is an array of objects, and return the list. + * Assume this is an array of structs, and return the list. * - * @return all object values of this array, except for nulls - * @throws HsonException in case not all elements of this array are objects (or nulls) + * @return all struct values of this array, except for nulls + * @throws HsonException in case not all elements of this array are structs (or nulls) */ - List getObjects(); + List getStructs(); } } diff --git a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonArray.java b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonArray.java index 882bec5e71b..4298c37b20a 100644 --- a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonArray.java +++ b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonArray.java @@ -132,8 +132,8 @@ public List getNumbers() { } @Override - public List getObjects() { - return getTypedList(Hson.Type.OBJECT); + public List getStructs() { + return getTypedList(Hson.Type.STRUCT); } @Override diff --git a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonParser.java b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonParser.java index eb9b4cdff8c..37fa5c4dab2 100644 --- a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonParser.java +++ b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonParser.java @@ -35,8 +35,8 @@ class HsonParser { private static final byte QUOTES = (byte) '"'; private static final byte ARRAY_START = (byte) '['; private static final byte ARRAY_END = (byte) ']'; - private static final byte OBJECT_START = (byte) '{'; - private static final byte OBJECT_END = (byte) '}'; + private static final byte STRUCT_START = (byte) '{'; + private static final byte STRUCT_END = (byte) '}'; private static final byte BACKSLASH = (byte) '\\'; private final DataReader reader; @@ -67,16 +67,16 @@ private Hson.Value read(boolean topLevel) { byte next = skipWhitespace(); if (next == ARRAY_START) { return readArray(); - } else if (next == OBJECT_START) { - return readObject(); + } else if (next == STRUCT_START) { + return readStruct(); } if (topLevel) { throw new HsonException("Index: " + position - + ": failed to parse HSON, invalid object/array opening character: \n" + + ": failed to parse HSON, invalid struct/array opening character: \n" + BufferData.create(new byte[] {next}).debugDataHex()); } if (next == QUOTES) { - return readString("Object"); + return readString("Struct"); } return readValue(); } @@ -95,7 +95,7 @@ private Hson.Value readArray() { } Hson.Value value = switch (next) { - case OBJECT_START -> readObject(); + case STRUCT_START -> readStruct(); case ARRAY_START -> readArray(); case QUOTES -> readString("Array"); default -> readValue(); @@ -119,18 +119,18 @@ private Hson.Value readArray() { } } - private Hson.Value readObject() { + private Hson.Value readStruct() { skip(); // skip { - var object = Hson.Object.builder(); + var struct = Hson.Struct.builder(); while (true) { byte next = skipWhitespace(); - if (next == OBJECT_END) { + if (next == STRUCT_END) { skip(); // skip } - return object.build(); + return struct.build(); } - // now we have "key": value (may be an object, value, string) + // now we have "key": value (may be a struct, value, string) String key = readKey(); skipWhitespace(); next = read(); @@ -140,21 +140,21 @@ private Hson.Value readObject() { .debugDataHex()); } skipWhitespace(); - // the value may be object, array, value + // the value may be struct, array, value Hson.Value value = read(false); - object.set(key, value); + struct.set(key, value); next = skipWhitespace(); if (next == COMMA) { skip(); // , } else { - // this must be object end + // this must be struct end next = skipWhitespace(); - if (next == OBJECT_END) { + if (next == STRUCT_END) { skip(); // skip } - return object.build(); + return struct.build(); } else { throw new HsonException("Index: " + position - + ": value not followed by a comma, and object does not end. Found: \n" + + ": value not followed by a comma, and struct does not end. Found: \n" + BufferData.create(new byte[] {next}).debugDataHex() + ", for key: \n" + BufferData.create(key).debugDataHex()); } @@ -249,7 +249,7 @@ private String toNonStringValueEnd() { if (whitespace(next)) { break; } - if (next == COMMA || next == ARRAY_END || next == OBJECT_END) { + if (next == COMMA || next == ARRAY_END || next == STRUCT_END) { break; } skip(); diff --git a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonObject.java b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonStruct.java similarity index 92% rename from metadata/hson/src/main/java/io/helidon/metadata/hson/HsonObject.java rename to metadata/hson/src/main/java/io/helidon/metadata/hson/HsonStruct.java index 1c0bd55e7bd..ddda1ae8576 100644 --- a/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonObject.java +++ b/metadata/hson/src/main/java/io/helidon/metadata/hson/HsonStruct.java @@ -27,15 +27,15 @@ import java.util.function.Function; import java.util.stream.Collectors; -final class HsonObject implements Hson.Object { +final class HsonStruct implements Hson.Struct { private final Map> values; - private HsonObject(Map> values) { + private HsonStruct(Map> values) { this.values = values; } @Override - public Hson.Object value() { + public Hson.Struct value() { return this; } @@ -55,8 +55,8 @@ public boolean booleanValue(String key, boolean defaultValue) { } @Override - public Optional objectValue(String key) { - return getValue(key, Hson.Type.OBJECT); + public Optional structValue(String key) { + return getValue(key, Hson.Type.STRUCT); } @Override @@ -105,8 +105,8 @@ public Optional> stringArray(String key) { } @Override - public Optional> objectArray(String key) { - return getTypedList(key, Hson.Array::getObjects); + public Optional> structArray(String key) { + return getTypedList(key, Hson.Array::getStructs); } @Override @@ -152,7 +152,7 @@ public void write(PrintWriter writer) { @Override public Hson.Type type() { - return Hson.Type.OBJECT; + return Hson.Type.STRUCT; } @Override @@ -160,10 +160,10 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof HsonObject object)) { + if (!(o instanceof HsonStruct struct)) { return false; } - return Objects.equals(values, object.values); + return Objects.equals(values, struct.values); } @Override @@ -200,7 +200,7 @@ private Optional> getTypedList(String key, Function> values = new LinkedHashMap<>(); Builder() { } @Override - public Hson.Object build() { - return new HsonObject(new LinkedHashMap<>(values)); + public Hson.Struct build() { + return new HsonStruct(new LinkedHashMap<>(values)); } @Override @@ -231,7 +231,7 @@ public Builder unset(String key) { } @Override - public Hson.Object.Builder setNull(String key) { + public Hson.Struct.Builder setNull(String key) { values.put(key, HsonValues.NullValue.INSTANCE); return this; } @@ -308,7 +308,7 @@ public Builder set(String key, Hson.Array value) { } @Override - public Builder setObjects(String key, List value) { + public Builder setStructs(String key, List value) { Objects.requireNonNull(key, "key cannot be null"); Objects.requireNonNull(value, "value cannot be null"); diff --git a/metadata/hson/src/main/java/io/helidon/metadata/hson/package-info.java b/metadata/hson/src/main/java/io/helidon/metadata/hson/package-info.java index c4816172424..b8a05808bfc 100644 --- a/metadata/hson/src/main/java/io/helidon/metadata/hson/package-info.java +++ b/metadata/hson/src/main/java/io/helidon/metadata/hson/package-info.java @@ -21,7 +21,7 @@ * To write HSON (compatible with JSON), start with either of the following types: *
    *
  • {@link io.helidon.metadata.hson.Hson.Array}
  • - *
  • {@link io.helidon.metadata.hson.Hson.Object}
  • + *
  • {@link io.helidon.metadata.hson.Hson.Struct}
  • *
* To read HSON, start with {@link io.helidon.metadata.hson.Hson#parse(java.io.InputStream)}. *

@@ -29,8 +29,8 @@ *

    *
  • Only UTF-8 is supported
  • *
  • Arrays
  • - *
  • Objects
  • - *
  • Nesting of objects and arrays
  • + *
  • Structs (Same as JsonObject from JSON-P in semantics)
  • + *
  • Nesting of structs and arrays
  • *
  • String, BigDecimal, boolean, null
  • *
  • No pretty print (always writes as small as possible)
  • *
  • Keeps order of insertion on write
  • @@ -51,6 +51,6 @@ * above. This module is not Helidon JSON solution - please use one of the supported JSON libraries, * such as JSON-P, JSON-B, or Jackson. *

    - * WARNING: The HSON object is always fully read into memory + * WARNING: The HSON value is always fully read into memory */ package io.helidon.metadata.hson; diff --git a/metadata/hson/src/test/java/io/helidon/metadata/hson/ExistingTypesTest.java b/metadata/hson/src/test/java/io/helidon/metadata/hson/ExistingTypesTest.java index 96fd6745e14..9d5cf77d9fa 100644 --- a/metadata/hson/src/test/java/io/helidon/metadata/hson/ExistingTypesTest.java +++ b/metadata/hson/src/test/java/io/helidon/metadata/hson/ExistingTypesTest.java @@ -35,15 +35,15 @@ class ExistingTypesTest { @Test void testServiceRegistry() throws IOException { - Hson.Object object; + Hson.Struct object; try (InputStream inputStream = resource("/service-registry.json")) { assertThat(inputStream, notNullValue()); object = Hson.parse(inputStream) - .asObject() + .asStruct() .value(); } - Hson.Object generated = object.objectValue("generated") + Hson.Struct generated = object.structValue("generated") .orElseThrow(() -> new IllegalStateException("Cannot find 'generated' object under root")); assertThat(generated.stringValue("trigger"), @@ -55,12 +55,12 @@ void testServiceRegistry() throws IOException { assertThat(generated.stringValue("comments"), optionalValue(is("Service descriptors in module unnamed/io.helidon.examples.quickstart.se"))); - List services = object.objectArray("services") + List services = object.structArray("services") .orElseThrow(() -> new IllegalStateException("Cannot find 'services' object under root")); assertThat(services, hasSize(2)); - Hson.Object service = services.get(0); + Hson.Struct service = services.get(0); assertThat(service.doubleValue("version"), optionalValue(is(1d))); assertThat(service.stringValue("type"), @@ -89,25 +89,25 @@ void testServiceRegistry() throws IOException { @Test void testConfigMetadata() throws IOException { - List objects; + List objects; try (InputStream inputStream = resource("/config-metadata.json")) { assertThat(inputStream, notNullValue()); objects = Hson.parse(inputStream) .asArray() - .getObjects(); + .getStructs(); } assertThat(objects, hasSize(1)); - Hson.Object module = objects.getFirst(); + Hson.Struct module = objects.getFirst(); assertThat(module.stringValue("module"), optionalValue(is("io.helidon.common.configurable"))); - Optional> types = module.objectArray("types"); + Optional> types = module.structArray("types"); assertThat(types, optionalPresent()); - List typesList = types.get(); + List typesList = types.get(); assertThat(typesList, hasSize(5)); - Hson.Object first = typesList.getFirst(); + Hson.Struct first = typesList.getFirst(); assertThat(first.stringValue("annotatedType"), optionalValue(is("io.helidon.common.configurable.ResourceConfig"))); assertThat(first.stringValue("type"), @@ -117,10 +117,10 @@ void testConfigMetadata() throws IOException { assertThat(first.intValue("number"), optionalValue(is(49))); - List optionsList = first.objectArray("options") + List optionsList = first.structArray("options") .orElse(List.of()); assertThat(optionsList, hasSize(9)); - Hson.Object firstOption = optionsList.getFirst(); + Hson.Struct firstOption = optionsList.getFirst(); assertThat(firstOption.stringValue("description"), optionalValue(is("Resource is located on filesystem.\n\n Path of the resource"))); assertThat(firstOption.stringValue("key"), diff --git a/metadata/hson/src/test/java/io/helidon/metadata/hson/HsonTest.java b/metadata/hson/src/test/java/io/helidon/metadata/hson/HsonTest.java index 082cc255b52..adb00182c0e 100644 --- a/metadata/hson/src/test/java/io/helidon/metadata/hson/HsonTest.java +++ b/metadata/hson/src/test/java/io/helidon/metadata/hson/HsonTest.java @@ -39,7 +39,7 @@ class HsonTest { @Test void testWrongTypes() { - Hson.Object object = Hson.objectBuilder() + Hson.Struct object = Hson.structBuilder() .set("number", 1) .set("string", "hi") .setLongs("numbers", List.of(1L, 2L, 3L)) @@ -53,11 +53,11 @@ void testWrongTypes() { assertThrows(HsonException.class, () -> object.doubleValue("string")); assertThrows(HsonException.class, - () -> object.objectValue("string")); + () -> object.structValue("string")); assertThrows(HsonException.class, () -> object.stringArray("string")); assertThrows(HsonException.class, - () -> object.objectArray("string")); + () -> object.structArray("string")); assertThrows(HsonException.class, () -> object.booleanArray("string")); assertThrows(HsonException.class, @@ -70,11 +70,11 @@ void testWrongTypes() { assertThrows(HsonException.class, () -> object.doubleValue("strings")); assertThrows(HsonException.class, - () -> object.objectValue("strings")); + () -> object.structValue("strings")); assertThrows(HsonException.class, () -> object.stringArray("numbers")); assertThrows(HsonException.class, - () -> object.objectArray("strings")); + () -> object.structArray("strings")); assertThrows(HsonException.class, () -> object.booleanArray("strings")); assertThrows(HsonException.class, @@ -83,7 +83,7 @@ void testWrongTypes() { @Test void testReads() { - Hson.Object empty = Hson.Object.create(); + Hson.Struct empty = Hson.Struct.create(); assertThat(empty.booleanValue("anyKey"), optionalEmpty()); assertThat(empty.booleanValue("anyKey", true), is(true)); @@ -102,7 +102,7 @@ void testReads() { assertThat(empty.numberValue("anyKey", bd), sameInstance(bd)); assertThat(empty.stringArray("anyKey"), optionalEmpty()); - assertThat(empty.objectArray("anyKey"), optionalEmpty()); + assertThat(empty.structArray("anyKey"), optionalEmpty()); assertThat(empty.numberArray("anyKey"), optionalEmpty()); } @@ -112,7 +112,7 @@ void testSetNumbers() { BigDecimal one = new BigDecimal(14); BigDecimal two = new BigDecimal(15); - Hson.Object jObject = Hson.objectBuilder() + Hson.Struct jObject = Hson.structBuilder() .setNumbers("numbers", List.of(one, two)) .build(); @@ -124,7 +124,7 @@ void testSetNumbers() { @Test void testSetBooleans() { - Hson.Object jObject = Hson.objectBuilder() + Hson.Struct jObject = Hson.structBuilder() .setBooleans("booleans", List.of(true, false, true)) .build(); @@ -188,7 +188,7 @@ void testWriteBooleanArray() { @Test void testWriteObjectArray() { - Hson.Object first = Hson.objectBuilder() + Hson.Struct first = Hson.structBuilder() .set("string", "value") .set("long", 4L) .set("double", 4d) @@ -198,7 +198,7 @@ void testWriteObjectArray() { .setDoubles("doubles", List.of(1.5d, 2.5d)) .setBooleans("booleans", List.of(true, false)) .build(); - Hson.Object second = Hson.objectBuilder() + Hson.Struct second = Hson.structBuilder() .set("string", "value2") .build(); @@ -225,33 +225,33 @@ void testWriteObjectArray() { assertThat(read.type(), is(Hson.Type.ARRAY)); Hson.Array objects = read.asArray(); - List value = objects.getObjects(); + List value = objects.getStructs(); assertThat(value, hasSize(2)); - Hson.Object readFirst = value.get(0); + Hson.Struct readFirst = value.get(0); assertThat(readFirst, is(first)); - Hson.Object readSecond = value.get(1); + Hson.Struct readSecond = value.get(1); assertThat(readSecond, is(second)); } @Test - void testSetObjects() { - Hson.Object first = Hson.objectBuilder() + void testSetStructs() { + Hson.Struct first = Hson.structBuilder() .set("key", "value1") .build(); - Hson.Object second = Hson.objectBuilder() + Hson.Struct second = Hson.structBuilder() .set("key", "value2") .build(); - Hson.Object withArray = Hson.objectBuilder() - .setObjects("objects", List.of(first, second)) + Hson.Struct withArray = Hson.structBuilder() + .setStructs("objects", List.of(first, second)) .build(); - List objects = withArray.objectArray("objects").get(); + List objects = withArray.structArray("objects").get(); assertThat(objects, hasItems(first, second)); } @Test void testNull() { - Hson.Object object = Hson.objectBuilder() + Hson.Struct object = Hson.structBuilder() .setNull("null-key") .build(); @@ -261,7 +261,7 @@ void testNull() { @Test void testUnset() { - Hson.Object object = Hson.objectBuilder() + Hson.Struct object = Hson.structBuilder() .setNull("null-key") .unset("null-key") .build(); @@ -273,7 +273,7 @@ void testUnset() { void testFunnyDoubles() { StringWriter stringWriter = new StringWriter(); try (PrintWriter pw = new PrintWriter(stringWriter)) { - Hson.objectBuilder() + Hson.structBuilder() .set("first", 1.1d) .set("second", 1.2f) .set("third", 1.3) @@ -288,8 +288,8 @@ void testFunnyDoubles() { @Test void testFeatures() { Hson.Value parsedValue = Hson.parse(HsonTest.class.getResourceAsStream("/json-features.json")); - assertThat(parsedValue.type(), is(Hson.Type.OBJECT)); - Hson.Object object = parsedValue.asObject(); + assertThat(parsedValue.type(), is(Hson.Type.STRUCT)); + Hson.Struct object = parsedValue.asStruct(); testFeaturesNull(object); testFeaturesArray(object); @@ -297,10 +297,10 @@ void testFeatures() { testFeaturesNumbers(object); } - private void testFeaturesNumbers(Hson.Object object) { - Optional maybeObject = object.objectValue("numbers"); + private void testFeaturesNumbers(Hson.Struct object) { + Optional maybeObject = object.structValue("numbers"); assertThat(maybeObject, optionalPresent()); - Hson.Object numbers = maybeObject.get(); + Hson.Struct numbers = maybeObject.get(); assertThat(numbers.numberValue("number1"), optionalValue(is(new BigDecimal(1)))); assertThat(numbers.numberValue("number2"), optionalValue(is(new BigDecimal("1.5")))); @@ -309,10 +309,10 @@ private void testFeaturesNumbers(Hson.Object object) { assertThat(numbers.numberValue("number5"), optionalValue(is(new BigDecimal("1.5e2")))); } - private void testFeaturesEscapes(Hson.Object object) { - Optional maybeObject = object.objectValue("escapes"); + private void testFeaturesEscapes(Hson.Struct object) { + Optional maybeObject = object.structValue("escapes"); assertThat(maybeObject, optionalPresent()); - Hson.Object escapes = maybeObject.get(); + Hson.Struct escapes = maybeObject.get(); assertThat(escapes.stringValue("newline"), optionalValue(is("a\nb"))); assertThat(escapes.stringValue("quotes"), optionalValue(is("a\"b"))); @@ -326,7 +326,7 @@ private void testFeaturesEscapes(Hson.Object object) { } - private void testFeaturesArray(Hson.Object object) { + private void testFeaturesArray(Hson.Struct object) { Optional maybeArray = object.arrayValue("array"); assertThat(maybeArray, optionalPresent()); List> value = maybeArray.get().value(); @@ -335,12 +335,12 @@ private void testFeaturesArray(Hson.Object object) { assertThat(value.get(1).type(), is(Hson.Type.NULL)); assertThat(value.get(2).type(), is(Hson.Type.STRING)); assertThat(value.get(3).type(), is(Hson.Type.NUMBER)); - assertThat(value.get(4).type(), is(Hson.Type.OBJECT)); + assertThat(value.get(4).type(), is(Hson.Type.STRUCT)); assertThat(value.get(5).type(), is(Hson.Type.ARRAY)); } - private void testFeaturesNull(Hson.Object object) { - Optional> maybeValue = object.objectValue("nulls") + private void testFeaturesNull(Hson.Struct object) { + Optional> maybeValue = object.structValue("nulls") .flatMap(it -> it.value("field")); assertThat(maybeValue, optionalPresent()); Hson.Value value = maybeValue.get(); diff --git a/service/codegen/src/main/java/io/helidon/service/codegen/HelidonMetaInfServices.java b/service/codegen/src/main/java/io/helidon/service/codegen/HelidonMetaInfServices.java index 425c0dfad2d..4108c516d39 100644 --- a/service/codegen/src/main/java/io/helidon/service/codegen/HelidonMetaInfServices.java +++ b/service/codegen/src/main/java/io/helidon/service/codegen/HelidonMetaInfServices.java @@ -108,15 +108,15 @@ void add(DescriptorMetadata service) { * Write the file to output. */ void write() { - var root = Hson.objectBuilder() + var root = Hson.structBuilder() .set("module", moduleName); - List servicesHson = new ArrayList<>(); + List servicesHson = new ArrayList<>(); descriptors.stream() .map(DescriptorMetadata::toHson) .forEach(servicesHson::add); - root.setObjects("services", servicesHson); + root.setStructs("services", servicesHson); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (var pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))) { diff --git a/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadata.java b/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadata.java index 6fbc36af9c0..cd665b6553e 100644 --- a/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadata.java +++ b/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadata.java @@ -78,5 +78,5 @@ static DescriptorMetadata create(String registryType, TypeName descriptor, doubl * * @return HSON object (similar to JSON) */ - Hson.Object toHson(); + Hson.Struct toHson(); } diff --git a/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadataImpl.java b/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadataImpl.java index 397a3e39582..19ed3141492 100644 --- a/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadataImpl.java +++ b/service/metadata/src/main/java/io/helidon/service/metadata/DescriptorMetadataImpl.java @@ -32,7 +32,7 @@ record DescriptorMetadataImpl(String registryType, private static final int CURRENT_DESCRIPTOR_VERSION = 1; private static final int DEFAULT_DESCRIPTOR_VERSION = 1; - static DescriptorMetadata create(String moduleName, String location, Hson.Object service) { + static DescriptorMetadata create(String moduleName, String location, Hson.Struct service) { int version = service.intValue("version", DEFAULT_DESCRIPTOR_VERSION); if (version != CURRENT_DESCRIPTOR_VERSION) { throw new IllegalStateException("Invalid descriptor version: " + version @@ -61,8 +61,8 @@ static DescriptorMetadata create(String moduleName, String location, Hson.Object } @Override - public Hson.Object toHson() { - var builder = Hson.objectBuilder(); + public Hson.Struct toHson() { + var builder = Hson.structBuilder(); if (!registryType.equals(REGISTRY_TYPE_CORE)) { builder.set("type", registryType); diff --git a/service/metadata/src/main/java/io/helidon/service/metadata/Descriptors.java b/service/metadata/src/main/java/io/helidon/service/metadata/Descriptors.java index c49f1160fd3..b15286291d2 100644 --- a/service/metadata/src/main/java/io/helidon/service/metadata/Descriptors.java +++ b/service/metadata/src/main/java/io/helidon/service/metadata/Descriptors.java @@ -46,7 +46,7 @@ private Descriptors() { public static List descriptors(String location, Hson.Array moduleRegistries) { List descriptors = new ArrayList<>(); - for (Hson.Object moduleRegistry : moduleRegistries.getObjects()) { + for (Hson.Struct moduleRegistry : moduleRegistries.getStructs()) { String moduleName = moduleRegistry.stringValue("module", "unknown"); int version = moduleRegistry.intValue("version", DEFAULT_REGISTRY_VERSION); if (version != CURRENT_REGISTRY_VERSION) { @@ -56,7 +56,7 @@ public static List descriptors(String location, Hson.Array m + "expected version: \"" + CURRENT_REGISTRY_VERSION + "\""); } - moduleRegistry.objectArray("services") + moduleRegistry.structArray("services") .orElseGet(List::of) .stream() .map(it -> DescriptorMetadataImpl.create(moduleName, location, it)) diff --git a/service/registry/src/main/java/io/helidon/service/registry/CoreServiceDiscovery.java b/service/registry/src/main/java/io/helidon/service/registry/CoreServiceDiscovery.java index 0eea43248a9..1ed9cfde51b 100644 --- a/service/registry/src/main/java/io/helidon/service/registry/CoreServiceDiscovery.java +++ b/service/registry/src/main/java/io/helidon/service/registry/CoreServiceDiscovery.java @@ -228,7 +228,7 @@ public double weight() { } @Override - public Hson.Object toHson() { + public Hson.Struct toHson() { return metadata.toHson(); }