https://github.com/gestalt-config/gestalt
A Java configuration library
https://github.com/gestalt-config/gestalt
configuration gradle java kotlin kotlin-library
Last synced: about 1 year ago
JSON representation
A Java configuration library
- Host: GitHub
- URL: https://github.com/gestalt-config/gestalt
- Owner: gestalt-config
- License: apache-2.0
- Created: 2020-12-24T19:47:17.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-30T01:25:52.000Z (over 1 year ago)
- Last Synced: 2024-10-30T03:57:15.230Z (over 1 year ago)
- Topics: configuration, gradle, java, kotlin, kotlin-library
- Language: Java
- Homepage: https://gestalt-config.github.io/gestalt/
- Size: 3.85 MB
- Stars: 80
- Watchers: 2
- Forks: 2
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-java-zh - Gestalt - Gestalt为配置管理的挑战提供了全面的解决方案。它允许您从多个输入获取配置数据,智能地合并它们,并以结构化、类型安全的方式呈现它们。 (项目 / 配置)
- fucking-awesome-java - Gestalt - Gestalt offers a comprehensive solution to the challenges of configuration management. It allows you to source configuration data from multiple inputs, merge them intelligently, and present them in a structured, type-safe manner. (Projects / Configuration)
- awesome-java - Gestalt - Gestalt offers a comprehensive solution to the challenges of configuration management. It allows you to source configuration data from multiple inputs, merge them intelligently, and present them in a structured, type-safe manner. (Projects / Configuration)
README
# Gestalt
[](https://search.maven.org/artifact/com.github.gestalt-config/gestalt-core)
[](LICENSE)
[](https://codecov.io/gh/gestalt-config/gestalt)
[](https://sonarcloud.io/summary/new_code?id=gestalt-config_gestalt)
### **Visit the official documentation: https://gestalt-config.github.io/gestalt/**
All documentation is being migrated to the official documentation site.
Gestalt is a powerful Java configuration library designed to simplify the way you handle and manage configurations in your software projects. In the rapidly evolving landscape of software development, having a flexible and reliable configuration management system is essential for ensuring the smooth operation of your applications.
Gestalt offers a comprehensive solution to the challenges of configuration management. It allows you to source configuration data from multiple inputs, merge them intelligently, and present them in a structured, type-safe manner. Whether you're working with Java beans, lists, sets, or primitive data types, Gestalt's automatic decoding based on data types simplifies the process.
This documentation will guide you through the key features of Gestalt, demonstrate how to get started quickly, and provide detailed insights into its capabilities. Whether you're a seasoned Java developer or just beginning your journey, Gestalt will empower you to manage your application configurations effectively and efficiently.
Let's dive in and explore how Gestalt can streamline your configuration management workflow and help you build more robust and adaptable software.
# Features
- **Automatic decoding based on type:** Supports decoding into bean classes, lists, sets, or primitive types. This simplifies configuration retrieval.
- **Java Records:** Full support for Java Records, constructing records from configuration using the Records Canonical Constructor.
- **Supports Multiple Formats:** Load configurations from various sources, including Environment Variables, Property files, an in-memory map, and more.
- **Read Sub-sections of Your Config:** Easily navigate to specific sub-sections within configurations using dot notation.
- **Kotlin interface:** Full support for Kotlin with an easy-to-use Kotlin-esque interface, ideal for Kotlin projects.
- **Merge Multiple Sources:** Seamlessly merge configurations from different sources to create comprehensive settings.
- **String Substitution:** Build a config value by injecting Environment Variables, System Properties or other nodes into your strings. Evaluate the substitution at either load time or run time.
- **node Substitution:** Include whole config nodes loaded from files or other places in the config tree anywhere in your config tree.
- **A/B Testing** Segregate results based on groups or random results. See A/B Testing in the use cases section.
- **Flexible and Configurable:** The library offers well-defined interfaces, allowing customization and extension.
- **Easy-to-Use Builder:** Get started quickly with a user-friendly builder, or customize specific aspects of the library.
- **Receive All Errors Up Front:** In case of configuration errors, receive multiple errors in a user-friendly log for efficient debugging.
- **Modular Support for Features:** Include only the required features and dependencies in your build, keeping your application lightweight.
- **Zero Dependencies:** The core library has zero external dependencies; add features and dependencies as needed.
- **Java 11 Minimum:** Requires a minimum of Java 11 for compatibility with modern Java versions.
- **Java Modules:** Supports Java 9 modules with proper exports.
- **Well Tested:** Our codebase boasts an impressive ~92% code coverage, validated by over 1975 meaningful tests.
# Getting Started
1. Add the Bintray repository:
Versions 0.1.0 through version 0.11.0 require Java 8. Versions 0.12.0 plus require Java 11.
```kotlin
repositories {
mavenCentral()
}
```
2. Import gestalt-core, and the specific modules you need to support your use cases.
Gradle example:
```groovy
implementation 'com.github.gestalt-config:gestalt-core:${version}'
implementation 'com.github.gestalt-config:gestalt-kotlin:${version}'
```
Or with the kotlin DSL:
```kotlin
implementation("com.github.gestalt-config:gestalt-core:$version")
implementation("com.github.gestalt-config:gestalt-kotlin:$version")
```
Maven Example:
```xml
com.github.gestalt-config
gestalt-core
${version}
```
3. Setup your configuration files
Multiple types of configurations are supported from multiple sources.
Here is an example of the `default.properties`:
```properties
db.hosts[0].user=credmond
db.hosts[0].url=jdbc:postgresql://localhost:5432/mydb1
db.hosts[1].user=credmond
db.hosts[1].url=jdbc:postgresql://localhost:5432/mydb2
db.hosts[2].user=credmond
db.hosts[2].url=jdbc:postgresql://localhost:5432/mydb3
db.connectionTimeout=6000
db.idleTimeout=600
db.maxLifetime=60000.0
http.pool.maxTotal=100
http.pool.maxPerRoute=10
http.pool.validateAfterInactivity=6000
http.pool.keepAliveTimeoutMs=60000
http.pool.idleTimeoutSec=25
```
Here is an example of the `dev.properties`:
```properties
db.hosts[0].url=jdbc:postgresql://dev.host.name1:5432/mydb
db.hosts[1].url=jdbc:postgresql://dev.host.name2:5432/mydb
db.hosts[2].url=jdbc:postgresql://dev.host.name3:5432/mydb
db.connectionTimeout=600
http.pool.maxTotal=1000
http.pool.maxPerRoute=50
```
4. Construct Gestalt using the builder.
Use the builder to construct the Gestalt library. It is possible to do this manually, but the builder greatly simplifies the construction of the library. It uses the service loader to automatically load all the default dependencies.
```java
// Create a map of configurations we wish to inject.
Map configs = new HashMap<>();
configs.put("db.hosts[0].password", "1234");
configs.put("db.hosts[1].password", "5678");
configs.put("db.hosts[2].password", "9012");
configs.put("db.idleTimeout", "123");
// Load the default property files from resources.
URL devFileURL = GestaltSample.class.getClassLoader().getResource("dev.properties");
File devFile = new File(devFileURL.getFile());
// using the builder to layer on the configuration files.
// The later ones layer on and over write any values in the previous
Gestalt gestalt = new GestaltBuilder()
.addSource(ClassPathConfigSourceBuilder.builder().setResource("/default.properties").build()) // Load the default property files from resources.
.addSource(FileConfigSourceBuilder.builder().setFile(devFile).build())
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.build();
// Loads and parses the configurations, this will throw exceptions if there are any errors.
gestalt.loadConfigs();
```
5. Retrieve configurations from Gestalt
Using the Gestalt Interface you can load sub nodes with dot notation into a wide variety of classes.
For non-generic classes you can pass in the class with `getConfig("db.port", Integer.class)` or for classes with generic types we need to use a special TypeCapture wrapper that captures the generic type at runtime. This allows us to construct generic classes with such as List using `new TypeCapture>() {}`
```java
Short myShortWrapper = gestalt.getConfig("http.pool.maxTotal", Short.class);
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);
List httpPoolList = gestalt.getConfig("http.pools", new TypeCapture<>() { });
var httpPoolList = gestalt.getConfig("http.pools", new TypeCapture>() { });
```
The API to retrieve configurations:
```java
/**
* Get a config for a path and a given class.
* If the config is missing or there are any errors it will throw a GestaltException
*/
T getConfig(String path, Class klass) throws GestaltException;
/**
* Get a config for a path and a given class.
* If the config is missing, invalid or there was an exception it will return the default value.
*/
T getConfig(String path, T defaultVal, Class klass);
/**
* Get a config Optional for a path and a given class.
* If the config is missing, invalid or there was an exception it will return an Optional.empty()
*/
Optional getConfigOptional(String path, Class klass);
```
# Config Sources
Adding a ConfigSource to the builder is the minimum step needed to build the Gestalt Library.
You can add several ConfigSources to the builder and Gestalt, and they will be loaded in the order they are added. Where each new source will be merged with the existing source and where applicable overwrite the values of the previous sources. Each Config Source can be a diffrent format such as json, properties or Snake Case Env Vars, then internally they are converted into a common config tree.
```java
Gestalt gestalt = builder
.addSource(FileConfigSourceBuilder.builder().setFile(defaults).build())
.addSource(FileConfigSourceBuilder.builder().setFile(devFile).build())
.addSource(EnvironmentConfigSourceBuilder.builder().setPrefix("MY_APP_CONFIG").build())
.build();
```
In the above example we first load a file defaults, then load a file devFile and overwrite any defaults, then overwrite any values from the Environment Variables.
The priority will be Env Vars > devFile > defaults.
# Config Tree
The config files are loaded and merged into a config tree. While loading into the config tree all node names and paths are converted to lower case and for environment variables we convert screaming snake case into dot notation. However, we do not convert other cases such as camel case into dot notation. So if your configs use a mix of dot notation and camel case, the nodes will not be merged. You can configure this conversion by providing your own `Sentence Lexer` in the `GestaltBuilder`. The config tree has a structure (sort of like json) where the root has one or more nodes or leafs. A node can have one or more nodes or leafs. A leaf can have a value but no further nodes or leafs. As we traverse the tree each node or leaf has a name and in combination it is called a path. A path can not have two leafs or both a node and a leaf at the same place. If this is detected Gestalt will throw an exception on loading with details on the path.
Valid:
```properties
db.connectionTimeout=6000
db.idleTimeout=600
db.maxLifetime=60000.0
http.pool.maxTotal=1000
http.pool.maxPerRoute=50
```
Invalid:
```properties
db.connectionTimeout=6000
db.idleTimeout=600
db=userTable #invalid the path db is both a node and a leaf.
http.pool.maxTotal=1000
http.pool.maxPerRoute=50
HTTP.pool.maxPerRoute=75 #invalid duplicate nodes at the same path.
```
All paths are converted to lower case as different sources have different naming conventions, Env Vars are typically Screaming Snake Case, properties are dot notation, json is camelCase. By normalizing them to lowercase it is easier to merge. However, we do not convert other cases such as camel case into dot notation. It is best to use a consistent case for your configurations.
# Retrieving a configuration
To retrieve a configuration from Gestalt we need the path to the configuration as well as what type of class.
### getConfig path options
Gestalt is **not case sensitive**. Since Gestalt interops between Environment Variables and other sources with various cases, all strings in Gestalt are normalized to a lower case.
By default, Gestalt uses dot notation and allows indexing into arrays using a '[0]'.
If you want to use a different path style you can provide your own [custom lexer](#relaxed-path-parsing-to-support-all-case-paths) to Gestalt. The SentenceLexer is used to convert the path passed to the Gestalt getConfig interface into tokens that Gestalt can use to navigate to your sub node.
```java
// load a whole class, this works best with pojo's
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);
// or get a specific config value from a class
short maxTotal gestalt.getConfig("HTTP.pool.maxTotal", Short.class);
// get with a default if you want a fallback from code
long maxConnectionsPerRoute = gestalt.getConfig("http.Pool.maxPerRoute", 24, Long.class);
// get a list of Host objects, or an PlaceHolder collection if there is no hosts found.
LinkedList hosts = gestalt.getConfig("db.hosts", new LinkedList(), new TypeCapture>() {});
// Get a class at a specific list index.
Host host = gestalt.getConfig("db.hosts[2]", Host.class);
// get a value of a class from a specific list index.
String password = gestalt.getConfig("db.hosts[2].password", String.class);
```
When decoding a path we do use path mappers to try different cases, but that only applies to the sub-tree starting at the path asked for. So if you call `gestalt.getConfig("http.pool", HttpPool.class)` it will try and map the cases for nodes under path `http.pool` but not the nodes in the path `http-pool`.
```java
// given the record.
public record HttpPool(String poolSize, int timeout) {}
```
And the properties:
```properties
booking.service.pool.size = 10
booking-service.timeout = 10
```
When getting the config will fail, because `booking.service` will not match `booking-service` so it will not find the timeout.
```java
HttpPool pool = gestalt.getConfig("booking.service", HttpPool.class);
```
But given the properties
```properties
booking.service.pool.size = 10
booking.service.timeout = 10
```
These calls will succeed. When decoding `poolSize` it will first try a lowercase match of `poolsize`, but will not find the node, so it will try `pool.size` for a combined path of `booking.service.pool.size` which it will find.
```java
HttpPool pool = gestalt.getConfig("booking.service", HttpPool.class);
```
### Retrieving Primitive and boxed types
Getting primitive and boxed types involves calling Gestalt and providing the class of the type you are trying to retrieve.
```java
Short myShortWrapper = gestalt.getConfig("http.pool.maxTotal", Short.class);
short myShort = gestalt.getConfig("http.pool.maxTotal", short.class);
String serviceMode = gestalt.getConfig("serviceMode", String.class);
```
Gestalt will automatically decode and provide the value in the type you requested.
## Retrieving Complex Objects
To retrieve a complex object, you need to pass in the class for Gestalt to return. Gestalt will automatically use reflection to create the object, determine all the fields in the requested class, and then lookup the values in the configurations to inject into the object. It will attempt to use the setter fields first, then fallback to directly setting the fields.
There are two configuration options that allow you to control when errors are thrown when decoding complex objects.
```java
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);
```
### `treatMissingValuesAsErrors`
Treat missing field values in an object, proxy, record, or data object as errors. This will cause the API to either throw errors or return an empty optional.
- If this is `true`, any time a value that is not discretionary is missing, it will fail and throw an exception.
- If this is `false`, a missing value will be returned as `null` or the default initialization. `Null` for objects and `0` for primitives.
### `treatMissingDiscretionaryValuesAsErrors`
Treat missing discretionary values (optional, fields with defaults, fields with default annotations) in an object, proxy, record, or data object as errors.
- If this is `false`, you will be able to get the configuration with default values or an empty Optional.
- If this is `true`, if a field is missing and would have had a default, it will fail and throw an exception.
### `@Nullable` annotations
If a field or method is annotated with a `@Nullable` annotation, it will treat a missing value as a discretionary value. So as long as `treatMissingDiscretionaryValuesAsErrors` is not enabled, `@Nullable` fields will allow null values without throwing errors.
There are multiple `@Nullable` annotations and for this to work the annotations must use `@Retention(RetentionPolicy.RUNTIME)` so the annotation is available at runtime for Gestalt.
One good library to use is `jakarta.annotation:jakarta.annotation-api` that has a `@Nullable` with `@Retention(RetentionPolicy.RUNTIME)`.
#### Examples of required and discretionary fields.
Here are some examples of required and discretionary fields and which setting can control if they are treated as errors or allowed.
```java
public class DBInfo {
// discretionary value controlled by treatMissingValuesAsErrors
private Optional port; // default value Optional.empty()
private String uri = "my.sql.db"; // default value "my.sql.db"
private @Config(defaultVal = "100") Integer connections; // default value 100
// required value controlled by treatMissingDiscretionaryValuesAsErrors
private String password; // default value null
}
public interface DBInfoInterface {
Optional getPort(); // default value Optional.empty()
default String getUri() { // default value "my.sql.db"
return "my.sql.db";
}
@Config(defaultVal = "100")
Integer getConnections(); // default value 100
// required value controlled by treatMissingDiscretionaryValuesAsErrors
String getPassword(); // default value null
}
public record DBInfoRecord(
// discretionary value controlled by treatMissingDiscretionaryValuesAsErrors
@Config(defaultVal = "100") Integer connections, // default value 100
Optional port, // default value Optional.empty()
// required value controlled by treatMissingDiscretionaryValuesAsErrors
String uri, // default value null
String password // default value null
) {}
```
```kotlin
data class DBInfoDataDefault(
// discretionary value controlled by treatMissingValuesAsErrors
var port: Int?, // default value null
var uri: String = "my.sql.db", // default value "my.sql.db"
@Config(defaultVal = "100") var connections: Integer, // default value 100
// required value cam not disable treatMissingDiscretionaryValuesAsErrors and allow nulls.
var password: String, // required, can not be null.
)
```
### Retrieving Interfaces
To get an interface you need to pass in the interface class for gestalt to return.
Gestalt will use a proxy object when requesting an interface. When you call a method on the proxy it will look up the similarly named property, decode and return it.
```java
iHttpPool pool = gestalt.getConfig("http.pool", iHttpPool.class);
```
### Retrieving Generic objects
To get an interface you need to pass in a TypeCapture with the Generic value of the class for gestalt to return.
Gestalt supports getting Generic objects such as Lists, Maps or Sets. However, due to type erasure we need to capture the type using the TypeCapture class. The Generic can be any type Gestalt supports decoding such as a primitive wrapper or an Object.
```java
List httpPoolList = gestalt.getConfig("http.pool", new TypeCapture<>() { });
var httpPoolMap = gestalt.getConfig("http.pool", new TypeCapture>() { });
```
Gestalt supports multiple varieties of List such as AbstractList, CopyOnWriteArrayList, ArrayList, LinkedList, Stack, Vector, and SequencedCollection. If asked for a List it will default to an ArrayList.
Gestalt supports multiple varieties of Maps such as HashMap, TreeMap, ArrayList, LinkedHashMap and SequencedMap. If asked for a Map it will default to an HashMap.
Gestalt supports multiple varieties of Sets such as HashSet, TreeSet, LinkedHashSet, LinkedHashMap and SequencedSet. If asked for a Set it will default to an HashSet.
#### Config Data Type
For non-generic classes you can use the interface that accepts a class `HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);`, for Generic classes you need to use the interface that accepts a TypeCapture `List pools = gestalt.getConfig("http.pools", Collections.emptyList(),
new TypeCapture>() {});` to capture the generic type. This allows you to decode Lists, Sets and Maps with a generic type.
There are multiple ways to get a configuration with either a default, an Optional or the straight value. With the default and Optional Gestalt will not throw an exception if there is an error, instead returning a default or an PlaceHolder Option and log any warnings or errors.
#### Tags
The API also supports tagged configuration, where providing a tag will retrieve configs that match the specific tags or fallback to the default of no tags.
You can implement profiles or environments using tags.
```java
T getConfig(String path, T defaultVal, Class klass, Tags tags);
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class, Tags.of("environment", "dev"));
```
Most configuration sources support tagging them. So you can easily add tags to all properties in a source for your profile or environment.
```java
Gestalt gestalt = new GestaltBuilder()
.addSource(ClassPathConfigSourceBuilder.builder().setResource("/default.properties").build()) // Load the default property files from resources.
.addSource(FileConfigSourceBuilder.builder().setFile(devFile).setTags(Tags.of("environment", "dev")).build())
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.build();
```
There are utility methods for common tags such as profile and environment.
```java
Tags.profile("test") == Tags.of("profile", "test")
Tags.environment("dev") == Tags.of("environment", "dev")
```
##### Default Tags
You can set a default tag in the gestalt builder. The default tags are applied to all calls to get a gestalt configuration when tags are not provided. If the caller provides tags they will be used and the default tags will be ignored.
```java
Gestalt gestalt = new GestaltBuilder()
.addSource(ClassPathConfigSourceBuilder.builder().setResource("/default.properties").build()) // Load the default property files from resources.
.addSource(FileConfigSourceBuilder.builder().setFile(devFile).setTags(Tags.profile("dev").build()))
.addSource(FileConfigSourceBuilder.builder().setFile(testFile).setTags(Tags.profile("test").build()))
.setDefaultTags(Tags.profile("dev"))
.build();
// has implicit Tags of Tags.profile("dev") that is applied as the default tags, so it will use values from the devFile.
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);
// will use the Tags.profile("test") and ignore the default tags of Tags.profile("dev"), so it will use values from the testFile.
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class, Tags.profile("test"));
```
##### Config Node Tags Resolution Strategies.
By default, Gestalt expects tags to be an exact match to select the roots to search. This is configurable by setting a different `ConfigNodeTagResolutionStrategy` in the gestalt builder.
```java
Gestalt gestalt = new GestaltBuilder()
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.addSource(MapConfigSourceBuilder.builder()
.setCustomConfig(configs2)
.addTag(Tag.profile("orange"))
.addTag(Tag.profile("flower"))
.build())
.setConfigNodeTagResolutionStrategy(new SubsetTagsWithDefaultTagResolutionStrategy())
.build();
```
You can implement the interface `ConfigNodeTagResolutionStrategy` to define your own resolution strategy.
The available strategies are:
| name | Set Theory | Description |
|---------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| EqualTagsWithDefaultTagResolutionStrategy | Equals | Will Search two config node roots, the one that is an equal match to the tags and the root with no tags. Then return the config node roots to be searched. Only return the roots if they exist. |
| SubsetTagsWithDefaultTagResolutionStrategy | Subset | Will Search for any roots that are a subset of the tags provided with a fallback of the default root. In combination with default tags, this can be used to create a profile system similar to Spring Config. |
##### Tags Merging Strategies.
You can provide tags to gestalt in two ways, setting the defaults in the gestalt config and passing in tags when getting a configuration.
```java
Gestalt gestalt = new GestaltBuilder()
.addSource(ClassPathConfigSourceBuilder.builder().setResource("/default.properties").build()) // Load the default property files from resources.
.addSource(FileConfigSourceBuilder.builder().setFile(devFile).setTags(Tags.profile("dev").build()))
.addSource(FileConfigSourceBuilder.builder().setFile(testFile).setTags(Tags.profile("test").build()))
.setDefaultTags(Tags.profile("dev"))
.build();
// will use the Tags.profile("test") and ignore the default tags of Tags.profile("dev"), so it will use values from the testFile.
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class, Tags.profile("test"));
```
The default behaviour is to use the provided tags with the `getConfig` and if not provided, fall back to the defaults.
By passing in the TagMergingStrategy to the GestaltBuilder, you can set your own strategy.
```java
Gestalt gestalt = new GestaltBuilder()
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.addSource(MapConfigSourceBuilder.builder()
.setCustomConfig(configs2)
.addTag(Tag.profile("orange"))
.addTag(Tag.profile("flower"))
.build())
.setTagMergingStrategy(new TagMergingStrategyCombine())
.build();
```
The available strategies are:
| name | Set Theory | Description |
|--------------------------------|--------------|-------------------------------------------------------------------------------------|
| TagMergingStrategyFallback | exclusive or | Use the provided tags with `getConfig`, and if not provided use a default fallback. |
| TagMergingStrategyCombine | union | Merge the provided tags with `getConfig`, and the defaults |
You can provide your own strategy by implementing TagMergingStrategy.
#### Example
Example of how to create and load a configuration objects using Gestalt:
```java
public static class HttpPool {
public short maxTotal;
public long maxPerRoute;
public int validateAfterInactivity;
public double keepAliveTimeoutMs = 6000; // has a default value if not found in configurations
public OptionalInt idleTimeoutSec = 10; // has a default value if not found in configurations
public float defaultWait = 33.0F; // has a default value if not found in configurations
public HttpPool() {
}
}
public static class Host {
private String user;
private String url;
private String password;
private Optional port;
public Host() {
}
// getter and setters ...
}
...
// load a whole class, this works best with pojo's
HttpPool pool = gestalt.getConfig("http.pool", HttpPool.class);
// or get a spcific config value
short maxTotal = gestalt.getConfig("http.pool.maxTotal", Short.class);
// get with a default if you want a fallback from code
long maxConnectionsPerRoute = gestalt.getConfig("http.pool.maxPerRoute", 24, Long.class);
// get a list of objects, or an PlaceHolder collection if there is no hosts found.
List hosts = gestalt.getConfig("db.hosts", Collections.emptyList(),
new TypeCapture>() {});
```
#### Kotlin
In Kotlin you dont need to specify the types if you used the kotlin extension methods provided in `gestalt-kotlin`. It uses inline reified methods that automatically capture the type for you based on return type. If no configuration is found and the type is nullable, it will return null otherwise it will throw an GestaltException.
```kotlin
data class HttpPool(
var maxTotal: Short = 0,
var maxPerRoute: Long = 0,
var validateAfterInactivity: Int? = 0,
var keepAliveTimeoutMs: Double = 6000.0,
var idleTimeoutSec: Short = 10,
var defaultWait: Float = 33.0f
)
// load a kotlin data class
val pool: HttpPool = gestalt.getConfig("http.pool")
// get a list of objects, or an PlaceHolder collection if there is no hosts found.
val hosts: List = gestalt.getConfig("db.hosts", emptyList())
```
# Annotations
When decoding a Java Bean style class, a record, an interface or a Kotlin Data Class you can provide a custom annotation to override the path for the field as well as provide a default.
The field annotation `@Config` takes priority if both the field and method are annotated.
The class annotation `@ConfigPrefix` allows the user to define the prefix for the config object as part of the class instead of the `getConfig()` call. If you provide both the resulting prefix is first the path in getConfig then the prefix in the `@ConfigPrefix` annotation.
For example using `@ConfigPrefix(prefix = "connection")` with `DBInfo pool = gestalt.getConfig("db", DBInfo.class);` the resulting path would be `db.connection`.
```java
@ConfigPrefix(prefix = "db")
public class DBInfo {
@Config(path = "channel.port", defaultVal = "1234")
private int port;
public int getPort() {
return port;
}
}
DBInfo pool = gestalt.getConfig("", DBInfo.class);
public class DBInfo {
private int port;
@Config(path = "channel.port", defaultVal = "1234")
public int getPort() {
return port;
}
}
DBInfo pool = gestalt.getConfig("db.connection", DBInfo.class);
```
The path provided in the annotation is used to find the configuration from the base path provided in the call to Gestalt getConfig.
So if the base path from gestalt.getConfig is `db.connection` and the annotation is `channel.port` the path the configuration will look for is `db.connection.channel.port`
The default accepts a string type and will be decoded into the property type using the gestalt decoders. For example if the property is an Integer and the default is "100" the integer value will be 100.
# Annotations Configurations
Certain annotations can be applied to a configuration using `@{annotation}`, this will covert the annotation to metadata that can be applied to the node. Then the metadata is used to apply the intended behaviour to the node.
For example, we can apply the temporary node feature on a node by using the annotation `@{temp:1}`
```properties
my.password=abcdef@{temp:1}
```
| annotation | parameter | description |
|------------|--------------------------------------------------|----------------------------------------------------------------------------------------------|
| temp | (int) Number of times this temp node can be read | restrict the number of times a value can be read before it is released |
| encrypt | (boolean) if we should apply to this node | Encrypts the node in memory. |
| nocache | (boolean) if we should apply to this node | Will not cache the node. If a node is part of a object the whole object will not be cached. |
| secret | (boolean) if we should apply to this node | Treats the node as a secret, so it will not print it out in errors or the debug print. |
## Trim Whitespace
By default, white spaces before and after the annotation are trimmed. You can disable this feature using the gestalt builder and setting `setAnnotationTrimWhiteSpace(false)`
```java
GestaltBuilder builder = new GestaltBuilder();
Gestalt gestalt = builder
.addSource(MapConfigSourceBuilder.builder()
.setCustomConfig(configs)
.build())
.setAnnotationTrimWhiteSpace(false)
.build();
```
# Searching for path while Decoding Objects
When decoding a class, we need to know what configuration to lookup for each field. To generate the name of the configuration to lookup, we first find the path as defined in the call to `gestalt.getConfig("book.service", HttpPool.class)` where the path is `book.service`. We do not apply the path mappers to the path, only the config tree notes from the path. Once at the path we check class for any annotations. If there are no annotations, then we search for the fields by exact match. So we look for a config value with the same name as the field. If it is unable to find the exact match, it will attempt to map it to a path based on camel case. Where the camel case words will be separated and converted to Kebab case, Snake case and Dot Notation, then used to search for the configuration.
The order is descending based on the priority of the mapper.
| Casing | Priority | Class Name |
|--------------------------|----------|-----------------------|
| Camel Case (exact match) | 1000 | StandardPathMapper |
| Kebab Case | 600 | KebabCasePathMapper |
| Snake Case | 550 | SnakeCasePathMapper |
| Dot Notation | 500 | DotNotationPathMapper |
Given the following class lets see how it is translated to the different casings:
```java
// With a class of
public static class DBConnection {
@Config(path = "host")
private String uri;
private int dbPort;
private String dbPath;
}
```
Kebab Case:
All objects in Java use the standard Camel case, however in the config files you can use Kebab case, and if an exact match isnt found it will search for a config variable converting Camel case into Kebab case.
Kebab case or an exact match are preferred as using dot notation could potentially cause some issues as it is parsed to a config tree. Using dot notation you would need to ensure that none of values break the tree rules.
```java
// Given a config of
"users.host" => "myHost"
"users.uri" => "myHost"
"users.dbPort" => "1234"
"users.db-path" => "usersTable"
// the uri will map to host
// the dbPort will map to dbPort using Camel case using exact match.
// the dbPath will automatically map to db.path using Kebab case.
DBConnection connection = gestalt.getConfig("users", TypeCapture.of(DBConnection.class));
```
Snake Case:
All objects in Java use the standard Camel case, however in the config files you can use Snake case, and if an exact match isnt found it will search for a config variable converting Camel case into snake case.
```java
// Given a config of
"users.host" => "myHost"
"users.uri" => "myHost"
"users.dbPort" => "1234"
"users.db_path" => "usersTable"
// the uri will map to host
// the dbPort will map to dbPort using Camel case using exact match.
// the dbPath will automatically map to db_path using snake case
DBConnection connection = gestalt.getConfig("users", TypeCapture.of(DBConnection.class));
```
Dot Notation:
All objects in Java use the standard Camel case, however in the config files you can use Dot Notation, and if an exact match isnt found it will search for a config variable converting Camel case into Dot Notation.
Kebab case or an exact match are preferred as using dot notation could potentially cause some issues as it is parsed to a config tree. Using dot notation you would need to ensure that none of values break the tree rules.
```java
// Given a config of
"users.host" => "myHost"
"users.uri" => "myHost"
"users.dbPort" => "1234"
"users.db.path" => "usersTable"
// the uri will map to host
// the dbPort will map to dbPort using Camel case using exact match.
// the dbPath will automatically map to db.path using dot notation.
DBConnection connection = gestalt.getConfig("users", TypeCapture.of(DBConnection.class));
```
# Kotlin
For Kotlin Gestalt includes several extension methods that allow easier use of Gestalt by way of reified functions to better capture the generic type information.
Using the extension functions you don't need to specify the type if the return type has enough information to be inferred. If nothing is found it will throw a GestaltException unless the type is nullable, then it will return null.
```kotlin
val pool: HttpPool = gestalt.getConfig("http.pool")
val hosts: List = gestalt.getConfig("db.hosts", emptyList())
```
| Gestalt Version | Kotlin Version |
|------------------|----------------|
| 0.35.0 + | 2.1 |
| 0.25.0 + | 1.9 |
| 0.17.0 + | 1.8 |
| 0.13.0 to 0.16.6 | 1.7 |
| 0.10.0 to 0.12.0 | 1.6 |
| 0.9.0 to 0.9.3 | 1.5 |
| 0.1.0 to 0.8.1 | 1.4 |
# Node Substitution (include nodes)
Using the `$include` keyword as part of a config path, you can include the referenced config node tree into the path provided. By default, the node is merged into the provided node under the current node as defaults that will be overridden. You can control the order of the nodes, by including a number where < 0 is included below the current node and > 0 is included above the current node. The root node is always 0. Having two nodes share the same order is undefined. For example: `$include:-1` for included under the current node, and `$include:1` for included over the current node.
If you are included multiple nodes each node must have an order, or the results are undefined, and some includes may be lost.
You can include into the root or any sub node. It also supports nested include.
The include node must provide a source that is used to determine how to include the source. Each source accepts different parameters that can be provided in the form of a key value with a comma separated list. One of the key value pairs must be `source` that is used to determine the source type.
For example a classPath source with the resource `includes.properties` would look like:
```properties
$include=source=classPath,resource=includes.properties
```
Example of include a classPath Node into a sub path with properties file `imports.properties`.
```properties
b=b changed
c=c
```
In the first example we include the loaded file node with default settings of order -1 `$include:-1`, where the root node is always order 0. So the node will be loaded under the current root nodes so will provide defaults that will be overwritten.
```java
Map configs = new HashMap<>();
configs.put("a", "a");
configs.put("b", "b");
configs.put("$include", "source=classPath,resource=includes.properties");
Gestalt gestalt = new GestaltBuilder()
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.build();
gestalt.loadConfigs();
Assertions.assertEquals("a", gestalt.getConfig("a", String.class));
Assertions.assertEquals("b", gestalt.getConfig("b", String.class));
Assertions.assertEquals("c", gestalt.getConfig("c", String.class));
```
That is why we don't see `b=b changed` as it will be overwritten by `b=b`, but we still see `c=c` as it was in the included defaults and not overwritten.
In this second example we include the node with `$include:1`. Since the root node is always order 0, the included nodes will override the root.
```java
Map configs = new HashMap<>();
configs.put("a", "a");
configs.put("b", "b");
configs.put("$include:1", "source=classPath,resource=includes.properties");
Gestalt gestalt = new GestaltBuilder()
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.build();
gestalt.loadConfigs();
Assertions.assertEquals("a", gestalt.getConfig("a", String.class));
Assertions.assertEquals("b changed", gestalt.getConfig("b", String.class));
Assertions.assertEquals("c", gestalt.getConfig("c", String.class));
```
That is why we see `b=b changed` as it is overwritten the root `b=b`.
In the final example, we include the loaded file node in the sub path `sub`.
```java
Map configs = new HashMap<>();
configs.put("a", "a");
configs.put("b", "b");
configs.put("sub.a", "a");
configs.put("sub.$include:1", "source=classPath,resource=includes.properties");
Gestalt gestalt = new GestaltBuilder()
.addSource(MapConfigSourceBuilder.builder().setCustomConfig(configs).build())
.build();
gestalt.loadConfigs();
Assertions.assertEquals("a", gestalt.getConfig("a", String.class));
Assertions.assertEquals("b", gestalt.getConfig("b", String.class));
Assertions.assertEquals("a", gestalt.getConfig("sub.a", String.class));
Assertions.assertEquals("b changed", gestalt.getConfig("sub.b", String.class));
Assertions.assertEquals("c", gestalt.getConfig("sub.c", String.class));
```
As you can see the nodes from the file `includes.properties` were included in the sub path `sub`. As can bee seen with `sub.b = b changed` and `sub.c = c`.
Supported substitution sources:
| Source Type | Module | Parameter | Description |
|-------------|----------------------|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| classPath | gestalt-core | resource | The name of the classpath resource to load. |
| node | gestalt-core | path | Load an node at the given path into the current node. |
| env | gestalt-core | failOnErrors | If we should fail on errors. Since Env Vars may not always conform to Gestalt expectations we can disable the errors and make it more lenient while loading Env Vars. |
| | | prefix | Only include Env Vars that match the prefix. |
| | | ignoreCaseOnPrefix | When matching the prefix should it ignore case. |
| | | removePrefix | If we should remove the prefix after matching. |
| file | gestalt-core | file | Load a file at a given location to the current node. |
| | | path | Load a file as a path at a given location to the current node. |
| k8Secret | gestalt-core | path | The directory to scan for kubernetes secrets. |
| | | file | The file directory to scan for kubernetes secrets. |
| system | gestalt-core | failOnErrors | If we should fail on errors. Since System Variables may not always conform to Gestalt expectations we can disable the errors and make it more lenient while loading System Vars. |
| s3 | gestalt-aws | Module Config | To use S3 with the include node feature you must register an `S3Client` via the AWSModuleConfig: ```Gestalt gestalt = builder.addModuleConfig(AWSBuilder.builder().setRegion("us-east-1").setS3Client(s3Client).build()).build();``` |
| | | bucket | The S3 bucket to search in. |
| | | key | The Key of the config file to load. |
| blob | gestalt-azure | Module Config | To use Azure Blob with the include node feature you must register an `BlobClient` or a `StorageSharedKeyCredential` via the AzureModuleBuilder : ```Gestalt gestalt = builder.addModuleConfig(AzureModuleBuilder.builder().setBlobClient(blobClient).build())).build();``` |
| | | endpoint | Azure endpoint to access the blob storage. |
| | | container | Azure Container containing the blob. |
| | | blob | The blob with the file. |
| git | gestalt-git | Module Config | When accessing private repos you must register the `GitModuleConfig` with Gestalt. ```Gestalt gestalt = new GestaltBuilder().addModuleConfig(GitModuleConfigBuilder.builder().setCredentials(new UsernamePasswordCredentialsProvider(userName, password)).build()).build(); ``` |
| | | repoURI | Where to locate the repo |
| | | branch | What branch to find the config files. |
| | | configFilePath | The subpath in the repo URI to find the config file. |
| | | localRepoDirectory | Where to save the git files Gestalt Syncs. |
| gcs | gestalt-google-cloud | Module Config | To use GCS with the include node feature you can register a `Storage` client via the GoogleModuleConfig : ```Gestalt gestalt = builder.addModuleConfig(GoogleModuleConfigBuilder.builder().setStorage(storage).build())).build();```. Otherwise it will fallback to the default storage client. |
| | | bucketName | What bucket to find the config files. |
| | | objectName | The specific config file to include. |
# String Substitution
Gestalt supports string substitutions using `${}` at load time on configuration properties to dynamically modify configurations.
For example if we have a properties file with a Database connection you don't want to save your usernames and passwords in the properties files. Instead, you want to inject the username and passwords as Environment Variables.
```properties
db.user=${DB_USER}
db.password=${DB_PASSWORD}
```
You can use multiple string replacements within a single string to build a configuration property.
```properties
db.uri=jdbc:mysql://${DB_HOST}:${DB_PORT}/${environment}
```
### Load time vs run time
Load time `${}` substitutions are evaluated when we load the configurations and build the config tree. This is done once on `gestalt.load()` then all results are cached in the config tree and returned.
Run time `#{}` substitutions are evaluated at runtime when you call `gestalt.getConfig(...);`, the results are not cached and each time you call `gestalt.getConfig(...);` you will re-evaluate the value.
It is recommended to use Load time `${}` substitutions in the vast majority of cases as it is more performant. The main use case for run time `#{}` substitutions is for values you expect to change from one call to the next, such as wanting a different random number each time you call `gestalt.getConfig(...);`.
Aside from evaluated time, the syntax and use of both `${}` and `#{}` are otherwise identical, you can mix and match them as needed.
### Specifying the Transformer
You can specify the substitution in the format ${transform:key} or ${key}. If you provide a transform name it will only check that one transform. Otherwise, it will check all the Transformer annotated with a `@ConfigPriority` in descending order and will return the first matching value.
Unlike the rest of Gestalt, this is case-sensitive, and it does not tokenize the string (except the node transform). The key expects an exact match, so if the Environment Variable name is DB_USER you need to use the key DB_USER. Using db.user or db_user will not match.
```properties
db.uri=jdbc:mysql://${DB_HOST}:${map:DB_PORT}/${sys:environment}
```
### Defaults for a Substitution
You can provide a default for the substitution in the format `${transform:key:=default}` or `${key:=default}`. If you provide a default it will use the default value in the event that the key provided cant be found
```properties
db.uri=jdbc:mysql://${DB_HOST}:${map:DB_PORT:=3306}/${environment:=dev}
```
Using nested substitution, you can have a chain of defaults. Where you can fall back from one source to another.
```properties
test.duration=${sys:duration:=${env:TEST_DURATION:=120}}
```
In this example, it will first try the system variable `duration`, then the Environment Variable `TEST_DURATION` and finally if none of those are found, it will use the default `120`
### Escaping a Substitution
You can escape the value with '\' like `\${my text}` to prevent the substitution. In Java you need to write `\\` to escape the character in a normal string but not in a Text block
In nested substitutions you should escape both the opening token `\${` and the closing token `\}` to be clear what is escaped, otherwise you may get undetermined results.
```properties
user.block.message=You are blocked because \\${reason\\}
```
### Nested Substitutions
Gestalt supports nested and recursive substitutions. Where a substitution can happen inside another substitution and the results could trigger another substitution.
Please use nested substitution sparingly, it can get very complex and confusing quickly.
Using these variables:
Environment Variables:
```properties
DB_HOST=cloudHost
environment=dev
```
System Variables:
```properties
DB_HOST=localHost
environment=test
```
Map Variable:
```properties
DB_TRANSFORM=sys
DB_PORT=13306
```
config source:
```properties
db.uri=jdbc:mysql://${${DB_TRANSFORM}:DB_HOST}:${map:DB_PORT}/${sys:environment}
```
This will resolve ${DB_TRANSFORM} => `sys`
then resolve ${sys:DB_HOST} => `localHost`
For a configuration value of `db.uri=jdbc:mysql://localHost:13306/test`
Nested substitution resolving to a nested substitution.
Given properties:
```properties
this.path = greeting
your.path = ${this.path}
my.path.greeting = good day
```
And a string to Substitute:
`"${my.path.${your.path}}"`
the result is `good day`
`${your.path}` resolves to `${this.path}`
`${this.path}` is then resolved to `greeting`
And finally the path `my.path.greeting` is resolved to `good day`
### Provided Transformers
| keyword | priority | source |
|--------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| env | 100 | Environment Variables |
| envVar | 100 | **Deprecated** Environment Variables |
| sys | 200 | Java System Properties |
| map | 400 | A custom map provided to the constructor |
| node | 300 | map to another leaf node in the configuration tree |
| random | n/a | provides a random value |
| base64Decode | n/a | decode a base 64 encoded string |
| base64Encode | n/a | encode a base 64 encoded string |
| classpath | n/a | load the contents of a file on the classpath into a string substitution. |
| dist100 | n/a | Use a comma-separated list, where each element is a colon-separated pair of a threshold and its corresponding value. If an element has no threshold, it is treated as the default. For example, the format `10:red,30:green,blue` defines ranges and outcomes for random distributions: numbers from `1 to 10` correspond to `red`, `11 to 30` correspond to `green`, and all numbers above `30` `default` to `blue`. This is best used with runtime evaluation using `#{dist100:10:red,30:green,blue}` |
| file | n/a | load the contents of a file into a string substitution |
| urlDecode | n/a | URL decode a string |
| urlEncode | n/a | URL encode a string |
| awsSecret | n/a | An AWS Secret is injected for the secret name and key. Configure the AWS Secret by registering a AWSModuleConfig using the AWSBuilder. ```Gestalt gestalt = builder.addModuleConfig(AWSBuilder.builder().setRegion("us-east-1").build()).build();``` |
| azureSecret | n/a | An Azure Secret is injected for the secret name and key. Configure the Azure Secret by registering a AzureModuleConfig using the AzureModuleBuilder. ```Gestalt gestalt = builder.addModuleConfig(AzureModuleBuilder.builder().setKeyVaultUri("test").setCredential(tokenCredential)).build();``` |
| gcpSecret | n/a | A Google Cloud Secret given the key provided. Optionally configure the GCP Secret by registering an GoogleModuleConfig using the GoogleBuilder, or let google use the defaults. ``` Gestalt gestalt = builder.addModuleConfig(GoogleBuilder.builder().setProjectId("myProject").build()).build()``` |
| vault | n/a | A vault Secret given the key provided. Configure the Vault Secret by registering an VaultModuleConfig using the VaultBuilder. ``` Gestalt gestalt = builder.addModuleConfig(VaultBuilder.builder().setVault(vault).build()).build()```. Uses the io.github.jopenlibs:vault-java-driver project to communicate with vault |
### Random String Substitution
To inject a random variable during config node processing you can use the format ${random:type(origin, bound)}
The random value is generated while loading the config, so you will always get the same random value when asking gestalt.
```properties
db.userId=dbUser-${random:int(5, 25)}
app.uuid=${random:uuid}
```
#### Random Options supported:
| data type | format | notes |
|-----------|-----------------------|------------------------------------------------------|
| byte | byte | a random byte of data base 64 encoded |
| byte | byte(length) | random bytes of provided length base 64 encoded |
| int | int | a random int of all possible int values |
| int | int(max) | a random int from 0 to the max value provided |
| int | int(origin, bound) | a random int between origin and bound |
| long | long | a random long of all possible long values |
| long | long(max) | a random long from 0 to the max value provided |
| long | long(origin, bound) | a random long between origin and bound |
| float | float | a random float between 0 and 1 |
| float | float(max) | a random float from 0 to the max value provided |
| float | float(origin, bound) | a random float between origin and bound |
| double | double | a random double of all possible long values |
| double | double(max) | a random double from 0 to the max value provided |
| double | double(origin, bound) | a random double between origin and bound |
| boolean | boolean | a random boolean |
| string | string | a random string of characters a-z of length 1 |
| string | string(length) | a random string of characters a-z of length provided |
| char | char | a random char of characters a-z |
| uuid | uuid | a random uuid |
* Note: The formats in the table would need to be embedded inside of ${random:format} so byte(length) would be ${random:byte(10)}
# Tags
When adding a config source you are able to apply zero or more Tags to the source. Those tags are then applied to all configuration within that source. Tags are optional and can be omitted.
When retrieving the config it will first search for an exact match to the tags, if provided, then search for the configs with no tags. It will then merge the results.
If you provide 2 tags in the source, when retrieving the configuration you must provide those two exact tags.
```java
// head.shot.multiplier = 1.3
// max.online.players = 32
ConfigSourcePackage pveConfig = ClassPathConfigSourceBuilder.builder().setResource("/test-pve.properties").setTags(Tags.of("mode", "pve")).build();
// head.shot.multiplier = 1.5
ConfigSourcePackage pvpConfig = ClassPathConfigSourceBuilder.builder().setResource("/test-pvp.properties").setTags(Tags.of("mode", "pvp")).build();
// head.shot.multiplier = 1.0
// gut.shot.multiplier = 1.0
ConfigSourcePackage defaultConfig = ClassPathConfigSourceBuilder.builder().setResource("/test.properties").setTags(Tags.of()).build(); // Tags.of() can be omitted
Gestalt gestalt = builder
.addSource(pveConfig)
.addSource(pvpConfig)
.addSource(defaultConfig)
.build();
// retrieving "head.shot.multiplier" values change depending on the tag.
float pvpHeadShot = gestalt.getConfig("head.shot.multiplier", Float.class, Tags.of("mode", "pve")); // 1.3
float pveHeadShot = gestalt.getConfig("head.shot.multiplier", Float.class, Tags.of("mode", "pvp")); // 1.5
float coopHeadShot = gestalt.getConfig("head.shot.multiplier", Float.class, Tags.of("mode", "coop")); // 1.0 fall back to default
float defaultHeadShot = gestalt.getConfig("head.shot.multiplier", Float.class); // 1.0
// Gut shot is only defined in the default, so it will always return the default.
float pvpGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "pve")); // 1.0
float pveGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "pvp")); // 1.0
float coopGutSoot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "coop")); // 1.0
float defaultGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class); // 1.0
// Max online players is only defined in the pvp, so it will only return with the pvp tags.
float pvpGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "pve")); // 32
float pveGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "pvp")); // not found
float coopGutSoot = gestalt.getConfig("gut.shot.multiplier", Float.class, Tags.of("mode", "coop")); // not found
float defaultGutShot = gestalt.getConfig("gut.shot.multiplier", Float.class); // not found
```
* **Note**: The config node processor string replacement doesn't accept tags, so it will always replace the configs with the tag-less ones.
## Supported config sources
| Config Source | module | Details |
|-------------------------------|----------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| BlobConfigSource | [`gestalt-azure`](https://search.maven.org/search?q=gestalt-azure) | Loads a config source from Azure Blob, Must include package com.github.gestalt-config:gestalt-azure:version. |
| ClassPathConfigSource | gestalt-core | Load a file from the java class path. Uses getResourceAsStream to find and load the InputStream. |
| EnvironmentConfigSource | gestalt-core | Loads all Environment Variables in the system. It expects Env Vars to be in screaming snake case, and will parse the "_" as a path delimiter. will convert them to a list of key values from the Env Map for the config loader. You can provide a prefix to only load Environment Variables with the prefix. Then you can choose to keep the prefix or remove it. |
| FileConfigSource | gestalt-core | Loads a file from the local file system. The format for the source will depend on the file extension of the file. For example if it is dev.properties, the format will be properties. Returns a InpuStream for the config loader. |
| InputStreamConfigSource | gestalt-core | Load a configuration from a InputStream. The format for the source will depend on the file extension of the file. For example if it is dev.properties, the format will be properties. Returns a InpuStream for the config loader. |
| KubernetesSecretConfigSource | gestalt-core | Specify a path to search for [kubernetes secrets](https://kubernetes.io/docs/concepts/configuration/secret/) files. The directory is scanned and each file is added to the configuration. The name of the file is treated as the key for configuration and the content of the file is the value for the configuration. |
| GCSConfigSource | [`gestalt-google`](https://search.maven.org/search?q=gestalt-google) | Load a config from Google Cloud Storage. Requires a bucketName and a objectName. A google Storage object is optional, otherwise it defaults to the default instance. |
| GitConfigSource | [`gestalt-git`](https://search.maven.org/search?q=gestalt-git) | Syncs a remote repo locally then uses the files to build a configuration. This uses jgit and supports several forms of authentication. See GitConfigSourceTest.java for examples of use. |
| MapConfigSource | gestalt-core | Allows you to pass in your own map, it will convert the map into a list of path and value for the config loader. |
| StringConfigSource | gestalt-core | Takes any string and converts it into a InputStream. You must also provide the format type so we can match it to a loader. |
| SystemPropertiesConfigSource | gestalt-core | Loads the Java System Properties and convert them to a list of key values or the config loader. |
| S3ConfigSource | [`gestalt-aws`](https://search.maven.org/search?q=gestalt-aws) | Loads a config source from AWS S3, Must include package com.github.gestalt-config:gestalt-aws:version. |
| URLConfigSource | gestalt-core | Loads a config source from a URL. |
# Config Loader
Each config loader understands how to load a specific type of config. Often this is associated with a specific ConfigSource. For example the EnvironmentVarsLoader only loads the EnvironmentConfigSource. However, some loaders expect a format of the config, but accept it from multiple sources. For example the PropertyLoader expects the typical java property file, but it can come from any source as long as it is an input stream. It may be the system properties, local file, github, or S3.
| Config Loader | Formats supported | details | module |
|-----------------------|-----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
| EnvironmentVarsLoader | envVars | Loads Environment Variables from the EnvironmentConfigSource, it expects a list not a InputStream. By default, it splits the paths using a "_". You can also disable failOnErrors if you are receiving errors from the environment variables, as you can not always control what is present. By treating Errors as warnings it will not fail if it finds a configuration the parser doesn't understand. Instead it will ignore the specific config. | core |
| MapConfigLoader | mapConfig | Loads a user provided Map from the MapConfigSource, it expects a list not a InputStream. By default, it splits the paths using a "." and tokenizes arrays with a numeric index as "[0]". | core |
| PropertyLoader | properties, props, and systemProperties | Loads a standard property file from an InputStream. By default, it splits the paths using a "." and tokenizes arrays with a numeric index as "[0]". | core |
| JsonLoader | json | Leverages Jackson to load json files and convert them into a ConfigNode tree. | [`gestalt-json`](https://search.maven.org/search?q=gestalt-json) |
| TomlLoader | toml | Leverages Jackson to load toml files and convert them into a ConfigNode tree. | [`gestalt-toml`](https://search.maven.org/search?q=gestalt-toml) |
| YamlLoader | yml and yaml | Leverages Jackson to load yaml files and convert them into a ConfigNode tree. | [`gestalt-yaml`](https://search.maven.org/search?q=gestalt-yaml) |
| HoconLoader | config | Leverages com.typesafe:config to load hocon files, supports substitutions. | [`gestalt-hocon`](https://search.maven.org/search?q=gestalt-hocon) |
If you didn't manually add any ConfigLoaders as part of the GestaltBuilder, it will add the defaults. The GestaltBuilder uses the service loader to create instances of the Config loaders. It will configure them by passing in the GestaltConfig to applyConfig.
To register your own default ConfigLoaders add them to the builder, or add it to a file in META-INF\services\org.github.gestalt.config.loader.ConfigLoader and add the full path to your ConfigLoader
By default, Gestalt expects Environment Variables to be screaming snake case, but you can configure it to have a different case.
By registering a `EnvironmentVarsLoaderModuleConfig` with the `GestaltBuilder` you can customize the Environment Loader.
In this example it will expect double `__` as delimiter.
```java
GestaltBuilder builder = new GestaltBuilder();
Gestalt gestalt = builder
.addSource(EnvironmentConfigSourceBuilder.builder().build())
.addModuleConfig(EnvironmentVarsLoaderModuleConfigBuilder
.builder()
.setLexer(new PathLexer("__"))
.build())
.build();
gestalt.loadConfigs();
```
You can also customize many of the Loaders such as the `YamlLoader`, `TomlLoader`, `JsonLoader` and `HoconLoader` by registering the Module Configs with the builder.
```java
GestaltBuilder builder = new GestaltBuilder();
Gestalt gestalt = builder
.addSource(ClassPathConfigSourceBuilder.builder().setResource("/default.yaml").build())
.addModuleConfig(YamlModuleConfigBuilder.builder()
.setObjectMapper(customObjectmapper)
.build())
.build();
gestalt.loadConfigs();
```
# Decoders
| Type | details |
|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Array | Java primitive array type with any generic class, Can decode simple types from a single comma separated value, or from an array node. You can escape the comma with a \\, so the values are not split. |
| BigDecimal | |
| BigInteger | |
| Boolean | Boolean and boolean |
| Byte | Byte and byte |
| Char | Char and char |
| ConfigContainer | A container that caches your config value, and updates it when there is a configuration change. |
| Date | takes a DateTimeFormatter as a parameter, by default it uses DateTimeFormatter.ISO_DATE_TIME |
| Double | Double and double |
| Duration | Decodes a duration from either a number or an ISO 8601 standardized string format like "PT42S". |
| Enum | |
| File | |
| Float | Float and float |
| Instant | |
| Integer | Integer and int |
| List | a Java list with any Generic class, Can decode simple types from a single comma separated value, or from an array node. You can escape the comma with a \\, so the values are not split. Supports multiple varieties of List such as AbstractList, CopyOnWriteArrayList, ArrayList, LinkedList, Stack, Vector, and SequencedCollection. If asked for a List it will default to an ArrayList. |
| LocalDate | Takes a DateTimeFormatter as a parameter, by default it uses DateTimeFormatter.ISO_LOCAL_DATE |
| LocalDateTime | Takes a DateTimeFormatter as a parameter, by default it uses DateTimeFormatter.ISO_DATE_TIME