Support kinds of emitter format-setting for YAML::Null. (#906)

* Support kinds of emitter format-setting for YAML::Null.

* update the code and test cases

* add the comment //fallthrough
This commit is contained in:
Chen 2020-06-29 12:31:53 +08:00 committed by GitHub
parent 27d8a0e302
commit 08aa252611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 1 deletions

View File

@ -50,6 +50,7 @@ class YAML_CPP_API Emitter {
bool SetOutputCharset(EMITTER_MANIP value);
bool SetStringFormat(EMITTER_MANIP value);
bool SetBoolFormat(EMITTER_MANIP value);
bool SetNullFormat(EMITTER_MANIP value);
bool SetIntBase(EMITTER_MANIP value);
bool SetSeqFormat(EMITTER_MANIP value);
bool SetMapFormat(EMITTER_MANIP value);
@ -123,6 +124,7 @@ class YAML_CPP_API Emitter {
void SpaceOrIndentTo(bool requireSpace, std::size_t indent);
const char* ComputeFullBoolName(bool b) const;
const char* ComputeNullName() const;
bool CanEmitNewline() const;
private:

View File

@ -26,6 +26,12 @@ enum EMITTER_MANIP {
DoubleQuoted,
Literal,
// null manipulators
LowerNull,
UpperNull,
CamelNull,
TildeNull,
// bool manipulators
YesNoBool, // yes, no
TrueFalseBool, // true, false

BIN
src/.emitterstate.h.swp Normal file

Binary file not shown.

View File

@ -49,6 +49,10 @@ bool Emitter::SetBoolFormat(EMITTER_MANIP value) {
return ok;
}
bool Emitter::SetNullFormat(EMITTER_MANIP value) {
return m_pState->SetNullFormat(value, FmtScope::Global);
}
bool Emitter::SetIntBase(EMITTER_MANIP value) {
return m_pState->SetIntFormat(value, FmtScope::Global);
}
@ -770,6 +774,21 @@ const char* Emitter::ComputeFullBoolName(bool b) const {
// these answers
}
const char* Emitter::ComputeNullName() const {
switch (m_pState->GetNullFormat()) {
case LowerNull:
return "null";
case UpperNull:
return "NULL";
case CamelNull:
return "Null";
case TildeNull:
// fallthrough
default:
return "~";
}
}
Emitter& Emitter::Write(bool b) {
if (!good())
return *this;
@ -893,7 +912,7 @@ Emitter& Emitter::Write(const _Null& /*null*/) {
PrepareNode(EmitterNodeType::Scalar);
m_stream << "~";
m_stream << ComputeNullName();
StartedScalar();

View File

@ -13,6 +13,7 @@ EmitterState::EmitterState()
m_boolFmt(TrueFalseBool),
m_boolLengthFmt(LongBool),
m_boolCaseFmt(LowerCase),
m_nullFmt(TildeNull),
m_intFmt(Dec),
m_indent(2),
m_preCommentIndent(2),
@ -43,6 +44,7 @@ void EmitterState::SetLocalValue(EMITTER_MANIP value) {
SetBoolFormat(value, FmtScope::Local);
SetBoolCaseFormat(value, FmtScope::Local);
SetBoolLengthFormat(value, FmtScope::Local);
SetNullFormat(value, FmtScope::Local);
SetIntFormat(value, FmtScope::Local);
SetFlowType(GroupType::Seq, value, FmtScope::Local);
SetFlowType(GroupType::Map, value, FmtScope::Local);
@ -279,6 +281,19 @@ bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value,
}
}
bool EmitterState::SetNullFormat(EMITTER_MANIP value, FmtScope::value scope) {
switch (value) {
case LowerNull:
case UpperNull:
case CamelNull:
case TildeNull:
_Set(m_nullFmt, value, scope);
return true;
default:
return false;
}
}
bool EmitterState::SetIntFormat(EMITTER_MANIP value, FmtScope::value scope) {
switch (value) {
case Dec:

View File

@ -91,6 +91,9 @@ class EmitterState {
bool SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope);
EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }
bool SetNullFormat(EMITTER_MANIP value, FmtScope::value scope);
EMITTER_MANIP GetNullFormat() const { return m_nullFmt.get(); }
bool SetIntFormat(EMITTER_MANIP value, FmtScope::value scope);
EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }
@ -131,6 +134,7 @@ class EmitterState {
Setting<EMITTER_MANIP> m_boolFmt;
Setting<EMITTER_MANIP> m_boolLengthFmt;
Setting<EMITTER_MANIP> m_boolCaseFmt;
Setting<EMITTER_MANIP> m_nullFmt;
Setting<EMITTER_MANIP> m_intFmt;
Setting<std::size_t> m_indent;
Setting<std::size_t> m_preCommentIndent, m_postCommentIndent;

View File

@ -1047,6 +1047,45 @@ TEST_F(EmitterTest, BoolFormatting) {
"- Y\n- Y\n- y\n- N\n- N\n- n");
}
TEST_F(EmitterTest, GlobalNullFormatting) {
out << Flow << BeginSeq;
out.SetNullFormat(LowerNull);
out << Null;
out.SetNullFormat(UpperNull);
out << Null;
out.SetNullFormat(CamelNull);
out << Null;
out.SetNullFormat(TildeNull);
out << Null;
out << EndSeq;
ExpectEmit("[null, NULL, Null, ~]");
}
TEST_F(EmitterTest, NullFormatting) {
out << Flow << BeginSeq;
out << LowerNull << Null;
out << UpperNull << Null;
out << CamelNull << Null;
out << TildeNull << Null;
out << EndSeq;
ExpectEmit("[null, NULL, Null, ~]");
}
TEST_F(EmitterTest, NullFormattingOnNode) {
Node n(Load("null"));
out << Flow << BeginSeq;
out.SetNullFormat(LowerNull);
out << n;
out.SetNullFormat(UpperNull);
out << n;
out.SetNullFormat(CamelNull);
out << n;
out.SetNullFormat(TildeNull);
out << n;
out << EndSeq;
ExpectEmit("[null, NULL, Null, ~]");
}
// TODO: Fix this test.
// TEST_F(EmitterTest, DocStartAndEnd) {
// out << BeginDoc;