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 ++++++ .../config/toml/TomlTableWithSourceTests.java | 81 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) (limited to 'src') 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) + ); + } } diff --git a/src/test/java/org/shedlang/compiler/config/toml/TomlTableWithSourceTests.java b/src/test/java/org/shedlang/compiler/config/toml/TomlTableWithSourceTests.java index 3dcc17f0..0165f8df 100644 --- a/src/test/java/org/shedlang/compiler/config/toml/TomlTableWithSourceTests.java +++ b/src/test/java/org/shedlang/compiler/config/toml/TomlTableWithSourceTests.java @@ -11,6 +11,87 @@ import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; public class TomlTableWithSourceTests { + @Test + public void whenKeyIsMissingThenGetLongThrowsError() { + var source = Sources.string(""" + [[a.b]] + x = 1 + + [[a.b]] + """); + var table = TomlTableWithSource.parse(source); + + var error = assertThrows( + ConfigError.class, + () -> table + .getTable("a") + .orElseThrow() + .getArrayOfTables("b") + .orElseThrow() + .get(1) + .getLong("x") + ); + + assertThat(error.getMessage(), equalTo("a.b[1] is missing x")); + assertThat(error.source().describe(), equalTo(""" + :4:1 + [[a.b]] + ^""" + )); + } + + @Test + public void whenKeyIsNotLongThenGetLongThrowsError() { + var source = Sources.string(""" + [[a.b]] + x = 1 + + [[a.b]] + x = true + """); + var table = TomlTableWithSource.parse(source); + + var error = assertThrows( + ConfigError.class, + () -> table + .getTable("a") + .orElseThrow() + .getArrayOfTables("b") + .orElseThrow() + .get(1) + .getLong("x") + ); + + assertThat(error.getMessage(), equalTo("Expected a.b[1].x to be a long, but was Boolean")); + assertThat(error.source().describe(), equalTo(""" + :5:1 + x = true + ^""" + )); + } + + @Test + public void whenKeyIsLongThenGetLongReturnsValue() { + var source = Sources.string(""" + [[a.b]] + x = 1 + + [[a.b]] + x = 2 + """); + var table = TomlTableWithSource.parse(source); + + var result = table + .getTable("a") + .orElseThrow() + .getArrayOfTables("b") + .orElseThrow() + .get(1) + .getLong("x"); + + assertThat(result, equalTo(2L)); + } + @Test public void whenKeyIsMissingThenGetStringThrowsError() { var source = Sources.string(""" -- cgit v1.2.3