From bfa430309af520a8978331754d7fa24ae79e5b44 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 5 Mar 2019 21:16:26 -0800 Subject: [Go] Add mutation functions for vector elements (#5221) Fixes #5206 --- src/idl_gen_go.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 4a402936..7a68e8aa 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -620,6 +620,27 @@ class GoGenerator : public BaseGenerator { code += "}\n\n"; } + // Mutate an element of a vector of scalars. + void MutateElementOfVectorOfNonStruct(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + auto vectortype = field.value.type.VectorType(); + std::string type = MakeCamel(GenTypeBasic(vectortype)); + std::string setter = "rcv._tab.Mutate" + type; + GenReceiver(struct_def, code_ptr); + code += " Mutate" + MakeCamel(field.name); + code += "(j int, n " + TypeName(field) + ") bool "; + code += OffsetPrefix(field); + code += "\t\ta := rcv._tab.Vector(o)\n"; + code += "\t\treturn " + setter + "("; + code += "a+flatbuffers.UOffsetT(j*"; + code += NumToString(InlineSize(vectortype)) + "), n)\n"; + code += "\t}\n"; + code += "\treturn false\n"; + code += "}\n\n"; + } + // Generate a struct field setter, conditioned on its child type(s). void GenStructMutator(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { @@ -630,6 +651,10 @@ class GoGenerator : public BaseGenerator { } else { MutateScalarFieldOfTable(struct_def, field, code_ptr); } + } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + if (IsScalar(field.value.type.element)) { + MutateElementOfVectorOfNonStruct(struct_def, field, code_ptr); + } } } -- cgit v1.2.3