mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 13:53:38 +00:00
Implement mutators for Go
This commit is contained in:
@@ -439,7 +439,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
||||
code += "func (rcv *" + struct_def.name + ")";
|
||||
}
|
||||
|
||||
// Generate a struct field, conditioned on its child type(s).
|
||||
// Generate a struct field getter, conditioned on its child type(s).
|
||||
static void GenStructAccessor(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
@@ -486,6 +486,48 @@ static void GenStructAccessor(const StructDef &struct_def,
|
||||
}
|
||||
}
|
||||
|
||||
// Mutate the value of a struct's scalar.
|
||||
static void MutateScalarFieldOfStruct(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string type = MakeCamel(GenTypeBasic(field.value.type));
|
||||
std::string setter = "rcv._tab.Mutate" + type;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Mutate" + MakeCamel(field.name);
|
||||
code += "(n " + TypeName(field) + ") bool { return " + setter;
|
||||
code += "(rcv._tab.Pos + flatbuffers.UOffsetT(";
|
||||
code += NumToString(field.value.offset) + "), n) }\n\n";
|
||||
}
|
||||
|
||||
// Mutate the value of a table's scalar.
|
||||
static void MutateScalarFieldOfTable(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
std::string type = MakeCamel(GenTypeBasic(field.value.type));
|
||||
std::string setter = "rcv._tab.Mutate" + type + "Slot";
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Mutate" + MakeCamel(field.name);
|
||||
code += "(n " + TypeName(field) + ") bool {\n\treturn ";
|
||||
code += setter + "(" + NumToString(field.value.offset) + ", n)\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Generate a struct field setter, conditioned on its child type(s).
|
||||
static void GenStructMutator(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
GenComment(field.doc_comment, code_ptr, nullptr, "");
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (struct_def.fixed) {
|
||||
MutateScalarFieldOfStruct(struct_def, field, code_ptr);
|
||||
} else {
|
||||
MutateScalarFieldOfTable(struct_def, field, code_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate table constructors, conditioned on its members' types.
|
||||
static void GenTableBuilders(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
@@ -530,6 +572,7 @@ static void GenStruct(const StructDef &struct_def,
|
||||
if (field.deprecated) continue;
|
||||
|
||||
GenStructAccessor(struct_def, field, code_ptr);
|
||||
GenStructMutator(struct_def, field, code_ptr);
|
||||
}
|
||||
|
||||
if (struct_def.fixed) {
|
||||
@@ -681,4 +724,3 @@ bool GenerateGo(const Parser &parser, const std::string &path,
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
Reference in New Issue
Block a user