diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-23 11:13:24 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-23 11:13:24 +0000 |
| commit | d22dab1ccfe55ee2f3449bb9065481803f8dc0ff (patch) | |
| tree | 700450a458c1e7bef19f9865ada88bd77d100948 /src | |
| parent | 39ab36c7a288e8ccf1203f8bb828f97e6aa025ea (diff) | |
Check type of example and profile elements
Diffstat (limited to 'src')
3 files changed, 47 insertions, 17 deletions
diff --git a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java index 8f5f63e2..12a6b3f6 100644 --- a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java @@ -5,7 +5,6 @@ import org.shedlang.compiler.config.toml.TomlPath; import org.shedlang.compiler.config.toml.TomlTableWithSource; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; -import org.shedlang.compiler.util.P; import org.tomlj.Toml; import org.tomlj.TomlParseResult; @@ -13,7 +12,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.stream.IntStream; public record ProjectConfig( Path root, @@ -68,25 +66,21 @@ public record ProjectConfig( public static List<ExampleConfig> examplesFromToml(TomlTableWithSource table) { return table .getTable("shed") - .flatMap(shed -> shed.getArray("examples")) - .map(examples -> - IntStream.range(0, examples.size()) - .mapToObj(exampleIndex -> ExampleConfig.fromToml(examples.getTable(exampleIndex))) - .toList() - ) - .orElseGet(() -> List.of()); + .flatMap(shed -> shed.getArrayOfTables("examples")) + .orElseGet(() -> List.of()) + .stream() + .map(example -> ExampleConfig.fromToml(example.table())) + .toList(); } public static List<ProfileConfig> profilesFromToml(TomlTableWithSource table) { return table .getTable("shed") - .flatMap(shed -> shed.getArray("profiles")) - .map(profiles -> - IntStream.range(0, profiles.size()) - .mapToObj(profileIndex -> ProfileConfig.fromToml(profiles.getTable(profileIndex))) - .toList() - ) - .orElseGet(() -> List.of()); + .flatMap(shed -> shed.getArrayOfTables("profiles")) + .orElseGet(() -> List.of()) + .stream() + .map(profile -> ProfileConfig.fromToml(profile.table())) + .toList(); } public Path targetDir() { 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 5b573628..f4c96647 100644 --- a/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java @@ -47,7 +47,7 @@ public record TomlTableWithSource( ); } - public Optional<TomlArray> getArray(String key) { + private Optional<TomlArray> getArray(String key) { var value = table.get(key); var position = table.inputPositionOf(key); diff --git a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java index b08fa78a..10397acf 100644 --- a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java +++ b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java @@ -178,6 +178,24 @@ public class ProjectConfigTests { } @Test + public void whenProjectConfigHasExampleThatIsntTableThenErrorIsThrown() { + var error = assertThrows( + ConfigError.class, + () -> ProjectConfig.fromString(PROJECT_PATH, """ + [shed] + examples = [1]""" + ) + ); + + assertThat(error.getMessage(), equalTo("Expected shed.examples[0] to be a table, but was Long")); + assertThat(error.source().describe(), equalTo(""" + <string>:2:13 + examples = [1] + ^""" + )); + } + + @Test public void canParseProjectConfigWithNoProfiles() { var config = ProjectConfig.fromString(PROJECT_PATH, ""); @@ -223,4 +241,22 @@ public class ProjectConfigTests { ^""" )); } + + @Test + public void whenProjectConfigHasProfileThatIsntTableThenErrorIsThrown() { + var error = assertThrows( + ConfigError.class, + () -> ProjectConfig.fromString(PROJECT_PATH, """ + [shed] + profiles = [1]""" + ) + ); + + assertThat(error.getMessage(), equalTo("Expected shed.profiles[0] to be a table, but was Long")); + assertThat(error.source().describe(), equalTo(""" + <string>:2:13 + profiles = [1] + ^""" + )); + } } |
