From e813737bf2820cb456e4728dad0cf5c0a5f39bf2 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 21:50:10 +0200 Subject: [PATCH 01/19] ci: remove codeql analysis --- .github/workflows/codeql-analysis.yml | 71 --------------------------- 1 file changed, 71 deletions(-) delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 786b7ee..0000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,71 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '36 12 * * 3' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ 'java' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] - # Learn more: - # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 From fcc1c472e13e23c325a24b4282b2289dbe592944 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 21:51:00 +0200 Subject: [PATCH 02/19] build: fix maven schema --- .mvnsettings.xml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.mvnsettings.xml b/.mvnsettings.xml index 4fbd3d1..d29a2ce 100644 --- a/.mvnsettings.xml +++ b/.mvnsettings.xml @@ -1,7 +1,4 @@ - + From 54c55b1a8285a420663620329a8bb7e79e6d3e3f Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:08:45 +0200 Subject: [PATCH 03/19] breaking: no longer static to lighten code --- .../java/com/github/jsixface/YamlConfig.java | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index fa09998..019c03d 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -27,55 +27,52 @@ import java.util.regex.Pattern; public class YamlConfig { + private final YamlConfig instance; + private final Yaml yaml; + private final Pattern arrayKeyPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]+)\\[([0-9]+)]$"); private Object content; - private Pattern arrayKeyPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]+)\\[([0-9]+)]$"); - - private YamlConfig() { - } /** * Create configuration from Reader + * * @param reader the reader to read config from - * @return YamlConfig instance */ - public static YamlConfig load(Reader reader) { - YamlConfig instance = new YamlConfig(); - Yaml yml = new Yaml(); - instance.content = yml.load(reader); - return instance; + public YamlConfig(Reader reader) { + this.yaml = new Yaml(); + this.content = yaml.load(reader); } + /** * Create configuration from input stream - * @param in the Input stream to read from - * @return YamlConfig instance + * + * @param inputStream the Input stream to read from */ - public static YamlConfig load(InputStream in) { - YamlConfig instance = new YamlConfig(); - Yaml yml = new Yaml(); - instance.content = yml.load(in); - return instance; + public YamlConfig(InputStream inputStream) { + this.yaml = new Yaml(); + this.content = yaml.load(inputStream); } /** * Create configuration from input stream, using your yaml instance - * @param yaml the Yaml instance to use - * @param in the Input stream to read from - * @return YamlConfig instance + * + * @param yaml the Yaml instance to use + * @param inputStream the Input stream to read from */ - public static YamlConfig load(Yaml yaml, InputStream in) { - YamlConfig instance = new YamlConfig(); - instance.content = yaml.load(in); - return instance; + public YamlConfig(Yaml yaml, InputStream inputStream) { + this.content = yaml.load(inputStream); } /** * Gets the String value for the specified key from the config. * * @param key Key in dotted notation like first.second[2].third - * @return The String value of property.
null if the key is not present - * or not a leaf node. Boolean or Integer or other format - * are converted to String. + * @return The String value of property. + *

+ * null if the key is not present + * or not a leaf node. + *

+ * Boolean or Integer or another format is converted to String. */ public String getString(String key) { Object foundNode = getNode(key, content); @@ -89,8 +86,9 @@ public String getString(String key) { * Gets the Integer value for the specified key from the config. * * @param key Key in dotted notation like first.second[2].third - * @return The Integer value of property.
null if the key is not present - * or not a leaf node. + * @return The Integer value of property. + *

+ * null if the key is not present or not a leaf node. */ public Integer getInt(String key) { Object foundNode = getNode(key, content); @@ -101,7 +99,7 @@ public Integer getInt(String key) { } private Object getNode(String key, Object foundNode) { - String[] parts = decompose(key); + String[] parts = splitByDot(key); for (String part : parts) { int arrayNum = -1; Matcher matcher = arrayKeyPattern.matcher(part); @@ -114,7 +112,7 @@ private Object getNode(String key, Object foundNode) { foundNode = ((Map) foundNode).get(part); if (arrayNum >= 0) { if (foundNode instanceof ArrayList - && ((ArrayList) foundNode).size() > arrayNum) { + && ((ArrayList) foundNode).size() > arrayNum) { foundNode = ((ArrayList) foundNode).get(arrayNum); } else return null; @@ -126,7 +124,13 @@ private Object getNode(String key, Object foundNode) { return foundNode; } - private String[] decompose(String key) { + /** + * Splits a key by the dot character. + * + * @param key the key to split + * @return the split key path. + */ + private String[] splitByDot(String key) { return key.split("\\."); } } From a7e79ec9066b111c1de419a5e11c0bb60eaf3797 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:10:05 +0200 Subject: [PATCH 04/19] fix: var init --- src/main/java/com/github/jsixface/YamlConfig.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index 019c03d..f9c6eb6 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -27,10 +27,8 @@ import java.util.regex.Pattern; public class YamlConfig { - private final YamlConfig instance; - private final Yaml yaml; private final Pattern arrayKeyPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]+)\\[([0-9]+)]$"); - private Object content; + private final Object content; /** * Create configuration from Reader @@ -38,7 +36,7 @@ public class YamlConfig { * @param reader the reader to read config from */ public YamlConfig(Reader reader) { - this.yaml = new Yaml(); + final Yaml yaml = new Yaml(); this.content = yaml.load(reader); } @@ -49,7 +47,7 @@ public YamlConfig(Reader reader) { * @param inputStream the Input stream to read from */ public YamlConfig(InputStream inputStream) { - this.yaml = new Yaml(); + final Yaml yaml = new Yaml(); this.content = yaml.load(inputStream); } From 50397b5ca8d4431e398daccc1a162075712e5f39 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:10:16 +0200 Subject: [PATCH 05/19] refactor: tests --- .../com/github/jsixface/YamlConfigTest.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/github/jsixface/YamlConfigTest.java b/src/test/java/com/github/jsixface/YamlConfigTest.java index 04a5329..af90e79 100644 --- a/src/test/java/com/github/jsixface/YamlConfigTest.java +++ b/src/test/java/com/github/jsixface/YamlConfigTest.java @@ -23,55 +23,58 @@ import static org.junit.Assert.*; public class YamlConfigTest { - private InputStream resource = getClass().getClassLoader().getResourceAsStream("test.yml"); + private final InputStream resource = getClass().getClassLoader().getResourceAsStream("test.yml"); @Test - public void load() { + public void getStringList() { + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("services.names"); + assertNotNull(value); + assertEquals("Andrew", value); } @Test public void getStringArray() { - YamlConfig config = YamlConfig.load(resource); - String value = config.getString("services.names[1].first"); + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("services.names[1].first"); assertNotNull(value); assertEquals("Andrew", value); } @Test public void getStringOutOfIndex() { - YamlConfig config = YamlConfig.load(resource); - String value = config.getString("services.names[3].first"); + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("services.names[3].first"); assertNull(value); } @Test public void getStringInvalidKey() { - YamlConfig config = YamlConfig.load(resource); - String value = config.getString("services.test.first"); + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("services.test.first"); assertNull(value); } @Test public void getStringNumber() { - YamlConfig config = YamlConfig.load(resource); - String value = config.getString("version"); + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("version"); assertNotNull(value); assertEquals("3", value); } @Test public void getString() { - YamlConfig config = YamlConfig.load(resource); - String value = config.getString("services.db.image"); + final YamlConfig config = YamlConfig.load(resource); + final String value = config.getString("services.db.image"); assertNotNull(value); assertEquals("mysql", value); } @Test public void getInt() { - YamlConfig config = YamlConfig.load(resource); - Integer value = config.getInt("version"); - assertNotNull(value); - assertEquals(Integer.valueOf(3), value); + final YamlConfig config = YamlConfig.load(resource); + final int value = config.getInt("version"); + assertEquals(3, value); } } \ No newline at end of file From cb3c2fd60e1b29b1f2c94efddc30369d1a4eb5a4 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:15:37 +0200 Subject: [PATCH 06/19] refactor: documentation --- .../java/com/github/jsixface/YamlConfig.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index f9c6eb6..26b2634 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -73,7 +73,7 @@ public YamlConfig(Yaml yaml, InputStream inputStream) { * Boolean or Integer or another format is converted to String. */ public String getString(String key) { - Object foundNode = getNode(key, content); + Object foundNode = getNode(key); if (foundNode != null && !(foundNode instanceof Collection)) { return foundNode.toString(); } @@ -89,15 +89,23 @@ public String getString(String key) { * null if the key is not present or not a leaf node. */ public Integer getInt(String key) { - Object foundNode = getNode(key, content); + Object foundNode = getNode(key); if (foundNode instanceof Integer) { return (Integer) foundNode; } return null; } - private Object getNode(String key, Object foundNode) { - String[] parts = splitByDot(key); + /** + * Goes through the file node by node until the desired key is found. + * + * @param key the key to find + * @return the found node or null if not found + */ + private Object getNode(String key) { + final String[] parts = splitByDot(key); + + Object foundNode = content; for (String part : parts) { int arrayNum = -1; Matcher matcher = arrayKeyPattern.matcher(part); @@ -106,12 +114,12 @@ private Object getNode(String key, Object foundNode) { arrayNum = Integer.parseInt(matcher.group(2)); } if (foundNode instanceof Map) { - if (((Map) foundNode).containsKey(part)) { - foundNode = ((Map) foundNode).get(part); + if (((Map) foundNode).containsKey(part)) { + foundNode = ((Map) foundNode).get(part); if (arrayNum >= 0) { if (foundNode instanceof ArrayList - && ((ArrayList) foundNode).size() > arrayNum) { - foundNode = ((ArrayList) foundNode).get(arrayNum); + && ((ArrayList) foundNode).size() > arrayNum) { + foundNode = ((ArrayList) foundNode).get(arrayNum); } else return null; } From 672dbd10b3c200ab343e6efdcdb25decbc846701 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:21:14 +0200 Subject: [PATCH 07/19] style: space --- src/main/java/com/github/jsixface/YamlConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index 26b2634..11c8ffe 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -40,7 +40,6 @@ public YamlConfig(Reader reader) { this.content = yaml.load(reader); } - /** * Create configuration from input stream * From 45a1acf94d078774b5b1b1c825c49b7577102d99 Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 22 Jul 2023 22:21:18 +0200 Subject: [PATCH 08/19] fix: tests --- .../java/com/github/jsixface/YamlConfigTest.java | 15 ++++++++------- .../jsixface/YamlConfigWithEnvOverridesTest.java | 6 ++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/github/jsixface/YamlConfigTest.java b/src/test/java/com/github/jsixface/YamlConfigTest.java index af90e79..96b3337 100644 --- a/src/test/java/com/github/jsixface/YamlConfigTest.java +++ b/src/test/java/com/github/jsixface/YamlConfigTest.java @@ -16,6 +16,7 @@ package com.github.jsixface; +import org.junit.Before; import org.junit.Test; import java.io.InputStream; @@ -24,10 +25,16 @@ public class YamlConfigTest { private final InputStream resource = getClass().getClassLoader().getResourceAsStream("test.yml"); + private YamlConfig config; + + @Before + public void loadResource() { + config = new YamlConfig(resource); + assertNotNull(resource); + } @Test public void getStringList() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("services.names"); assertNotNull(value); assertEquals("Andrew", value); @@ -35,7 +42,6 @@ public void getStringList() { @Test public void getStringArray() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("services.names[1].first"); assertNotNull(value); assertEquals("Andrew", value); @@ -43,21 +49,18 @@ public void getStringArray() { @Test public void getStringOutOfIndex() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("services.names[3].first"); assertNull(value); } @Test public void getStringInvalidKey() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("services.test.first"); assertNull(value); } @Test public void getStringNumber() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("version"); assertNotNull(value); assertEquals("3", value); @@ -65,7 +68,6 @@ public void getStringNumber() { @Test public void getString() { - final YamlConfig config = YamlConfig.load(resource); final String value = config.getString("services.db.image"); assertNotNull(value); assertEquals("mysql", value); @@ -73,7 +75,6 @@ public void getString() { @Test public void getInt() { - final YamlConfig config = YamlConfig.load(resource); final int value = config.getInt("version"); assertEquals(3, value); } diff --git a/src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java b/src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java index 277ff46..ffcd574 100644 --- a/src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java +++ b/src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java @@ -37,17 +37,15 @@ * key: ${ENV_KEY:-someDefault} */ public class YamlConfigWithEnvOverridesTest { - public static final String ENV_OVERRIDE_VALUE = "my db image env value"; - private final InputStream resource = getClass().getClassLoader().getResourceAsStream("testWithEnvOverrides.yml"); + private final InputStream resource = getClass().getClassLoader().getResourceAsStream("testWithEnvOverrides.yml"); private YamlConfig config; @Before public void setUp() { Yaml yaml = givenYamlInstanceWithEnvScalar(); - - config = YamlConfig.load(yaml, resource); + config = new YamlConfig(yaml, resource); } private Yaml givenYamlInstanceWithEnvScalar() { From 58a0193e492e3e60fef993e98db5ba7ee115305a Mon Sep 17 00:00:00 2001 From: ineanto Date: Tue, 25 Jul 2023 16:34:02 +0200 Subject: [PATCH 09/19] feat: get list --- .../java/com/github/jsixface/YamlConfig.java | 70 +++++++++++++------ .../com/github/jsixface/YamlConfigTest.java | 8 ++- src/test/resources/test.yml | 5 +- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index 11c8ffe..0928a3e 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -20,14 +20,14 @@ import java.io.InputStream; import java.io.Reader; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; +import java.lang.reflect.Array; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class YamlConfig { private final Pattern arrayKeyPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]+)\\[([0-9]+)]$"); + private final Pattern keyPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]+)\\[([0-9]+)]$"); private final Object content; /** @@ -88,15 +88,38 @@ public String getString(String key) { * null if the key is not present or not a leaf node. */ public Integer getInt(String key) { - Object foundNode = getNode(key); - if (foundNode instanceof Integer) { - return (Integer) foundNode; + Object node = getNode(key); + if (node instanceof Integer) { + return (Integer) node; + } + return null; + } + + /** + * Gets a type list value for the specified key from the config. + * + * @param key Key in dotted notation like first.second + * @return The type list value of property. + *

+ * null if the key is not present or not a leaf node. + */ + public ArrayList getList(String key, Class type) { + final ArrayList node = (ArrayList) getNode(key); + final ArrayList typeList = new ArrayList<>(); + if (node != null) { + try { + node.forEach(o -> typeList.add(type.cast(o))); + return typeList; + } catch (ClassCastException exception) { + return null; + } } return null; } /** - * Goes through the file node by node until the desired key is found. + * Gets a node at the specific index. + * The key follows this pattern: my.key[index].entry * * @param key the key to find * @return the found node or null if not found @@ -104,31 +127,34 @@ public Integer getInt(String key) { private Object getNode(String key) { final String[] parts = splitByDot(key); - Object foundNode = content; + Object node = content; for (String part : parts) { int arrayNum = -1; - Matcher matcher = arrayKeyPattern.matcher(part); - if (matcher.matches()) { - part = matcher.group(1); - arrayNum = Integer.parseInt(matcher.group(2)); + final Matcher arrayKeyPatternMatcher = arrayKeyPattern.matcher(part); + final Matcher keyPatternMatcher = keyPattern.matcher(part); + if (arrayKeyPatternMatcher.matches()) { + part = arrayKeyPatternMatcher.group(1); + arrayNum = Integer.parseInt(arrayKeyPatternMatcher.group(2)); + } else if (keyPatternMatcher.matches()) { + part = keyPatternMatcher.group(1); } - if (foundNode instanceof Map) { - if (((Map) foundNode).containsKey(part)) { - foundNode = ((Map) foundNode).get(part); + if (node instanceof Map) { + if (((Map) node).containsKey(part)) { + node = ((Map) node).get(part); if (arrayNum >= 0) { - if (foundNode instanceof ArrayList - && ((ArrayList) foundNode).size() > arrayNum) { - foundNode = ((ArrayList) foundNode).get(arrayNum); - } else - return null; + if (node instanceof ArrayList && ((ArrayList) node).size() > arrayNum) { + node = ((ArrayList) node).get(arrayNum); + } } - } else + } else { return null; + } } } - return foundNode; + return node; } + /** * Splits a key by the dot character. * diff --git a/src/test/java/com/github/jsixface/YamlConfigTest.java b/src/test/java/com/github/jsixface/YamlConfigTest.java index 96b3337..6f40bc4 100644 --- a/src/test/java/com/github/jsixface/YamlConfigTest.java +++ b/src/test/java/com/github/jsixface/YamlConfigTest.java @@ -20,6 +20,9 @@ import org.junit.Test; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import static org.junit.Assert.*; @@ -35,9 +38,8 @@ public void loadResource() { @Test public void getStringList() { - final String value = config.getString("services.names"); - assertNotNull(value); - assertEquals("Andrew", value); + final ArrayList value = config.getList("services.list", String.class); + assertFalse(value.isEmpty()); } @Test diff --git a/src/test/resources/test.yml b/src/test/resources/test.yml index c8d6a7a..0d9fb45 100644 --- a/src/test/resources/test.yml +++ b/src/test/resources/test.yml @@ -19,4 +19,7 @@ services: - first: James last: Justinson - first: Andrew - last: Armstrong \ No newline at end of file + last: Armstrong + list: + - "First" + - "Last" \ No newline at end of file From d2efe1c6a6e847d8081612d7a0de79ecd99ba7d7 Mon Sep 17 00:00:00 2001 From: ineanto Date: Tue, 25 Jul 2023 16:42:10 +0200 Subject: [PATCH 10/19] feat: getStringList --- .../java/com/github/jsixface/YamlConfig.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index 0928a3e..7ed64b2 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -63,11 +63,10 @@ public YamlConfig(Yaml yaml, InputStream inputStream) { /** * Gets the String value for the specified key from the config. * - * @param key Key in dotted notation like first.second[2].third - * @return The String value of property. + * @param key key in dotted notation like first.second[2].third + * @return the String value of property. *

- * null if the key is not present - * or not a leaf node. + * null if the key is not present or not a leaf node. *

* Boolean or Integer or another format is converted to String. */ @@ -82,8 +81,8 @@ public String getString(String key) { /** * Gets the Integer value for the specified key from the config. * - * @param key Key in dotted notation like first.second[2].third - * @return The Integer value of property. + * @param key key in dotted notation like first.second[2].third + * @return the Integer value of property. *

* null if the key is not present or not a leaf node. */ @@ -96,10 +95,22 @@ public Integer getInt(String key) { } /** - * Gets a type list value for the specified key from the config. + * Gets a string list for the specified key from the config. + * + * @param key key in dotted notation like first.second + * @return the type list value of property. + *

+ * null if the key is not present or not a leaf node. + */ + public ArrayList getStringList(String key) { + return getList(key, String.class); + } + + /** + * Gets a generic list for the specified key from the config. * - * @param key Key in dotted notation like first.second - * @return The type list value of property. + * @param key key in dotted notation like first.second + * @return the generic list. *

* null if the key is not present or not a leaf node. */ @@ -118,10 +129,10 @@ public ArrayList getList(String key, Class type) { } /** - * Gets a node at the specific index. + * Gets a node by the key. * The key follows this pattern: my.key[index].entry * - * @param key the key to find + * @param key The key to find * @return the found node or null if not found */ private Object getNode(String key) { @@ -158,7 +169,7 @@ private Object getNode(String key) { /** * Splits a key by the dot character. * - * @param key the key to split + * @param key The key to split * @return the split key path. */ private String[] splitByDot(String key) { From 0e7914150a68cbdcfe01a0995e545c904364cb1c Mon Sep 17 00:00:00 2001 From: ineanto Date: Tue, 25 Jul 2023 16:42:18 +0200 Subject: [PATCH 11/19] refactor: update readme --- README.md | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 668dd14..f47336e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,20 @@ # YamlConfig + [![Build Status](https://travis-ci.com/jsixface/YamlConfig.svg?branch=master)](https://travis-ci.com/jsixface/YamlConfig) -[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. +[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 + +- 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. @@ -24,6 +28,7 @@ If you use Maven for Dependency management, you can include this using below dep ``` ## Usage - internal Yaml + Get an instance of the YamlConfig by passing in a reader or an inputstream. ``` @@ -31,7 +36,7 @@ InputStream resource = getClass() .getClassLoader() .getResourceAsStream("config.yml"); -YamlConfig config = YamlConfig.load(resource); +YamlConfig config = new YamlConfig(resource); ``` Assume the contents of `config.yml` is as below: @@ -46,6 +51,9 @@ services: port: 2976 - host: example.com port: 2978 + mylist: + - "The first line" + - "The second line" ``` You can access the value of db image by: @@ -53,14 +61,23 @@ 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"); ``` +If you want to get a list of strings, you can do the following: + +``` +ArrayList value = config.getList("services.list", String.class); +``` + ## Usage - externally supplied Yaml -As above, but with you supplying the Yaml instance, allowing Yaml to template environment variables to override yaml supplied values. + +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. @@ -73,16 +90,20 @@ InputStream resource = getClass() .getResourceAsStream("config.yml"); # pass the lot to YamlConfig -YamlConfig config = YamlConfig.load(yaml, resource); +YamlConfig config = new YamlConfig(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) + +see the +test [YamlConfigWithEnvOverridesTest.java](src/test/java/com/github/jsixface/YamlConfigWithEnvOverridesTest.java) and its config file: [testWithEnvOverrides.yml](src/test/resources/testWithEnvOverrides.yml) \ No newline at end of file From 48282a3c75a26220c7840d2809b2cdd4c233bfcb Mon Sep 17 00:00:00 2001 From: ineanto Date: Tue, 25 Jul 2023 16:46:54 +0200 Subject: [PATCH 12/19] refactor: readme --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f47336e..2d2250c 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,28 @@ [![Build Status](https://travis-ci.com/jsixface/YamlConfig.svg?branch=master)](https://travis-ci.com/jsixface/YamlConfig) -[Yaml](https://en.wikipedia.org/wiki/YAML) is a data serialization format similar to JSON but more human readable. +[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. +## ⚠ DISCLAIMER ⚠ + +This forks introduces breaking changes with upstream! + +``` +YamlConfig config = YamlConfig.load(resource); +YamlConfig config = YamlConfig.load(yaml, resource); +``` + +is now: + +``` +YamlConfig config = new YamlConfig(resource); +YamlConfig config = new YamlConfig(yaml, resource); +``` + ## Features - Uses SnakeYAML for reading YAML, so it can handle any data recognizable by SnakeYAML. @@ -74,6 +90,10 @@ If you want to get a list of strings, you can do the following: ArrayList value = config.getList("services.list", String.class); ``` +``` +ArrayList value = config.getStringList("services.list"); +``` + ## Usage - externally supplied Yaml As above, but with you supplying the Yaml instance, allowing Yaml to template environment variables to override yaml From ced19f83b83f35d1a30afaa30ac3838ff824a68a Mon Sep 17 00:00:00 2001 From: ineanto Date: Wed, 26 Jul 2023 04:14:39 +0200 Subject: [PATCH 13/19] fix: readme grammar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d2250c..4a5efc1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ manner. ## ⚠ DISCLAIMER ⚠ -This forks introduces breaking changes with upstream! +This fork introduces breaking changes with upstream! ``` YamlConfig config = YamlConfig.load(resource); From 6e60639f056465bf7fe3d816ebdebe532362d199 Mon Sep 17 00:00:00 2001 From: ineanto Date: Wed, 26 Jul 2023 04:16:50 +0200 Subject: [PATCH 14/19] feat(build): bump version and deps --- pom.xml | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 88ae803..9a82213 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.github.jsixface yamlconfig - 1.1.2 + 1.2 1.8 @@ -14,12 +14,14 @@ yamlconfig - Manage configuration for java project in a YAML file and access them via dotted notation. + + Manage configuration for a java project in a YAML file and access them via dotted notation. + https://github.com/jsixface/YamlConfig Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt + https://www.apache.org/licenses/LICENSE-2.0.txt repo @@ -57,12 +59,12 @@ org.yaml snakeyaml - 1.32 + 2.0 junit junit - 4.13.1 + 4.13.2 test @@ -73,7 +75,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M5 + 3.0.0-M7 my db image env value @@ -98,7 +100,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.8 + 1.6.13 true ossrh @@ -110,7 +112,7 @@ org.apache.maven.plugins maven-source-plugin - 3.0.1 + 3.2.1 8 @@ -126,7 +128,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.4 + 3.5.0 8 From 317600940e37bdfcbd8c0262df81edd1629eb382 Mon Sep 17 00:00:00 2001 From: ineanto Date: Wed, 26 Jul 2023 04:18:29 +0200 Subject: [PATCH 15/19] feat: update readme disclaimer --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4a5efc1..e0c8555 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ manner. **YamlConfig** helps read configuration for a java project from a YAML config file and access them via dotted notation. -## ⚠ DISCLAIMER ⚠ +## ⚠ Warning: -This fork introduces breaking changes with upstream! +Version **1.2** introduces breaking changes as follows: ``` YamlConfig config = YamlConfig.load(resource); @@ -26,7 +26,7 @@ YamlConfig config = new YamlConfig(yaml, resource); ## Features -- Uses SnakeYAML for reading YAML, so it can handle any data recognizable by SnakeYAML. +- Use SnakeYAML for reading YAML, so it can handle any data recognizable by SnakeYAML. - Ease of access using dotted notation to read properties ## Getting Started @@ -39,7 +39,7 @@ If you use Maven for Dependency management, you can include this using below dep com.github.jsixface yamlconfig - 1.1 + 1.2 ``` From dd2e96837a458b999fc97ea094d2c2a9fe163f8f Mon Sep 17 00:00:00 2001 From: ineanto Date: Mon, 15 Apr 2024 14:19:10 +0200 Subject: [PATCH 16/19] fix: remove unused import --- src/main/java/com/github/jsixface/YamlConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/jsixface/YamlConfig.java b/src/main/java/com/github/jsixface/YamlConfig.java index 7ed64b2..87d3fab 100644 --- a/src/main/java/com/github/jsixface/YamlConfig.java +++ b/src/main/java/com/github/jsixface/YamlConfig.java @@ -20,7 +20,6 @@ import java.io.InputStream; import java.io.Reader; -import java.lang.reflect.Array; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; From 4529d4c4b2da732b9bcf97352855442081ca506a Mon Sep 17 00:00:00 2001 From: ineanto Date: Mon, 15 Apr 2024 14:23:51 +0200 Subject: [PATCH 17/19] feat: bump version inside README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ce46d41..faf607c 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ If you use Maven for Dependency management, you can include this using below dep com.github.jsixface yamlconfig - 1.2.0 + 1.3.0 ``` Or if you use Gradle, you can include this using below dependency. ``` -implementation 'com.github.jsixface:yamlconfig:1.2.0' +implementation 'com.github.jsixface:yamlconfig:1.3.0' ``` ## Usage - internal Yaml From 9ae3e15128331728702bf085307831737545b67a Mon Sep 17 00:00:00 2001 From: ineanto Date: Mon, 15 Apr 2024 14:30:45 +0200 Subject: [PATCH 18/19] fix: update examples --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index faf607c..deda281 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ InputStream resource = getClass() .getClassLoader() .getResourceAsStream("config.yml"); -YamlConfig config = YamlConfig.load(resource); +YamlConfig config = new YamlConfig(resource); ``` Assume the contents of `config.yml` is as below: @@ -78,7 +78,7 @@ InputStream resource = getClass() .getResourceAsStream("config.yml"); # pass the lot to YamlConfig -YamlConfig config = YamlConfig.load(yaml, resource); +YamlConfig config = new YamlConfig(yaml, resource); # and now you can use fully qualified dot notation keys: config.getString("service.db.someKey"); From 902a3edda249067aad107710326a89b5ede31f77 Mon Sep 17 00:00:00 2001 From: ineanto Date: Mon, 15 Apr 2024 14:34:12 +0200 Subject: [PATCH 19/19] feat: add back the notice --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index deda281..33ba121 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,23 @@ It looks better to organize config in a YAML file since it makes sense to mainta **YamlConfig** helps read configuration for a java project from a YAML config file and access them via dotted notation. +## ⚠ 1.3.0 Breaking changes: + + +Version **1.3.0** introduces breaking changes as follows: + +``` +YamlConfig config = YamlConfig.load(resource); +YamlConfig config = YamlConfig.load(yaml, resource); +``` + +is now: + +``` +YamlConfig config = new YamlConfig(resource); +YamlConfig config = new YamlConfig(yaml, resource); +``` + ## Features - Uses SnakeYAML for reading YAML, so it can handle any data recognizable by SnakeYAML. - Ease of access using dotted notation to read properties