Skip to content

Commit a9c30f6

Browse files
authored
Merge pull request #115 from avaje/feature/simpleYamlLoader
Allow access to parsers from configuration
2 parents 9ee0d9e + ab28c54 commit a9c30f6

File tree

10 files changed

+93
-18
lines changed

10 files changed

+93
-18
lines changed

avaje-config/src/main/java/io/avaje/config/ConfigParser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.config;
22

33
import java.io.InputStream;
4+
import java.io.Reader;
45
import java.util.Map;
56

67
/**
@@ -14,9 +15,17 @@ public interface ConfigParser {
1415
String[] supportedExtensions();
1516

1617
/**
17-
* Loads a file into a flat map of key value pairs.
18+
* Parse content into key value pairs.
1819
*
19-
* @param is stream of file contents
20+
* @param reader configuration contents
21+
* @return Key-Value pairs of all the configs
22+
*/
23+
Map<String, String> load(Reader reader);
24+
25+
/**
26+
* Parse content into key value pairs.
27+
*
28+
* @param is configuration contents
2029
* @return Key-Value pairs of all the configs
2130
*/
2231
Map<String, String> load(InputStream is);

avaje-config/src/main/java/io/avaje/config/Configuration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,24 @@ default boolean enabled(String key, boolean enabledDefault) {
506506
*/
507507
void evalModify(Properties properties);
508508

509+
/**
510+
* Return the parser for the given extension.
511+
*
512+
* <pre>{@code
513+
*
514+
* // obtain a yaml parser
515+
* ConfigParser yamlParser = configuration.parser("yaml").orElseThrow();
516+
*
517+
* // parse some yaml content
518+
* Map<String,String> keyValues = yamlParser.load(inputStream)
519+
*
520+
* // put the keyValues into the configuration
521+
* configuration.putAll(keyValues);
522+
*
523+
* }</pre>
524+
*/
525+
Optional<ConfigParser> parser(String extension);
526+
509527
/**
510528
* Expression evaluation.
511529
*/

avaje-config/src/main/java/io/avaje/config/CoreConfiguration.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@NonNullApi
2727
final class CoreConfiguration implements Configuration {
2828

29+
private final Parsers parsers;
2930
private final ConfigurationLog log;
3031
private final ModifyAwareProperties properties;
3132
private final ReentrantLock lock = new ReentrantLock();
@@ -40,24 +41,31 @@ final class CoreConfiguration implements Configuration {
4041
private Timer timer;
4142
private final String pathPrefix;
4243

43-
CoreConfiguration(ModificationEventRunner eventRunner, ConfigurationLog log, CoreEntry.CoreMap entries) {
44-
this(eventRunner, log, entries, "");
45-
}
46-
47-
CoreConfiguration(ModificationEventRunner eventRunner, ConfigurationLog log, CoreEntry.CoreMap entries, String prefix) {
44+
CoreConfiguration(Parsers parsers, ModificationEventRunner eventRunner, ConfigurationLog log, CoreEntry.CoreMap entries) {
45+
this.parsers = parsers;
4846
this.eventRunner = eventRunner;
4947
this.log = log;
5048
this.properties = new ModifyAwareProperties(entries);
5149
this.listValue = new CoreListValue(this);
5250
this.setValue = new CoreSetValue(this);
51+
this.pathPrefix = "";
52+
}
53+
54+
CoreConfiguration(CoreConfiguration parent, CoreEntry.CoreMap entries, String prefix) {
55+
this.parsers = parent.parsers;
56+
this.eventRunner = parent.eventRunner;
57+
this.log = parent.log;
58+
this.properties = new ModifyAwareProperties(entries);
59+
this.listValue = new CoreListValue(this);
60+
this.setValue = new CoreSetValue(this);
5361
this.pathPrefix = prefix;
5462
}
5563

5664
/**
5765
* For testing purposes.
5866
*/
5967
CoreConfiguration(CoreEntry.CoreMap entries) {
60-
this(new ForegroundEventRunner(), new DefaultConfigurationLog(), entries, "");
68+
this(new Parsers(), new ForegroundEventRunner(), new DefaultConfigurationLog(), entries);
6169
}
6270

6371
/**
@@ -124,6 +132,11 @@ public void schedule(long delayMillis, long periodMillis, Runnable runnable) {
124132
}
125133
}
126134

135+
@Override
136+
public Optional<ConfigParser> parser(String extension) {
137+
return Optional.ofNullable(parsers.get(extension));
138+
}
139+
127140
String eval(String value) {
128141
return properties.eval(value);
129142
}
@@ -177,7 +190,7 @@ public Configuration forPath(String pathPrefix) {
177190
newEntryMap.put("", entry);
178191
}
179192
});
180-
return new CoreConfiguration(eventRunner, log, newEntryMap, dotPrefix);
193+
return new CoreConfiguration(this, newEntryMap, dotPrefix);
181194
}
182195

183196
@Override

avaje-config/src/main/java/io/avaje/config/CoreConfigurationBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ public Configuration.Builder includeResourceLoading() {
7575
public Configuration build() {
7676
final var runner = initRunner();
7777
final var log = initLog();
78+
final var parsers = new Parsers();
7879
if (includeResourceLoading) {
7980
log.preInitialisation();
80-
initialLoader = new InitialLoader(log, initResourceLoader());
81+
initialLoader = new InitialLoader(parsers, log, initResourceLoader());
8182
}
82-
return new CoreConfiguration(runner, log, initEntries()).postLoad(initialLoader);
83+
return new CoreConfiguration(parsers, runner, log, initEntries()).postLoad(initialLoader);
8384
}
8485

8586
private CoreEntry.CoreMap initEntries() {

avaje-config/src/main/java/io/avaje/config/FileWatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class FileWatch {
2020
this.log = configuration.log();
2121
this.configuration = configuration;
2222
this.delay = configuration.getLong("config.watch.delay", 60);
23-
this.period = configuration.getInt("config.watch.period", 10);
23+
this.period = configuration.getLong("config.watch.period", 10);
2424
this.parsers = parsers;
2525
this.files = initFiles(loadedFiles);
2626
if (files.isEmpty()) {

avaje-config/src/main/java/io/avaje/config/InitialLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ enum Source {
4141
private final Set<String> profileResourceLoaded = new HashSet<>();
4242
private final Parsers parsers;
4343

44-
InitialLoader(ConfigurationLog log, ResourceLoader resourceLoader) {
44+
InitialLoader(Parsers parsers, ConfigurationLog log, ResourceLoader resourceLoader) {
45+
this.parsers = parsers;
4546
this.log = log;
4647
this.loadContext = new InitialLoadContext(log, resourceLoader);
47-
this.parsers = new Parsers();
4848
}
4949

5050
Set<String> loadedFrom() {

avaje-config/src/main/java/io/avaje/config/YamlLoaderSimple.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
*/
99
final class YamlLoaderSimple implements YamlLoader {
1010

11+
@Override
12+
public Map<String, String> load(Reader reader) {
13+
return new Load().load(reader);
14+
}
15+
1116
@Override
1217
public Map<String, String> load(InputStream is) {
1318
return new Load().load(is);
@@ -39,7 +44,11 @@ enum State {
3944
private int multiLineIndent;
4045

4146
private Map<String, String> load(InputStream is) {
42-
try (LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(is))) {
47+
return load(new InputStreamReader(is));
48+
}
49+
50+
private Map<String, String> load(Reader reader) {
51+
try (LineNumberReader lineReader = new LineNumberReader(reader)) {
4352
String line;
4453
do {
4554
line = lineReader.readLine();

avaje-config/src/main/java/io/avaje/config/YamlLoaderSnake.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.yaml.snakeyaml.Yaml;
44

55
import java.io.InputStream;
6+
import java.io.Reader;
67
import java.util.LinkedHashMap;
78
import java.util.Map;
89

@@ -20,11 +21,20 @@ final class YamlLoaderSnake implements YamlLoader {
2021
this.yaml = new Yaml();
2122
}
2223

23-
@SuppressWarnings("unchecked")
24+
@Override
25+
public Map<String, String> load(Reader reader) {
26+
return load(yaml.loadAll(reader));
27+
}
28+
2429
@Override
2530
public Map<String, String> load(InputStream is) {
31+
return load(yaml.loadAll(is));
32+
}
33+
34+
@SuppressWarnings("unchecked")
35+
private Map<String, String> load(Iterable<Object> source) {
2636
Load load = new Load();
27-
for (Object map : yaml.loadAll(is)) {
37+
for (Object map : source) {
2838
load.loadMap((Map<String, Object>) map, null);
2939
}
3040
return load.map();

avaje-config/src/test/java/io/avaje/config/CoreConfigurationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.avaje.config.CoreEntry.CoreMap;
44
import org.junit.jupiter.api.Test;
55

6+
import java.io.StringReader;
67
import java.util.*;
78
import java.util.concurrent.atomic.AtomicBoolean;
89
import java.util.concurrent.atomic.AtomicInteger;
@@ -43,6 +44,20 @@ private CoreConfiguration createConfig(CoreEntry.CoreMap entries) {
4344
return new CoreConfiguration(entries);
4445
}
4546

47+
@Test
48+
void parser() {
49+
Configuration base = createSample();
50+
51+
ConfigParser yamlParser = base.parser("yaml").orElseThrow();
52+
53+
var ris = new StringReader("my.key: \n other: 42\n more.again: 52");
54+
Map<String, String> keyValues = yamlParser.load(ris);
55+
base.putAll(keyValues);
56+
57+
assertThat(base.get("my.key.other")).isEqualTo("42");
58+
assertThat(base.getLong("my.key.more.again")).isEqualTo(52);
59+
}
60+
4661
@Test
4762
void asProperties() {
4863
final var properties = basicProperties();

avaje-config/src/test/java/io/avaje/config/InitialLoaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class InitialLoaderTest {
1111

1212
private static InitialLoader newInitialLoader() {
13-
return new InitialLoader(new DefaultConfigurationLog(), new DefaultResourceLoader());
13+
return new InitialLoader(new Parsers(), new DefaultConfigurationLog(), new DefaultResourceLoader());
1414
}
1515

1616
@Test

0 commit comments

Comments
 (0)