Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed May 15, 2022
1 parent a13dc38 commit 1a57de0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public final Set<Class<?>> supportedTypes() {
abstract static class Scalar<V> extends Tag {

private final @Nullable Pattern pattern;
private final @Nullable ScalarStyle preferredScalarStyle;

// for unregistered tags on scalars
static Scalar<String> ofUnknown(final URI tagUri) {
return new Scalar<String>(tagUri, Collections.emptySet(), null) {
return new Scalar<String>(tagUri, Collections.emptySet(), null, null) {
@Override
public String fromString(final String input) {
return input;
Expand All @@ -65,8 +66,13 @@ public String toString(final String own) {
}

Scalar(final URI tagUri, final Set<Class<? extends V>> supportedTypes, final @Nullable Pattern pattern) {
this(tagUri, supportedTypes, pattern, null);
}

Scalar(final URI tagUri, final Set<Class<? extends V>> supportedTypes, final @Nullable Pattern pattern, final @Nullable ScalarStyle preferredScalarStyle) {
super(tagUri, supportedTypes);
this.pattern = pattern;
this.preferredScalarStyle = preferredScalarStyle;
}

/**
Expand All @@ -76,11 +82,22 @@ public String toString(final String own) {
* implicit tag.</p>
*
* @return the detection pattern
* @since 4.2.0
*/
public final @Nullable Pattern pattern() {
return this.pattern;
}

/**
* Get the preferred scalar style to use for this type, when none is specifically used.
*
* @return the preferred scalar style
* @since 4.2.0
*/
public final @Nullable ScalarStyle preferredScalarStyle() {
return this.preferredScalarStyle;
}

public abstract V fromString(String input) throws ParsingException;

public abstract String toString(V own) throws ConfigurateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static TagRepository.Builder builder() {
* @return a calculated tag
* @since 4.2.0
*/
@SuppressWarnings("rawtypes")
AnalyzedTag analyze(final ConfigurationNode node) throws ConfigurateException {
final @Nullable Tag explicit = node.ownHint(YamlConfigurationLoader.TAG);
final @Nullable Tag calculated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
* where useful).</li>
* <li>Alias nodes and merge keys: flattened on load, not yet supported by
* the Configurate object model</li>
* <li>keys: limited, tag and representation information is lost when using
* <li>Keys: limited, tag and representation information is lost when using
* complex keys (since keys are not preserved as a node)</li>
* </ul>
*
Expand Down Expand Up @@ -191,8 +191,8 @@ public static final class Builder extends AbstractConfigurationLoader.Builder<Bu
private boolean enableComments = COMMENTS_DEFAULT;

Builder() {
indent(4);
defaultOptions(o -> o.nativeTypes(NATIVE_TYPES));
this.indent(4);
this.defaultOptions(o -> o.nativeTypes(NATIVE_TYPES));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,25 @@ public void enterNode(final ConfigurationNode node, final State state) throws Co
public void enterMappingNode(final ConfigurationNode node, final State state) throws ConfigurateException {
final TagRepository.AnalyzedTag analysis = this.tags.analyze(node);
state.emit(new MappingStartEvent(
anchor(node),
this.anchor(node),
analysis.actual().tagUri().toString(),
analysis.implicit(),
null,
null,
NodeStyle.asSnakeYaml(determineStyle(node, state))
NodeStyle.asSnakeYaml(this.determineStyle(node, state))
));
}

@Override
public void enterListNode(final ConfigurationNode node, final State state) throws ConfigurateException {
final TagRepository.AnalyzedTag analysis = this.tags.analyze(node);
state.emit(new SequenceStartEvent(
anchor(node),
this.anchor(node),
analysis.actual().tagUri().toString(),
analysis.implicit(),
null,
null,
NodeStyle.asSnakeYaml(determineStyle(node, state))
NodeStyle.asSnakeYaml(this.determineStyle(node, state))
));
}

Expand All @@ -149,14 +149,18 @@ public void enterScalarNode(final ConfigurationNode node, final State state) thr
}

state.emit(new ScalarEvent(
anchor(node),
this.anchor(node),
actual.tagUri().toString(),
implicity,
((Tag.Scalar<Object>) actual).toString(node.rawScalar()),
null,
null,
// todo: support configuring default scalar style
ScalarStyle.asSnakeYaml(node.hint(YamlConfigurationLoader.SCALAR_STYLE), implicity, null)
ScalarStyle.asSnakeYaml(
node.hint(YamlConfigurationLoader.SCALAR_STYLE),
implicity,
((Tag.Scalar<?>) actual).preferredScalarStyle()
)
));
}

Expand Down

0 comments on commit 1a57de0

Please sign in to comment.