1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.langspec.step;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.Json;
22 import jakarta.json.JsonObject;
23 import java.util.Map;
24 import org.mal_lang.langspec.Asset;
25 import org.mal_lang.langspec.Field;
26 import org.mal_lang.langspec.Variable;
27 import org.mal_lang.langspec.builders.step.StepFieldBuilder;
28
29
30
31
32
33
34 public final class StepField extends StepReference {
35 private final Field field;
36
37 private StepField(Asset sourceAsset, Asset targetAsset, Field field) {
38 super(sourceAsset, targetAsset);
39 this.field = requireNonNull(field);
40 }
41
42
43
44
45
46
47
48 public Field getField() {
49 return this.field;
50 }
51
52 @Override
53 public JsonObject toJson() {
54 return Json.createObjectBuilder()
55 .add("type", "field")
56 .add("name", this.field.getName())
57 .build();
58 }
59
60 static StepField fromBuilder(
61 StepFieldBuilder builder,
62 Asset sourceAsset,
63 Map<String, Asset> assets,
64 Map<Variable, Asset> variableTargets) {
65 requireNonNull(builder);
66 requireNonNull(sourceAsset);
67 requireNonNull(assets);
68 requireNonNull(variableTargets);
69 if (!sourceAsset.hasField(builder.getName())) {
70 throw new IllegalArgumentException(
71 String.format("Field \"%s\" not found", builder.getName()));
72 }
73 var field = sourceAsset.getField(builder.getName());
74 return new StepField(sourceAsset, field.getTarget().getAsset(), field);
75 }
76 }