From 31fc64bc31b43c5dcddbe263a38645d146fc4e5e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 23 Dec 2024 11:57:36 +0000 Subject: Add TomlTableWithSource.getLong() --- .../compiler/config/toml/TomlTableWithSource.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java b/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java index 4dab6e35..725d510a 100644 --- a/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java @@ -148,4 +148,27 @@ public record TomlTableWithSource( TomlSource.sourceAtTomlPosition(fullSource, valuePosition) ); } + + public long getLong(String key) { + var value = table.get(key); + var valuePosition = table.inputPositionOf(key); + + if (value == null) { + throw new ConfigError( + MessageFormat.format("{0} is missing {1}", path, key), + TomlSource.sourceAtTomlPosition(fullSource, position) + ); + } + + if (value instanceof Long valueLong) { + return valueLong; + } + + throw new UnexpectedTypeConfigError( + path.plusKey(key), + "a long", + value.getClass(), + TomlSource.sourceAtTomlPosition(fullSource, valuePosition) + ); + } } -- cgit v1.2.3