Skip to content

Commit

Permalink
[Go] Write required string fields into the buffer when using Object API
Browse files Browse the repository at this point in the history
In C++, CreateX allows to write the default "" value of a required
string, when the string is not explicitly set. Object API Pack method
uses this implementation of CreateX.

However, in go, despite whether the field is optional or required, it is
always checked against empty string allowing to create messages that
fail to pass the verifier. This commits partially reverts
flatbuffers/google#7719 when the string field is required.
  • Loading branch information
razvanalex committed Sep 23, 2024
1 parent c7a8102 commit 8fdb30d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,11 +1111,16 @@ class GoGenerator : public BaseGenerator {
const std::string offset = field_var + "Offset";

if (IsString(field.value.type)) {
code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n";
code += "\tif t." + field_field + " != \"\" {\n";
code += "\t\t" + offset + " = builder.CreateString(t." + field_field +
")\n";
code += "\t}\n";
if (!field.IsRequired()) {
code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n";
code += "\tif t." + field_field + " != \"\" {\n";
code += "\t\t" + offset + " = builder.CreateString(t." + field_field +
")\n";
code += "\t}\n";
} else {
code += "\t" + offset + " := builder.CreateString(t." + field_field +
")\n";
}
} else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_UCHAR &&
field.value.type.enum_def == nullptr) {
Expand Down

0 comments on commit 8fdb30d

Please sign in to comment.