Skip to content

Commit

Permalink
ReadYamlMapping.ReadScalarKey
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed May 27, 2024
1 parent c9e72ca commit 0a81c24
Showing 1 changed file with 85 additions and 3 deletions.
88 changes: 85 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ public Set<YamlNode> keys() {
//@checkstyle NestedIfDepth (50 lines)
final String key = matcher.group("key");
if (key != null && !key.isEmpty()) {
keys.add(new PlainStringScalar(key));
keys.add(new ReadScalarKey(key, this.all, line));
} else {
final String keyQ = matcher.group("keyQ");
if (keyQ != null && !keyQ.isEmpty()) {
keys.add(new PlainStringScalar(keyQ));
keys.add(new ReadScalarKey(keyQ, this.all, line));
} else {
final String keySQ = matcher.group("keySQ");
if (keySQ != null && !keySQ.isEmpty()) {
keys.add(new PlainStringScalar(keySQ));
keys.add(new ReadScalarKey(
keySQ, this.all, line)
);
}
}
}
Expand Down Expand Up @@ -358,4 +360,84 @@ private YamlLine getPreviousLine(final YamlLine line) {
}
return prev;
}

/**
* A plain scalar mapping key.
* @author Mihai Andronace ([email protected])
* @version $Id$
* @since 3.1.3
*/
private static class ReadScalarKey extends BaseScalar {

/**
* String key.
*/
private final String key;

/**
* All YAML Lines of the document.
*/
private final AllYamlLines all;

/**
* Line where the plain scalar key is found.
*/
private final YamlLine scalarLine;

/**
* Constructor.
*
* @param key String key.
* @param all All lines of the document.
* @param scalarLine YamlLine containing the scalar.
*/
ReadScalarKey(
final String key, final AllYamlLines all, final YamlLine scalarLine
) {
this.key = key;
this.all = all;
this.scalarLine = scalarLine;
}

/**
* Unescaped String value of this scalar. Pay attention, if the
* scalar's value is the "null" String, then we return null, because
* "null" is a reserved keyword in YAML, indicating a null Scalar.
*
* @return String or null if the Strings value is "null".
* @checkstyle ReturnCount (50 lines)
*/
@Override
public String value() {
return this.key;
}

@Override
public Comment comment() {
final Comment comment;
if (this.scalarLine instanceof YamlLine.NullYamlLine) {
comment = new BuiltComment(this, "");
} else {
final int lineNumber = this.scalarLine.number();
comment = new ReadComment(
new Backwards(
new FirstCommentFound(
new Backwards(
new Skip(
this.all,
line -> line.number() >= lineNumber,
line -> line.trimmed().startsWith("..."),
line -> line.trimmed().startsWith("%"),
line -> line.trimmed().startsWith("!!")
)
),
false
)
),
this
);
}
return comment;
}
}
}

0 comments on commit 0a81c24

Please sign in to comment.