aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/backends/ExamplesConfig.java
blob: f41682831ff533dd583eddf8d352f3e3b49e7b8a (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
package org.shedlang.compiler.backends;

import org.shedlang.compiler.config.json.Json5ObjectWithSource;
import org.zwobble.sourcetext.SourceText;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public record ExamplesConfig(
    List<ExampleConfig> examples
) {
    public static ExamplesConfig read(Path projectPath) throws IOException {
        var configPath = projectPath.resolve("shed-examples.json5");
        var contents = Files.readString(configPath);
        var sourceText = SourceText.fromString(configPath.toString(), contents);
        return fromSourceText(projectPath, sourceText);
    }

    public static ExamplesConfig fromSourceText(Path projectPath, SourceText sourceText) {
        return fromJson5(
            projectPath,
            Json5ObjectWithSource.parse(sourceText)
        );
    }

    public static ExamplesConfig fromJson5(Path root, Json5ObjectWithSource object) {
        return new ExamplesConfig(
            examplesFromJson5(root, object)
        );
    }

    public static List<ExampleConfig> examplesFromJson5(
        Path root,
        Json5ObjectWithSource object
    ) {
        return object.getArrayOfObjects("examples")
            .orElseGet(() -> List.of())
            .stream()
            .map(example -> ExampleConfig.fromJson5(root, example))
            .toList();
    }
}