Skip to content

Commit 98acc5a

Browse files
authored
Emit the correct Alias on the key (jbeder#908) (jbeder#929)
1 parent 1c9abc8 commit 98acc5a

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/emitter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ void Emitter::FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child) {
504504
if (m_stream.comment())
505505
m_stream << "\n";
506506
m_stream << IndentTo(lastIndent);
507+
if (m_pState->HasAlias()) {
508+
m_stream << " ";
509+
}
507510
m_stream << ":";
508511
}
509512

@@ -643,6 +646,9 @@ void Emitter::BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child) {
643646
const std::size_t nextIndent = curIndent + m_pState->CurGroupIndent();
644647

645648
if (!m_pState->HasBegunNode()) {
649+
if (m_pState->HasAlias()) {
650+
m_stream << " ";
651+
}
646652
m_stream << ":";
647653
}
648654

@@ -864,6 +870,8 @@ Emitter& Emitter::Write(const _Alias& alias) {
864870

865871
StartedScalar();
866872

873+
m_pState->SetAlias();
874+
867875
return *this;
868876
}
869877

src/emitterstate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ EmitterState::EmitterState()
2929
m_groups{},
3030
m_curIndent(0),
3131
m_hasAnchor(false),
32+
m_hasAlias(false),
3233
m_hasTag(false),
3334
m_hasNonContent(false),
3435
m_docCount(0) {}
@@ -53,6 +54,8 @@ void EmitterState::SetLocalValue(EMITTER_MANIP value) {
5354

5455
void EmitterState::SetAnchor() { m_hasAnchor = true; }
5556

57+
void EmitterState::SetAlias() { m_hasAlias = true; }
58+
5659
void EmitterState::SetTag() { m_hasTag = true; }
5760

5861
void EmitterState::SetNonContent() { m_hasNonContent = true; }
@@ -87,6 +90,7 @@ void EmitterState::StartedNode() {
8790
}
8891

8992
m_hasAnchor = false;
93+
m_hasAlias = false;
9094
m_hasTag = false;
9195
m_hasNonContent = false;
9296
}

src/emitterstate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class EmitterState {
4343

4444
// node handling
4545
void SetAnchor();
46+
void SetAlias();
4647
void SetTag();
4748
void SetNonContent();
4849
void SetLongKey();
@@ -65,6 +66,7 @@ class EmitterState {
6566
std::size_t LastIndent() const;
6667
std::size_t CurIndent() const { return m_curIndent; }
6768
bool HasAnchor() const { return m_hasAnchor; }
69+
bool HasAlias() const { return m_hasAlias; }
6870
bool HasTag() const { return m_hasTag; }
6971
bool HasBegunNode() const {
7072
return m_hasAnchor || m_hasTag || m_hasNonContent;
@@ -187,6 +189,7 @@ class EmitterState {
187189
std::vector<std::unique_ptr<Group>> m_groups;
188190
std::size_t m_curIndent;
189191
bool m_hasAnchor;
192+
bool m_hasAlias;
190193
bool m_hasTag;
191194
bool m_hasNonContent;
192195
std::size_t m_docCount;

test/integration/emitter_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,21 @@ TEST_F(EmitterTest, AliasAndAnchor) {
430430
ExpectEmit("- &fred\n name: Fred\n age: 42\n- *fred");
431431
}
432432

433+
TEST_F(EmitterTest, AliasOnKey) {
434+
out << BeginSeq;
435+
out << Anchor("name") << "Name";
436+
out << BeginMap;
437+
out << Key << Alias("name") << Value << "Fred";
438+
out << EndMap;
439+
out << Flow << BeginMap;
440+
out << Key << Alias("name") << Value << "Mike";
441+
out << EndMap;
442+
out << EndSeq;
443+
ExpectEmit(R"(- &name Name
444+
- *name : Fred
445+
- {*name : Mike})");
446+
}
447+
433448
TEST_F(EmitterTest, AliasAndAnchorWithNull) {
434449
out << BeginSeq;
435450
out << Anchor("fred") << Null;

0 commit comments

Comments
 (0)