Skip to content

Commit deb89f0

Browse files
committed
yaml: update for core changes
1 parent f2fb40a commit deb89f0

File tree

9 files changed

+238
-86
lines changed

9 files changed

+238
-86
lines changed

format/yaml/src/main/java/org/spongepowered/configurate/yaml/ConfigurateScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ final class ConfigurateScanner implements Scanner { // Configurate: rename + pac
183183
// 32-bit Unicode (Supplementary characters are supported)
184184
ESCAPE_CODES.put(Character.valueOf('U'), 8);
185185
}
186-
private final StreamReader reader;
186+
final StreamReader reader; // Configurate: private -> package-private
187187
// Had we reached the end of the stream?
188188
private boolean done = false;
189189

format/yaml/src/main/java/org/spongepowered/configurate/yaml/ScalarStyle.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,33 @@
1919
import org.checkerframework.checker.nullness.qual.Nullable;
2020
import org.yaml.snakeyaml.DumperOptions;
2121

22-
import java.util.HashMap;
22+
import java.util.EnumMap;
2323
import java.util.Map;
2424

2525
/**
2626
* Style that can be used to represent a scalar.
2727
*/
2828
public enum ScalarStyle {
2929

30+
/**
31+
* A double-quoted string.
32+
*
33+
* <p><pre>"hello world"</pre></p>
34+
*/
3035
DOUBLE_QUOTED(DumperOptions.ScalarStyle.DOUBLE_QUOTED),
36+
37+
/**
38+
* A single-quoted string.
39+
*
40+
* <p><pre>'hello world'</pre></p>
41+
*/
3142
SINGLE_QUOTED(DumperOptions.ScalarStyle.SINGLE_QUOTED),
3243
UNQUOTED(DumperOptions.ScalarStyle.PLAIN),
3344
FOLDED(DumperOptions.ScalarStyle.FOLDED),
3445
LITERAL(DumperOptions.ScalarStyle.LITERAL)
3546
;
3647

37-
private static final Map<DumperOptions.ScalarStyle, ScalarStyle> BY_SNAKE = new HashMap<>();
48+
private static final Map<DumperOptions.ScalarStyle, ScalarStyle> BY_SNAKE = new EnumMap<>(DumperOptions.ScalarStyle.class);
3849
private final DumperOptions.ScalarStyle snake;
3950

4051
ScalarStyle(final DumperOptions.ScalarStyle snake) {

format/yaml/src/main/java/org/spongepowered/configurate/yaml/Tag.java

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
@AutoValue
3232
public abstract class Tag {
3333

34+
/**
35+
* Create a new builder for a {@link Tag}.
36+
*
37+
* @return a new builder
38+
*/
3439
public static Tag.Builder builder() {
3540
return new AutoValue_Tag.Builder();
3641
}
@@ -43,53 +48,88 @@ public static Tag.Builder builder() {
4348
*
4449
* @return tag uri, with `tag:` schema
4550
*/
46-
public abstract URI getUri();
51+
public abstract URI uri();
4752

4853
/**
4954
* The native type that maps to this tag.
5055
*
5156
* @return native type for tag
5257
*/
53-
public abstract Type getNativeType();
58+
public abstract Type nativeType();
5459

5560
/**
5661
* Pattern to test scalar values against when resolving this tag.
5762
*
5863
* @return match pattern
5964
* @apiNote See §3.3.2 of YAML 1.1 spec
6065
*/
61-
public abstract Pattern getTargetPattern();
66+
public abstract Pattern targetPattern();
6267

6368
/**
6469
* Whether this tag is a global tag with a full namespace or a local one.
6570
*
6671
* @return if this is a global tag
6772
*/
68-
public final boolean isGlobal() {
69-
return getUri().getScheme().equals("tag");
73+
public final boolean global() {
74+
return uri().getScheme().equals("tag");
7075
}
7176

77+
/**
78+
* A builder for {@link Tag Tags}.
79+
*/
7280
@AutoValue.Builder
7381
public abstract static class Builder {
7482

75-
public abstract Builder setUri(URI url);
76-
77-
public final Builder setUri(final String tagUrl) {
83+
/**
84+
* Set the URI used to refer to the tag.
85+
*
86+
* @param url canonical tag URI
87+
* @return this builder
88+
*/
89+
public abstract Builder uri(URI url);
90+
91+
/**
92+
* Set the URI used to refer to the tag, parsing a new URL from
93+
* the argument.
94+
*
95+
* @param tagUrl canonical tag URI
96+
* @return this builder
97+
*/
98+
public final Builder uri(final String tagUrl) {
7899
try {
79100
if (tagUrl.startsWith("!")) {
80-
return this.setUri(new URI(tagUrl.substring(1)));
101+
return this.uri(new URI(tagUrl.substring(1)));
81102
} else {
82-
return this.setUri(new URI(tagUrl));
103+
return this.uri(new URI(tagUrl));
83104
}
84105
} catch (final URISyntaxException e) {
85106
throw new RuntimeException(e);
86107
}
87108
}
88109

89-
public abstract Builder setNativeType(Type type);
90-
91-
public abstract Builder setTargetPattern(Pattern targetPattern);
92-
110+
/**
111+
* The Java type that will be used to represent this value in the node
112+
* structure.
113+
*
114+
* @param type type for the value
115+
* @return this builder
116+
*/
117+
public abstract Builder nativeType(Type type);
118+
119+
/**
120+
* Pattern to match an undefined scalar string to this tag as an
121+
* <em>implicit tag</em>.
122+
*
123+
* @param targetPattern pattern to match
124+
* @return this builder
125+
*/
126+
public abstract Builder targetPattern(Pattern targetPattern);
127+
128+
/**
129+
* Create a new tag from the provided parameters.
130+
*
131+
* @return a new tag
132+
*/
93133
public abstract Tag build();
94134

95135
}

format/yaml/src/main/java/org/spongepowered/configurate/yaml/TagRepository.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27+
/**
28+
* A collection of tags that are understood when reading a document.
29+
*/
2730
public final class TagRepository {
2831

2932
private final List<Tag> tags;
3033
private final Map<Class<?>, Tag> byErasedType;
3134
private final Map<String, Tag> byName;
3235

33-
3436
/**
3537
* Create a new tag repository.
3638
*
@@ -45,19 +47,25 @@ public static TagRepository of(final List<Tag> tags) {
4547
this.tags = tags;
4648
this.byErasedType = UnmodifiableCollections.buildMap(map -> {
4749
for (final Tag tag : this.tags) {
48-
map.put(erase(tag.getNativeType()), tag);
50+
map.put(erase(tag.nativeType()), tag);
4951
}
5052
});
5153
this.byName = UnmodifiableCollections.buildMap(map -> {
5254
for (final Tag tag : this.tags) {
53-
map.put(tag.getUri().toString(), tag);
55+
map.put(tag.uri().toString(), tag);
5456
}
5557
});
5658
}
5759

60+
/**
61+
* Determine the implicit tag for a scalar value.
62+
*
63+
* @param scalar scalar to test
64+
* @return the first matching tag
65+
*/
5866
public @Nullable Tag forInput(final String scalar) {
5967
for (final Tag tag : this.tags) {
60-
if (tag.getTargetPattern().matcher(scalar).matches()) {
68+
if (tag.targetPattern().matcher(scalar).matches()) {
6169
return tag;
6270
}
6371
}

format/yaml/src/main/java/org/spongepowered/configurate/yaml/Yaml11Tags.java

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,73 +34,113 @@ private static String yamlOrg(final String specific) {
3434
return "tag:yaml.org,2002:" + specific;
3535
}
3636

37-
// https://yaml.org/type/binary.html
37+
/**
38+
* A binary data tag.
39+
*
40+
* @see <a href="https://yaml.org/type/binary.html">tag:yaml.org,2002:binary</a>
41+
*/
3842
public static final Tag BINARY = Tag.builder()
39-
.setUri(yamlOrg("binary"))
40-
.setNativeType(byte[].class)
41-
.setTargetPattern(Pattern.compile("base64 TODO"))
43+
.uri(yamlOrg("binary"))
44+
.nativeType(byte[].class)
45+
.targetPattern(Pattern.compile("base64 TODO"))
4246
.build();
4347

44-
// https://yaml.org/type/bool.html
45-
// Canonically these are y|n in YAML 1.1, but because YAML 1.2 moves to
46-
// true|false only, we'll just use those
48+
/**
49+
* A boolean value.
50+
*
51+
* @implNote Canonically, these are y|n in YAML 1.1, but because YAML 1.2
52+
* will only support true|false, we will treat those as the default
53+
* output format.
54+
* @see <a href="https://yaml.org/type/bool.html">tag:yaml.org,2002:bool</a>
55+
*/
4756
public static final Tag BOOL = Tag.builder()
48-
.setUri(yamlOrg("bool"))
49-
.setNativeType(Boolean.class)
50-
.setTargetPattern(Pattern.compile("y|Y|yes|Yes|YES|n|N|no|No|NO"
57+
.uri(yamlOrg("bool"))
58+
.nativeType(Boolean.class)
59+
.targetPattern(Pattern.compile("y|Y|yes|Yes|YES|n|N|no|No|NO"
5160
+ "|true|True|TRUE|false|False|FALSE"
5261
+ "|on|On|ON|off|Off|OFF"))
5362
.build();
5463

55-
// https://yaml.org/type/float.html
64+
/**
65+
* A floating-point number.
66+
*
67+
* @see <a href="https://yaml.org/type/float.html">tag:yaml.org,2002:float</a>
68+
*/
5669
public static final Tag FLOAT = Tag.builder()
57-
.setUri(yamlOrg("float"))
58-
.setNativeType(Double.class)
59-
.setTargetPattern(Pattern.compile("[-+]?([0-9][0-9_]*)?\\.[0-9.]*([eE][-+][0-9]+)?" // base 10
70+
.uri(yamlOrg("float"))
71+
.nativeType(Double.class)
72+
.targetPattern(Pattern.compile("[-+]?([0-9][0-9_]*)?\\.[0-9.]*([eE][-+][0-9]+)?" // base 10
6073
+ "|[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\\.[0-9]*" // base 60
6174
+ "|[-+]?\\.(inf|Inf|INF)" // infinity
6275
+ "|\\.(nan|NaN|NAN)")) // not a number
6376
.build();
6477

65-
// https://yaml.org/type/int.html
78+
/**
79+
* An integer.
80+
*
81+
* @see <a href="https://yaml.org/type/int.html">tag:yaml.org,2002:int</a>
82+
*/
6683
public static final Tag INT = Tag.builder()
67-
.setUri(yamlOrg("int"))
68-
.setNativeType(Long.class)
69-
.setTargetPattern(Pattern.compile("[-+]?0b[0-1_]+" // base 2
84+
.uri(yamlOrg("int"))
85+
.nativeType(Long.class)
86+
.targetPattern(Pattern.compile("[-+]?0b[0-1_]+" // base 2
7087
+ "|[-+]?0[0-7_]+" // base 8
7188
+ "|[-+]?(0|[1-9][0-9_]*)" // base 10
7289
+ "|[-+]?0x[0-9a-fA-F_]+" // base 16
7390
+ "|[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+")) // base 60
7491
.build();
7592

76-
// https://yaml.org/type/merge.html
93+
/**
94+
* A mapping merge.
95+
*
96+
* <p>This will not be supported in Configurate until reference-type nodes
97+
* are fully implemented.</p>
98+
*
99+
* @see <a href="https://yaml.org/type/merge.html">tag:yaml.org,2002:merge</a>
100+
*/
77101
public static final Tag MERGE = Tag.builder()
78-
.setUri(yamlOrg("merge"))
79-
.setNativeType(ConfigurationNode.class)
80-
.setTargetPattern(Pattern.compile("<<"))
102+
.uri(yamlOrg("merge"))
103+
.nativeType(ConfigurationNode.class)
104+
.targetPattern(Pattern.compile("<<"))
81105
.build();
82106

83-
// https://yaml.org/type/null.html
107+
/**
108+
* The value {@code null}.
109+
*
110+
* <p>Because Configurate has no distinction between a node with a
111+
* {@code null} value, and a node that does not exist, this tag will most
112+
* likely never be encountered in an in-memory representation.</p>
113+
*
114+
* @see <a href="https://yaml.org/type/null.html">tag:yaml.org,2002:null</a>
115+
*/
84116
public static final Tag NULL = Tag.builder()
85-
.setUri(yamlOrg("null"))
86-
.setNativeType(Void.class)
87-
.setTargetPattern(Pattern.compile("~"
117+
.uri(yamlOrg("null"))
118+
.nativeType(Void.class)
119+
.targetPattern(Pattern.compile("~"
88120
+ "|null|Null|NULL"
89121
+ "|$"))
90122
.build();
91123

92-
// https://yaml.org/type/str.html
124+
/**
125+
* Any string.
126+
*
127+
* @see <a href="https://yaml.org/type/str.html">tag:yaml.org,2002:str</a>
128+
*/
93129
public static final Tag STR = Tag.builder()
94-
.setUri(yamlOrg("str"))
95-
.setNativeType(String.class)
96-
.setTargetPattern(Pattern.compile(".+")) // empty scalar is NULL
130+
.uri(yamlOrg("str"))
131+
.nativeType(String.class)
132+
.targetPattern(Pattern.compile(".+")) // empty scalar is NULL
97133
.build();
98134

99-
// https://yaml.org/type/timestamp.html
135+
/**
136+
* A timestamp, containing date, time, and timezone.
137+
*
138+
* @see <a href="https://yaml.org/type/timestamp.html">tag:yaml.org,2002:timestamp</a>
139+
*/
100140
public static final Tag TIMESTAMP = Tag.builder()
101-
.setUri(yamlOrg("timestamp"))
102-
.setNativeType(ZonedDateTime.class)
103-
.setTargetPattern(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}" // YYYY-MM-DD
141+
.uri(yamlOrg("timestamp"))
142+
.nativeType(ZonedDateTime.class)
143+
.targetPattern(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}" // YYYY-MM-DD
104144
+ "|[0-9]{4}" // YYYY
105145
+ "-[0-9]{1,2}" // month
106146
+ "-[0-9]{1,2}" // day

format/yaml/src/main/java/org/spongepowered/configurate/yaml/YamlConfigurationLoader.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,25 @@
4848
*/
4949
public final class YamlConfigurationLoader extends AbstractConfigurationLoader<CommentedConfigurationNode> {
5050

51+
/**
52+
* The identifier for a YAML anchor that can be used to refer to the node
53+
* this hint is set on.
54+
*/
5155
public static final RepresentationHint<String> ANCHOR_ID = RepresentationHint.of("anchor-id", String.class);
5256

57+
/**
58+
* The YAML scalar style this node should attempt to use.
59+
*
60+
* <p>If the chosen scalar style would produce syntactically invalid YAML, a
61+
* valid one will replace it.</p>
62+
*/
5363
public static final RepresentationHint<ScalarStyle> SCALAR_STYLE = RepresentationHint.of("scalar-style", ScalarStyle.class);
5464

65+
/**
66+
* The YAML node style to use for collection nodes. A {@code null} value
67+
* will instruct the emitter to fall back to the
68+
* {@link Builder#nodeStyle()} setting.
69+
*/
5570
public static final RepresentationHint<NodeStyle> NODE_STYLE = RepresentationHint.of("node-style", NodeStyle.class);
5671

5772
/**

0 commit comments

Comments
 (0)