Skip to content

Commit

Permalink
Allow anchor/alias of text fields
Browse files Browse the repository at this point in the history
closes #898
  • Loading branch information
alamar committed Jun 6, 2024
1 parent c23c09f commit 419790c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
64 changes: 44 additions & 20 deletions src/main/java/net/openhft/chronicle/wire/YamlWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -1268,34 +1268,58 @@ StringBuilder textTo0(@NotNull StringBuilder a) {
if (yt.current() == YamlToken.SEQUENCE_ENTRY)
yt.next();

// handle text or literal tokens
if (yt.current() == YamlToken.TEXT || yt.current() == YamlToken.LITERAL) {
a.append(yt.text()); // append the text value
switch (yt.current()) {
// handle text or literal tokens
case TEXT:
case LITERAL:
a.append(yt.text()); // append the text value

// unescape the text value if needed
if (yt.current() == YamlToken.TEXT)
unescape(a, yt.blockQuote());
// unescape the text value if needed
if (yt.current() == YamlToken.TEXT)
unescape(a, yt.blockQuote());

// handle tag tokens
} else if (yt.current() == YamlToken.TAG) {
// check for a NULL tag and move to the next token
if (yt.isText(NULL_TAG)) {
break;

case ANCHOR:
// Handle YAML anchors, which can be referred to later as aliases
String alias = yt.text();
yt.next();
textTo0(sb);
// Store the anchor for later reference
anchorValues.put(alias, sb.toString());
break;

return null;
}
case ALIAS:
// Retrieve the actual object that an alias refers to
alias = yt.text();
Object o = anchorValues.get(alias);
if (o == null)
throw new IllegalStateException("Unknown alias " + alias + " with no corresponding anchor");

// check for a BINARY tag, decode and append its value
if (yt.isText(BINARY_TAG)) {
yt.next();
final byte[] arr = (byte[]) decodeBinary(byte[].class);
for (byte b : arr) {
a.append((char) b);
sb.append(o);
break;

// handle tag tokens
case TAG:
// check for a NULL tag and move to the next token
if (yt.isText(NULL_TAG)) {
yt.next();

return null;
}
return a;
}

throw new UnsupportedOperationException(yt.toString());
// check for a BINARY tag, decode and append its value
if (yt.isText(BINARY_TAG)) {
yt.next();
final byte[] arr = (byte[]) decodeBinary(byte[].class);
for (byte b : arr) {
a.append((char) b);
}
return a;
}

throw new UnsupportedOperationException(yt.toString());
}
return a;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/issue/Issue739Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ public void interfaceFieldAnchorAlias() {
assertEquals("hello", ((One)three.twoAndHalf).text);
assertSame(three.one, three.twoAndHalf);
}

@Test
public void anchorOfTextField() {
Wire wire = new YamlWire(Bytes.wrapForRead(("three: !net.openhft.chronicle.wire.issue.Issue739Test$IThree\n" +
" one: &first !net.openhft.chronicle.wire.issue.Issue739Test$One\n" +
" text: &msg hello\n" +
" two: !net.openhft.chronicle.wire.issue.Issue739Test$Two\n" +
" text: *msg\n" +
" twoAndHalf: !net.openhft.chronicle.wire.issue.Issue739Test$One\n" +
" text: world\n").getBytes()));
IThree three = (IThree) wire.getValueIn().<Map<String, Object>>typedMarshallable().get("three");
assertEquals("hello", ((One)three.one).text);
assertEquals("hello", ((Two)three.two).text);
assertEquals("world", ((One)three.twoAndHalf).text);
}
}

0 comments on commit 419790c

Please sign in to comment.