Perform nil check on string fields when packing (#7719)

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Michael Le 2022-12-22 15:28:00 -05:00 committed by GitHub
parent 4e396d47bc
commit 96d438df47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -1054,8 +1054,11 @@ class GoGenerator : public BaseGenerator {
const std::string offset = field_var + "Offset";
if (IsString(field.value.type)) {
code +=
"\t" + offset + " := builder.CreateString(t." + field_field + ")\n";
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 if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_UCHAR &&
field.value.type.enum_def == nullptr) {

View File

@ -74,7 +74,10 @@ type MonsterT struct {
func (t *MonsterT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
nameOffset := builder.CreateString(t.Name)
nameOffset := flatbuffers.UOffsetT(0)
if t.Name != "" {
nameOffset = builder.CreateString(t.Name)
}
inventoryOffset := flatbuffers.UOffsetT(0)
if t.Inventory != nil {
inventoryOffset = builder.CreateByteString(t.Inventory)

View File

@ -14,7 +14,10 @@ type StatT struct {
func (t *StatT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
idOffset := builder.CreateString(t.Id)
idOffset := flatbuffers.UOffsetT(0)
if t.Id != "" {
idOffset = builder.CreateString(t.Id)
}
StatStart(builder)
StatAddId(builder, idOffset)
StatAddVal(builder, t.Val)