Skip to content

Commit 6d5cfab

Browse files
authored
fix bug from issue298:Emitter stylings settings overridden by node settings. (jbeder#915)
1 parent 026a53f commit 6d5cfab

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

include/yaml-cpp/emitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class YAML_CPP_API Emitter {
5959
bool SetPostCommentIndent(std::size_t n);
6060
bool SetFloatPrecision(std::size_t n);
6161
bool SetDoublePrecision(std::size_t n);
62+
void RestoreGlobalModifiedSettings();
6263

6364
// local setters
6465
Emitter& SetLocalValue(EMITTER_MANIP value);

src/emitfromevents.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void EmitFromEvents::OnSequenceStart(const Mark&, const std::string& tag,
5959
default:
6060
break;
6161
}
62+
// Restore the global settings to eliminate the override from node style
63+
m_emitter.RestoreGlobalModifiedSettings();
6264
m_emitter << BeginSeq;
6365
m_stateStack.push(State::WaitingForSequenceEntry);
6466
}
@@ -83,6 +85,8 @@ void EmitFromEvents::OnMapStart(const Mark&, const std::string& tag,
8385
default:
8486
break;
8587
}
88+
// Restore the global settings to eliminate the override from node style
89+
m_emitter.RestoreGlobalModifiedSettings();
8690
m_emitter << BeginMap;
8791
m_stateStack.push(State::WaitingForKey);
8892
}

src/emitter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ bool Emitter::SetDoublePrecision(std::size_t n) {
9090
return m_pState->SetDoublePrecision(n, FmtScope::Global);
9191
}
9292

93+
void Emitter::RestoreGlobalModifiedSettings() {
94+
m_pState->RestoreGlobalModifiedSettings();
95+
}
96+
9397
// SetLocalValue
9498
// . Either start/end a group, or set a modifier locally
9599
Emitter& Emitter::SetLocalValue(EMITTER_MANIP value) {

src/emitterstate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ std::size_t EmitterState::LastIndent() const {
222222

223223
void EmitterState::ClearModifiedSettings() { m_modifiedSettings.clear(); }
224224

225+
void EmitterState::RestoreGlobalModifiedSettings() {
226+
m_globalModifiedSettings.restore();
227+
}
228+
225229
bool EmitterState::SetOutputCharset(EMITTER_MANIP value,
226230
FmtScope::value scope) {
227231
switch (value) {

src/emitterstate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class EmitterState {
7272
bool HasBegunContent() const { return m_hasAnchor || m_hasTag; }
7373

7474
void ClearModifiedSettings();
75+
void RestoreGlobalModifiedSettings();
7576

7677
// formatters
7778
void SetLocalValue(EMITTER_MANIP value);

test/integration/emitter_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,31 @@ TEST_F(EmitterTest, GlobalLongKeyOnMap) {
732732
: *value)");
733733
}
734734

735+
TEST_F(EmitterTest, GlobalSettingStyleOnSeqNode) {
736+
Node n(Load(R"(foo:
737+
- 1
738+
- 2
739+
- 3
740+
bar: aa)"));
741+
out.SetSeqFormat(YAML::Flow);
742+
out << n;
743+
ExpectEmit(R"(foo: [1, 2, 3]
744+
bar: aa)");
745+
}
746+
747+
TEST_F(EmitterTest, GlobalSettingStyleOnMapNode) {
748+
Node n(Load(R"(-
749+
foo: a
750+
bar: b
751+
- 2
752+
- 3)"));
753+
out.SetMapFormat(YAML::Flow);
754+
out << n;
755+
ExpectEmit(R"(- {foo: a, bar: b}
756+
- 2
757+
- 3)");
758+
}
759+
735760
TEST_F(EmitterTest, ComplexGlobalSettings) {
736761
out << BeginSeq;
737762
out << Block;

0 commit comments

Comments
 (0)