Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Yaml configuration and List values #17088

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -334,6 +335,17 @@ public String getValue(@NonNull String s) throws NoSuchElementException {
};
}

@Override
public boolean isListProperty(@NonNull final String propertyName) throws NoSuchElementException {
return false;
}

@NonNull
@Override
public List<String> getListValue(@NonNull final String propertyName) throws NoSuchElementException {
return List.of();
}

public int port() {
return port;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
return wrappedConfig.exists(s);
}

@Override
public boolean isListValue(@NonNull final String propertyName) {
return wrappedConfig.isListValue(propertyName);

Check warning on line 61 in hedera-node/hedera-config/src/main/java/com/hedera/node/config/VersionedConfigImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-config/src/main/java/com/hedera/node/config/VersionedConfigImpl.java#L61

Added line #L61 was not covered by tests
}

@Override
public String getValue(@NonNull final String s) throws NoSuchElementException {
return wrappedConfig.getValue(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.hapi.node.base.AccountID;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.stream.Stream;

/**
Expand All @@ -26,7 +27,7 @@
public class AccountIDConverter implements ConfigConverter<AccountID> {

@Override
public AccountID convert(final String value) throws IllegalArgumentException, NullPointerException {
public AccountID convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

import com.hedera.node.config.types.CongestionMultipliers;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Config api {@link ConfigConverter} implementation for the type {@link CongestionMultipliers}.
*/
public class CongestionMultipliersConverter implements ConfigConverter<CongestionMultipliers> {

@Override
public CongestionMultipliers convert(final String value) throws IllegalArgumentException, NullPointerException {
public CongestionMultipliers convert(@NonNull final String value)
throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.hapi.node.base.ContractID;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.stream.Stream;

/**
Expand All @@ -26,7 +27,7 @@
public class ContractIDConverter implements ConfigConverter<ContractID> {

@Override
public ContractID convert(final String value) throws IllegalArgumentException, NullPointerException {
public ContractID convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

import com.hedera.node.config.types.EntityScaleFactors;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Config api {@link ConfigConverter} implementation for the type {@link EntityScaleFactors}.
*/
public class EntityScaleFactorsConverter implements ConfigConverter<EntityScaleFactors> {

@Override
public EntityScaleFactors convert(final String value) throws IllegalArgumentException, NullPointerException {
public EntityScaleFactors convert(@NonNull final String value)
throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.hapi.node.base.FileID;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.stream.Stream;

/**
Expand All @@ -26,7 +27,7 @@
public class FileIDConverter implements ConfigConverter<FileID> {

@Override
public FileID convert(final String value) throws IllegalArgumentException, NullPointerException {
public FileID convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.node.config.types.KeyValuePair;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import java.util.regex.Pattern;

Expand All @@ -29,7 +30,7 @@ public class KeyValuePairConverter implements ConfigConverter<KeyValuePair> {
private static final String PATTERN = Pattern.quote("=");

@Override
public KeyValuePair convert(final String value) throws IllegalArgumentException, NullPointerException {
public KeyValuePair convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(value, "Parameter 'value' cannot be null");
final String[] split = value.split(PATTERN, 2);
if (split.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

import com.hedera.node.app.hapi.utils.sysfiles.domain.KnownBlockValues;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Config api {@link ConfigConverter} implementation for the type {@link KnownBlockValues}.
*/
public class KnownBlockValuesConverter implements ConfigConverter<KnownBlockValues> {

@Override
public KnownBlockValues convert(final String value) throws IllegalArgumentException, NullPointerException {
public KnownBlockValues convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.node.config.types.LongPair;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import java.util.regex.Pattern;

Expand All @@ -29,7 +30,7 @@ public class LongPairConverter implements ConfigConverter<LongPair> {
private static final String PATTERN = Pattern.quote("-");

@Override
public LongPair convert(final String value) throws IllegalArgumentException, NullPointerException {
public LongPair convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(value, "Parameter 'value' cannot be null");
final String[] split = value.split(PATTERN, 2);
if (split.length <= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

import com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Config api {@link ConfigConverter} implementation for the type {@link ScaleFactor}.
*/
public class ScaleFactorConverter implements ConfigConverter<ScaleFactor> {

@Override
public ScaleFactor convert(final String value) throws IllegalArgumentException, NullPointerException {
public ScaleFactor convert(@NonNull final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.swirlds.config.api.source.ConfigSource;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Properties;
Expand Down Expand Up @@ -54,6 +55,17 @@
return properties.getProperty(s);
}

@Override
public boolean isListProperty(@NonNull final String propertyName) throws NoSuchElementException {
return false;

Check warning on line 60 in hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/DynamicConfigSource.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/DynamicConfigSource.java#L60

Added line #L60 was not covered by tests
timo0 marked this conversation as resolved.
Show resolved Hide resolved
Neeharika-Sompalli marked this conversation as resolved.
Show resolved Hide resolved
}

@NonNull
@Override
public List<String> getListValue(@NonNull final String propertyName) throws NoSuchElementException {
return List.of();

Check warning on line 66 in hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/DynamicConfigSource.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/DynamicConfigSource.java#L66

Added line #L66 was not covered by tests
timo0 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public int getOrdinal() {
return 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
timo0 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -88,6 +89,17 @@
return properties.getProperty(key);
}

@Override
public boolean isListProperty(@NonNull final String propertyName) throws NoSuchElementException {
return false;
timo0 marked this conversation as resolved.
Show resolved Hide resolved
}

@NonNull
@Override
public List<String> getListValue(@NonNull final String propertyName) throws NoSuchElementException {
return List.of();

Check warning on line 100 in hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/PropertyConfigSource.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-config/src/main/java/com/hedera/node/config/sources/PropertyConfigSource.java#L100

Added line #L100 was not covered by tests
timo0 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public int getOrdinal() {
return ordinal;
Expand Down
20 changes: 10 additions & 10 deletions platform-sdk/docs/base/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,22 @@ final Configuration config = ConfigurationBuilder.create()
String value1 = config.getValue("app.var"); // value1 will be null
String value2 = config.getValue("app.var", "default"); // value2 will be null
String value3 = config.getValue("app.var", String.class); // value3 will be null
String value4 = config.getValue("app.var", String.class, "default"); // value4 will be null
String value4 = config.getValue("app.var", String.class, "default"); // value4 will be null

String value5 = config.getValue("app.foo"); // expression will throw a NoSuchElementException
String value6 = config.getValue("app.foo", "default"); // value6 will be "default"
String value7 = config.getValue("app.foo", String.class); // expression will throw a NoSuchElementException
String value8 = config.getValue("app.foo", String.class, "default"); // value8 will be "default"
String value8 = config.getValue("app.foo", String.class, "default"); // value8 will be "default"

List<String> value9 = config.getValues("app.vars"); // value9 will be null
List<String> value10 = config.getValues("app.vars", List.of()); // value10 will be null
List<String> value11 = config.getValues("app.vars", String.class); // value11 will be null
List<String> value12 = config.getValues("app.vars", String.class, List.of()); // value12 will be null
List<String> value12 = config.getValues("app.vars", String.class, List.of()); // value12 will be null

List<String> value13 = config.getValues("app.foos"); // expression will throw a NoSuchElementException
List<String> value14 = config.getValues("app.foos", List.of()); // value14 will be the empty default list
List<String> value14 = config.getValues("app.foos", List.of()); // value14 will be the empty default list
List<String> value15 = config.getValues("app.foos", String.class); // expression will throw a NoSuchElementException
List<String> value16 = config.getValues("app.foos", String.class, List.of()); // value16 will be the empty default list
List<String> value16 = config.getValues("app.foos", String.class, List.of()); // value16 will be the empty default list
```

**Note:** if you want to define an empty list as a value for a property you should define it by
Expand Down Expand Up @@ -171,7 +171,8 @@ The config API supports several datatypes for reading config properties. The fol
1994-11-05T08:15:30-05:00"
- `Duration`: the custom format from the old settings is supported; examples are "2ms" or "10s"
- `ChronoUnit`: examples are "millis" or "seconds"
- `Enum`
- `Enum`
- `InetAddress`

The support for all data types is done by using the `com.swirlds.config.api.converter.ConfigConverter` API. The
`com.swirlds.config.api.ConfigurationBuilder` must be used to add support for custom datatypes. The following code
Expand Down Expand Up @@ -494,4 +495,3 @@ be changed directly.

For the platform the config is provided by the `com.swirlds.common.context.PlatformContext`. Like the context the config
is defined in the `com.swirlds.platform.Browser` class.

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public interface Configuration {
*/
boolean exists(@NonNull String propertyName);

/**
* Checks if the property with the given name is a list property.
* @param propertyName the name of the property that should be checked
* @return true if the property is a list property, false otherwise
*/
boolean isListValue(@NonNull String propertyName);

/**
* Returns the {@link String} value of the property with the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

Expand Down Expand Up @@ -49,11 +47,30 @@ public interface ConfigSource {
*
* @param propertyName the name of the property
* @return the string value of the property
* @throws NoSuchElementException if the property with the given name is not defined in the source
* @throws NoSuchElementException if the property with the given name is not defined in the source OR is a list property
*/
@Nullable
String getValue(@NonNull String propertyName) throws NoSuchElementException;

/**
* Checks if the property with the given name is a list property.
*
* @param propertyName the name of the property
* @return {@code true} if the property is a list property, {@code false} otherwise
* @throws NoSuchElementException if the property with the given name is not defined in the source OR is not a list property
*/
boolean isListProperty(@NonNull String propertyName) throws NoSuchElementException;

/**
* Returns the list value of the property with the given name.
*
* @param propertyName the name of the property
* @return the list value of the property
* @throws NoSuchElementException if the property with the given name is not defined in the source OR is not a list property
*/
@NonNull
List<String> getListValue(@NonNull String propertyName) throws NoSuchElementException;

/**
* Returns the ordinal. The ordinal is used to define a priority order of all config sources while the config source
* with the highest ordinal has the highest priority. A config source will overwrite values of properties that are
Expand All @@ -66,18 +83,6 @@ default int getOrdinal() {
return DEFAULT_ORDINAL;
}

/**
* Returns all properties of this source as an immutable map.
*
* @return the map with all properties
*/
@NonNull
default Map<String, String> getProperties() {
final Map<String, String> props = new HashMap<>();
getPropertyNames().forEach(prop -> props.put(prop, getValue(prop)));
return Collections.unmodifiableMap(props);
}

/**
* Returns the name of the config source. A name must not be unique.
*
Expand Down
Loading