diff --git a/src/main/java/net/openhft/chronicle/wire/YamlWire.java b/src/main/java/net/openhft/chronicle/wire/YamlWire.java index 5506842955..fe333dfa58 100644 --- a/src/main/java/net/openhft/chronicle/wire/YamlWire.java +++ b/src/main/java/net/openhft/chronicle/wire/YamlWire.java @@ -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; } diff --git a/src/test/java/net/openhft/chronicle/wire/issue/Issue739Test.java b/src/test/java/net/openhft/chronicle/wire/issue/Issue739Test.java index 09b551a8aa..d76a689ee5 100644 --- a/src/test/java/net/openhft/chronicle/wire/issue/Issue739Test.java +++ b/src/test/java/net/openhft/chronicle/wire/issue/Issue739Test.java @@ -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().>typedMarshallable().get("three"); + assertEquals("hello", ((One)three.one).text); + assertEquals("hello", ((Two)three.two).text); + assertEquals("world", ((One)three.twoAndHalf).text); + } }