Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jsixface/yamlconfig
Manage configuration for java project in a YAML file and access them via dotted notation.
https://github.com/jsixface/yamlconfig
configuration java properties yaml yml
Last synced: 3 months ago
JSON representation
Manage configuration for java project in a YAML file and access them via dotted notation.
- Host: GitHub
- URL: https://github.com/jsixface/yamlconfig
- Owner: jsixface
- License: apache-2.0
- Created: 2019-03-01T00:45:44.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-15T12:34:16.000Z (10 months ago)
- Last Synced: 2024-10-09T05:53:41.941Z (4 months ago)
- Topics: configuration, java, properties, yaml, yml
- Language: Java
- Size: 103 KB
- Stars: 8
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# YamlConfig
[![Build Package](https://github.com/jsixface/YamlConfig/actions/workflows/build-package.yml/badge.svg)](https://github.com/jsixface/YamlConfig/actions/workflows/build-package.yml)[Yaml](https://en.wikipedia.org/wiki/YAML) is a data serialization format similar to JSON but more human readable.
It looks better to organize config in a YAML file since it makes sense to maintain some properties in a hierarchical manner.**YamlConfig** helps read configuration for a java project from a YAML config file and access them via dotted notation.
## Features
- Uses SnakeYAML for reading YAML, so it can handle any data recognizable by SnakeYAML.
- Ease of access using dotted notation to read properties## Getting Started
The latest jar can be downloaded from the [releases](https://github.com/jsixface/YamlConfig/releases) page.If you use Maven for Dependency management, you can include this using below dependency.
```
com.github.jsixface
yamlconfig
1.2.0```
Or if you use Gradle, you can include this using below dependency.```
implementation 'com.github.jsixface:yamlconfig:1.2.0'
```## Usage - internal Yaml
Get an instance of the YamlConfig by passing in a reader or an inputstream.```
InputStream resource = getClass()
.getClassLoader()
.getResourceAsStream("config.yml");YamlConfig config = YamlConfig.load(resource);
```Assume the contents of `config.yml` is as below:
```
services:
db:
image: mysql
container_name: mysql_db
endpoint:
- host: example.com
port: 2976
- host: example.com
port: 2978
```You can access the value of db image by:
```
String imgName = config.getString("services.db.image");
```
It can also be used to get through arrays like below:```
String value = config.getString("services.endpoint[1].host");
```## Usage - externally supplied Yaml
As above, but with you supplying the Yaml instance, allowing Yaml to template environment variables to override yaml supplied values.```
# externally managed and configured Yaml instance.
Yaml yaml = new Yaml(new EnvScalarConstructor());
yaml.addImplicitResolver(EnvScalarConstructor.ENV_TAG, EnvScalarConstructor.ENV_FORMAT, "$");# externally managed yml file
InputStream resource = getClass()
.getClassLoader()
.getResourceAsStream("config.yml");
# pass the lot to YamlConfig
YamlConfig config = YamlConfig.load(yaml, resource);# and now you can use fully qualified dot notation keys:
config.getString("service.db.someKey");
```
allowing `*.yml` files to contain `key: ${ENV_KEY:-defaultValue}` like this:
```yaml
services:
db:
someKey: ${SOME_KEY:-defaultValue}
```
see the test [YamlConfigWithEnvOverridesTest.java](src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java)
and its config file: [testWithEnvOverrides.yml](src/test/resources/testWithEnvOverrides.yml)