[c++] Add support for boolean types in flexbuffers (#4386)

* Add support for boolean types in flexbuffers

* Simplify casting number <=> boolean

* Added comments for tests

* Add proper support for Booleans

* Bad rebase

* No special case for strings

* Removed unused test

* Simplify logic
This commit is contained in:
rouzier
2017-08-04 11:04:28 -04:00
committed by Wouter van Oortmerssen
parent f2b3705c2c
commit a2b1bfc107
4 changed files with 54 additions and 9 deletions

View File

@@ -187,7 +187,8 @@ std::string Namespace::GetFullyQualifiedName(const std::string &name,
TD(Attribute, 270, "attribute") \
TD(Null, 271, "null") \
TD(Service, 272, "rpc_service") \
TD(NativeInclude, 273, "native_include")
TD(NativeInclude, 273, "native_include") \
TD(BooleanConstant, 274, "boolean constant")
#ifdef __GNUC__
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
#endif
@@ -395,7 +396,7 @@ CheckedError Parser::Next() {
// which simplifies our logic downstream.
if (attribute_ == "true" || attribute_ == "false") {
attribute_ = NumToString(attribute_ == "true");
token_ = kTokenIntegerConstant;
token_ = kTokenBooleanConstant;
return NoError();
}
// Check for declaration keywords:
@@ -1343,6 +1344,11 @@ CheckedError Parser::ParseSingleValue(Value &e) {
e,
BASE_TYPE_INT,
&match));
ECHECK(TryTypedValue(kTokenBooleanConstant,
IsBool(e.type.base_type),
e,
BASE_TYPE_BOOL,
&match));
ECHECK(TryTypedValue(kTokenFloatConstant,
IsFloat(e.type.base_type),
e,
@@ -2012,6 +2018,9 @@ CheckedError Parser::SkipAnyJsonValue() {
case kTokenFloatConstant:
EXPECT(kTokenFloatConstant);
break;
case kTokenBooleanConstant:
EXPECT(kTokenBooleanConstant);
break;
default:
return TokenError();
}
@@ -2069,6 +2078,10 @@ CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
builder->Int(StringToInt(attribute_.c_str()));
EXPECT(kTokenIntegerConstant);
break;
case kTokenBooleanConstant:
builder->Bool(StringToInt(attribute_.c_str()) != 0);
EXPECT(kTokenBooleanConstant);
break;
case kTokenFloatConstant:
builder->Double(strtod(attribute_.c_str(), nullptr));
EXPECT(kTokenFloatConstant);