Respect shared attribute in Parser (#5885)

Respects the 'shared' field attribute when reading data with Parser.
This commit is contained in:
Mika Raento 2020-05-04 20:33:11 +03:00 committed by GitHub
parent ff1c78233d
commit 6e9f5d9810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -859,7 +859,7 @@ class Parser : public ParserState {
const std::string &name, const Type &type,
FieldDef **dest);
FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def);
FLATBUFFERS_CHECKED_ERROR ParseString(Value &val);
FLATBUFFERS_CHECKED_ERROR ParseString(Value &val, bool use_string_pooling);
FLATBUFFERS_CHECKED_ERROR ParseComma();
FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
size_t parent_fieldn,

View File

@ -878,10 +878,14 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
return NoError();
}
CheckedError Parser::ParseString(Value &val) {
CheckedError Parser::ParseString(Value &val, bool use_string_pooling) {
auto s = attribute_;
EXPECT(kTokenStringConstant);
val.constant = NumToString(builder_.CreateString(s).o);
if (use_string_pooling) {
val.constant = NumToString(builder_.CreateSharedString(s).o);
} else {
val.constant = NumToString(builder_.CreateString(s).o);
}
return NoError();
}
@ -974,7 +978,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
val.constant = NumToString(builder_.GetSize());
}
} else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
ECHECK(ParseString(val));
ECHECK(ParseString(val, field->shared));
} else {
FLATBUFFERS_ASSERT(false);
}
@ -984,7 +988,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
break;
case BASE_TYPE_STRING: {
ECHECK(ParseString(val));
ECHECK(ParseString(val, field->shared));
break;
}
case BASE_TYPE_VECTOR: {
@ -3293,6 +3297,7 @@ bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
if (!nested_flatbuffer) return false;
}
shared = attributes.Lookup("shared") != nullptr;
DeserializeDoc(doc_comment, field->documentation());
return true;
}