From 98acc5a8874faab28b82c28936f4b400b389f5d6 Mon Sep 17 00:00:00 2001 From: Chen Date: Tue, 28 Jul 2020 02:49:04 +0800 Subject: [PATCH] Emit the correct Alias on the key (#908) (#929) --- src/emitter.cpp | 8 ++++++++ src/emitterstate.cpp | 4 ++++ src/emitterstate.h | 3 +++ test/integration/emitter_test.cpp | 15 +++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/emitter.cpp b/src/emitter.cpp index 251df8cbf..644b3129b 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -504,6 +504,9 @@ void Emitter::FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child) { if (m_stream.comment()) m_stream << "\n"; m_stream << IndentTo(lastIndent); + if (m_pState->HasAlias()) { + m_stream << " "; + } m_stream << ":"; } @@ -643,6 +646,9 @@ void Emitter::BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child) { const std::size_t nextIndent = curIndent + m_pState->CurGroupIndent(); if (!m_pState->HasBegunNode()) { + if (m_pState->HasAlias()) { + m_stream << " "; + } m_stream << ":"; } @@ -864,6 +870,8 @@ Emitter& Emitter::Write(const _Alias& alias) { StartedScalar(); + m_pState->SetAlias(); + return *this; } diff --git a/src/emitterstate.cpp b/src/emitterstate.cpp index e7344810d..3dbe40110 100644 --- a/src/emitterstate.cpp +++ b/src/emitterstate.cpp @@ -29,6 +29,7 @@ EmitterState::EmitterState() m_groups{}, m_curIndent(0), m_hasAnchor(false), + m_hasAlias(false), m_hasTag(false), m_hasNonContent(false), m_docCount(0) {} @@ -53,6 +54,8 @@ void EmitterState::SetLocalValue(EMITTER_MANIP value) { void EmitterState::SetAnchor() { m_hasAnchor = true; } +void EmitterState::SetAlias() { m_hasAlias = true; } + void EmitterState::SetTag() { m_hasTag = true; } void EmitterState::SetNonContent() { m_hasNonContent = true; } @@ -87,6 +90,7 @@ void EmitterState::StartedNode() { } m_hasAnchor = false; + m_hasAlias = false; m_hasTag = false; m_hasNonContent = false; } diff --git a/src/emitterstate.h b/src/emitterstate.h index c83544c17..8f379ca95 100644 --- a/src/emitterstate.h +++ b/src/emitterstate.h @@ -43,6 +43,7 @@ class EmitterState { // node handling void SetAnchor(); + void SetAlias(); void SetTag(); void SetNonContent(); void SetLongKey(); @@ -65,6 +66,7 @@ class EmitterState { std::size_t LastIndent() const; std::size_t CurIndent() const { return m_curIndent; } bool HasAnchor() const { return m_hasAnchor; } + bool HasAlias() const { return m_hasAlias; } bool HasTag() const { return m_hasTag; } bool HasBegunNode() const { return m_hasAnchor || m_hasTag || m_hasNonContent; @@ -187,6 +189,7 @@ class EmitterState { std::vector> m_groups; std::size_t m_curIndent; bool m_hasAnchor; + bool m_hasAlias; bool m_hasTag; bool m_hasNonContent; std::size_t m_docCount; diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index c79ade43c..66198a62d 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -430,6 +430,21 @@ TEST_F(EmitterTest, AliasAndAnchor) { ExpectEmit("- &fred\n name: Fred\n age: 42\n- *fred"); } +TEST_F(EmitterTest, AliasOnKey) { + out << BeginSeq; + out << Anchor("name") << "Name"; + out << BeginMap; + out << Key << Alias("name") << Value << "Fred"; + out << EndMap; + out << Flow << BeginMap; + out << Key << Alias("name") << Value << "Mike"; + out << EndMap; + out << EndSeq; + ExpectEmit(R"(- &name Name +- *name : Fred +- {*name : Mike})"); +} + TEST_F(EmitterTest, AliasAndAnchorWithNull) { out << BeginSeq; out << Anchor("fred") << Null;