blob: 8c85a08996000fd027cb4b2dbb46b7f4e21de642 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
/* (C) 2025 Linnea Gräf - Licensed to everyone under the BSD 3 Clause License */
package moe.nea.prickly.config;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public record ConfigPath(String path) {
public ConfigPath join(String extraPath) {
return new ConfigPath(path + "_" + extraPath);
}
public Optional<String> getString() {
return Optional.ofNullable(ConfigCompat.getEnv(path));
}
public Stream<ConfigPath> findChildren() {
return ConfigCompat.getAllDirectChildren(path).stream().map(this::join);
}
public Optional<Integer> getInt() {
var str = getString();
if (str.isPresent())
try {
return Optional.of(Integer.parseInt(str.get()));
} catch (NumberFormatException ex) {
log.warn("could not parse config value at {}", path, ex);
}
return Optional.empty();
}
public Optional<Boolean> getBoolean() {
var str = getString();
if (str.isPresent()) {
var sstr = str.get();
if (sstr.equalsIgnoreCase("true") || sstr.equals("1")) return OPT_TRUE;
else if (sstr.equalsIgnoreCase("false") || sstr.equals("0")) return OPT_FALSE;
else log.warn("could not parse boolean value at {}", path);
}
return Optional.empty();
}
private static final Optional<Boolean> OPT_TRUE = Optional.of(true), OPT_FALSE = Optional.of(false);
public Supplier<RuntimeException> requireMessage() {
return () -> new RuntimeException("missing required value at path " + path());
}
public String requireString() {
return getString().orElseThrow(requireMessage());
}
public int requireInt() {
return getInt().orElseThrow(requireMessage());
}
public String lastPart() {
int index = path.lastIndexOf('_');
return path.substring(index + 1);
}
}
|