-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
465 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
gestalt-core/src/main/java/org/github/gestalt/config/decoder/ConfigDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.github.gestalt.config.decoder; | ||
|
||
import org.github.gestalt.config.entity.ConfigContainer; | ||
import org.github.gestalt.config.node.ConfigNode; | ||
import org.github.gestalt.config.reflect.TypeCapture; | ||
import org.github.gestalt.config.tag.Tags; | ||
import org.github.gestalt.config.utils.GResultOf; | ||
|
||
/** | ||
* Decodes a generic optional type. | ||
* | ||
* @author <a href="mailto:colin.redmond@outlook.com"> Colin Redmond </a> (c) 2024. | ||
*/ | ||
public final class ConfigDecoder implements Decoder<ConfigContainer<?>> { | ||
|
||
@Override | ||
public Priority priority() { | ||
return Priority.MEDIUM; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "ConfigContainer"; | ||
} | ||
|
||
@Override | ||
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) { | ||
return ConfigContainer.class.isAssignableFrom(type.getRawType()); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
@Override | ||
public GResultOf<ConfigContainer<?>> decode(String path, Tags tags, ConfigNode node, TypeCapture<?> type, | ||
DecoderContext decoderContext) { | ||
// decode the generic type of the optional. Then we will wrap the result into an Optional | ||
GResultOf<?> configValue = decoderContext.getDecoderService() | ||
.decodeNode(path, tags, node, type.getFirstParameterType(), decoderContext); | ||
|
||
return configValue.mapWithError((result) -> new ConfigContainer(path, tags, decoderContext, result, type)); | ||
} | ||
} | ||
|
70 changes: 70 additions & 0 deletions
70
gestalt-core/src/main/java/org/github/gestalt/config/entity/ConfigContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.github.gestalt.config.entity; | ||
|
||
import org.github.gestalt.config.decoder.DecoderContext; | ||
import org.github.gestalt.config.exceptions.GestaltException; | ||
import org.github.gestalt.config.reflect.TypeCapture; | ||
import org.github.gestalt.config.reload.CoreReloadListener; | ||
import org.github.gestalt.config.tag.Tags; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A Container for a configuration value that supports dynamic config reloading. | ||
* Allows users to get the config value. | ||
* | ||
* @param <T> Type of the Config Container | ||
*/ | ||
public class ConfigContainer<T> implements CoreReloadListener { | ||
|
||
private static final System.Logger logger = System.getLogger(ConfigContainer.class.getName()); | ||
|
||
// Path used to get the config on reload. | ||
protected final String path; | ||
// Tags used to get the config on reload. | ||
protected final Tags tags; | ||
// Decoder Context hold gestalt to get the config on reload. | ||
protected final DecoderContext decoderContext; | ||
// Type of the Configuration value. | ||
protected final TypeCapture<ConfigContainer<T>> klass; | ||
|
||
protected final TypeCapture<T> configContainerType; | ||
|
||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
Optional<T> value; | ||
|
||
@SuppressWarnings("unchecked") | ||
public ConfigContainer(String path, Tags tags, DecoderContext decoderContext, T value, TypeCapture<ConfigContainer<T>> klass) { | ||
this.path = path; | ||
this.tags = tags; | ||
this.decoderContext = decoderContext; | ||
this.klass = klass; | ||
this.value = Optional.ofNullable(value); | ||
|
||
configContainerType = (TypeCapture<T>) klass.getFirstParameterType(); | ||
|
||
decoderContext.getGestalt().registerListener(this); | ||
} | ||
|
||
public boolean isPresent() { | ||
return value.isPresent(); | ||
} | ||
|
||
public T orElseThrow() throws GestaltException { | ||
return value.orElseThrow(() -> new GestaltException("No results for config path: " + path + ", tags: " + tags + | ||
", and class: " + klass.getName())); | ||
} | ||
|
||
public Optional<T> getOptional() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void reload() { | ||
value = decoderContext.getGestalt().getConfigOptional(path, configContainerType, tags); | ||
|
||
if (value.isEmpty()) { | ||
logger.log(System.Logger.Level.WARNING, "On Reload, no results for config path: " + path + ", tags: " + tags + | ||
", and class: " + klass.getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.