Implement mutators for Go
authorMuhammed Thanish <mnmtanish@gmail.com>
Wed, 20 Jul 2016 17:28:22 +0000 (22:58 +0530)
committerMuhammed Thanish <mnmtanish@gmail.com>
Wed, 20 Jul 2016 17:28:22 +0000 (22:58 +0530)
docs/source/GoUsage.md
go/table.go
src/idl_gen_go.cpp
tests/MyGame/Example/Monster.go
tests/MyGame/Example/Stat.go
tests/MyGame/Example/Test.go
tests/MyGame/Example/TestSimpleTableWithEnum.go
tests/MyGame/Example/Vec3.go
tests/go_test.go

index a3a0e92..ab6ddbd 100644 (file)
@@ -67,6 +67,29 @@ Now you can access values like this:
     pos := monster.Pos(nil)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+
+In some cases it's necessary to modify values in an existing FlatBuffer in place (without creating a copy). For this reason, scalar fields of a Flatbuffer table or struct can be mutated.
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.go}
+    monster := example.GetRootAsMonster(buf, 0)
+
+    // Set table field.
+    if ok := monster.MutateHp(10); !ok {
+      panic("failed to mutate Hp")
+    }
+
+    // Set struct field.
+    monster.Pos().MutateZ(4)
+
+    // This mutation will fail because the mana field is not available in
+    // the buffer. It should be set when creating the buffer.
+    if ok := monster.MutateMana(20); !ok {
+      panic("failed to mutate Hp")
+    }
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The term `mutate` is used instead of `set` to indicate that this is a special use case. All mutate functions return a boolean value which is false if the field we're trying to mutate is not available in the buffer.
+
 ## Text Parsing
 
 There currently is no support for parsing text (Schema's and JSON) directly
index 976a7db..01d3db1 100644 (file)
@@ -293,3 +293,213 @@ func (t *Table) GetVOffsetTSlot(slot VOffsetT, d VOffsetT) VOffsetT {
        }
        return VOffsetT(off)
 }
+
+// MutateBool updates a bool at the given offset.
+func (t *Table) MutateBool(off UOffsetT, n bool) bool {
+       WriteBool(t.Bytes[off:], n)
+       return true
+}
+
+// MutateByte updates a Byte at the given offset.
+func (t *Table) MutateByte(off UOffsetT, n byte) bool {
+       WriteByte(t.Bytes[off:], n)
+       return true
+}
+
+// MutateUint8 updates a Uint8 at the given offset.
+func (t *Table) MutateUint8(off UOffsetT, n uint8) bool {
+       WriteUint8(t.Bytes[off:], n)
+       return true
+}
+
+// MutateUint16 updates a Uint16 at the given offset.
+func (t *Table) MutateUint16(off UOffsetT, n uint16) bool {
+       WriteUint16(t.Bytes[off:], n)
+       return true
+}
+
+// MutateUint32 updates a Uint32 at the given offset.
+func (t *Table) MutateUint32(off UOffsetT, n uint32) bool {
+       WriteUint32(t.Bytes[off:], n)
+       return true
+}
+
+// MutateUint64 updates a Uint64 at the given offset.
+func (t *Table) MutateUint64(off UOffsetT, n uint64) bool {
+       WriteUint64(t.Bytes[off:], n)
+       return true
+}
+
+// MutateInt8 updates a Int8 at the given offset.
+func (t *Table) MutateInt8(off UOffsetT, n int8) bool {
+       WriteInt8(t.Bytes[off:], n)
+       return true
+}
+
+// MutateInt16 updates a Int16 at the given offset.
+func (t *Table) MutateInt16(off UOffsetT, n int16) bool {
+       WriteInt16(t.Bytes[off:], n)
+       return true
+}
+
+// MutateInt32 updates a Int32 at the given offset.
+func (t *Table) MutateInt32(off UOffsetT, n int32) bool {
+       WriteInt32(t.Bytes[off:], n)
+       return true
+}
+
+// MutateInt64 updates a Int64 at the given offset.
+func (t *Table) MutateInt64(off UOffsetT, n int64) bool {
+       WriteInt64(t.Bytes[off:], n)
+       return true
+}
+
+// MutateFloat32 updates a Float32 at the given offset.
+func (t *Table) MutateFloat32(off UOffsetT, n float32) bool {
+       WriteFloat32(t.Bytes[off:], n)
+       return true
+}
+
+// MutateFloat64 updates a Float64 at the given offset.
+func (t *Table) MutateFloat64(off UOffsetT, n float64) bool {
+       WriteFloat64(t.Bytes[off:], n)
+       return true
+}
+
+// MutateUOffsetT updates a UOffsetT at the given offset.
+func (t *Table) MutateUOffsetT(off UOffsetT, n UOffsetT) bool {
+       WriteUOffsetT(t.Bytes[off:], n)
+       return true
+}
+
+// MutateVOffsetT updates a VOffsetT at the given offset.
+func (t *Table) MutateVOffsetT(off UOffsetT, n VOffsetT) bool {
+       WriteVOffsetT(t.Bytes[off:], n)
+       return true
+}
+
+// MutateSOffsetT updates a SOffsetT at the given offset.
+func (t *Table) MutateSOffsetT(off UOffsetT, n SOffsetT) bool {
+       WriteSOffsetT(t.Bytes[off:], n)
+       return true
+}
+
+// MutateBoolSlot updates the bool at given vtable location
+func (t *Table) MutateBoolSlot(slot VOffsetT, n bool) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateBool(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateByteSlot updates the byte at given vtable location
+func (t *Table) MutateByteSlot(slot VOffsetT, n byte) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateByte(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateInt8Slot updates the int8 at given vtable location
+func (t *Table) MutateInt8Slot(slot VOffsetT, n int8) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateInt8(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateUint8Slot updates the uint8 at given vtable location
+func (t *Table) MutateUint8Slot(slot VOffsetT, n uint8) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateUint8(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateInt16Slot updates the int16 at given vtable location
+func (t *Table) MutateInt16Slot(slot VOffsetT, n int16) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateInt16(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateUint16Slot updates the uint16 at given vtable location
+func (t *Table) MutateUint16Slot(slot VOffsetT, n uint16) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateUint16(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateInt32Slot updates the int32 at given vtable location
+func (t *Table) MutateInt32Slot(slot VOffsetT, n int32) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateInt32(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateUint32Slot updates the uint32 at given vtable location
+func (t *Table) MutateUint32Slot(slot VOffsetT, n uint32) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateUint32(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateInt64Slot updates the int64 at given vtable location
+func (t *Table) MutateInt64Slot(slot VOffsetT, n int64) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateInt64(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateUint64Slot updates the uint64 at given vtable location
+func (t *Table) MutateUint64Slot(slot VOffsetT, n uint64) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateUint64(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateFloat32Slot updates the float32 at given vtable location
+func (t *Table) MutateFloat32Slot(slot VOffsetT, n float32) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateFloat32(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
+
+// MutateFloat64Slot updates the float64 at given vtable location
+func (t *Table) MutateFloat64Slot(slot VOffsetT, n float64) bool {
+       if off := t.Offset(slot); off != 0 {
+               t.MutateFloat64(t.Pos+UOffsetT(off), n)
+               return true
+       }
+
+       return false
+}
index a04b6ba..0920b77 100644 (file)
@@ -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
-
index 0c42f45..f4cd18c 100644 (file)
@@ -43,6 +43,10 @@ func (rcv *Monster) Mana() int16 {
        return 150
 }
 
+func (rcv *Monster) MutateMana(n int16) bool {
+       return rcv._tab.MutateInt16Slot(6, n)
+}
+
 func (rcv *Monster) Hp() int16 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
        if o != 0 {
@@ -51,6 +55,10 @@ func (rcv *Monster) Hp() int16 {
        return 100
 }
 
+func (rcv *Monster) MutateHp(n int16) bool {
+       return rcv._tab.MutateInt16Slot(8, n)
+}
+
 func (rcv *Monster) Name() []byte {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(10))
        if o != 0 {
@@ -92,6 +100,10 @@ func (rcv *Monster) Color() int8 {
        return 8
 }
 
+func (rcv *Monster) MutateColor(n int8) bool {
+       return rcv._tab.MutateInt8Slot(16, n)
+}
+
 func (rcv *Monster) TestType() byte {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(18))
        if o != 0 {
@@ -100,6 +112,10 @@ func (rcv *Monster) TestType() byte {
        return 0
 }
 
+func (rcv *Monster) MutateTestType(n byte) bool {
+       return rcv._tab.MutateByteSlot(18, n)
+}
+
 func (rcv *Monster) Test(obj *flatbuffers.Table) bool {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(20))
        if o != 0 {
@@ -173,6 +189,8 @@ func (rcv *Monster) TestarrayoftablesLength() int {
        return 0
 }
 
+/// an example documentation comment: this will end up in the generated code
+/// multiline too
 func (rcv *Monster) Enemy(obj *Monster) *Monster {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(28))
        if o != 0 {
@@ -232,6 +250,10 @@ func (rcv *Monster) Testbool() byte {
        return 0
 }
 
+func (rcv *Monster) MutateTestbool(n byte) bool {
+       return rcv._tab.MutateByteSlot(34, n)
+}
+
 func (rcv *Monster) Testhashs32Fnv1() int32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(36))
        if o != 0 {
@@ -240,6 +262,10 @@ func (rcv *Monster) Testhashs32Fnv1() int32 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashs32Fnv1(n int32) bool {
+       return rcv._tab.MutateInt32Slot(36, n)
+}
+
 func (rcv *Monster) Testhashu32Fnv1() uint32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(38))
        if o != 0 {
@@ -248,6 +274,10 @@ func (rcv *Monster) Testhashu32Fnv1() uint32 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashu32Fnv1(n uint32) bool {
+       return rcv._tab.MutateUint32Slot(38, n)
+}
+
 func (rcv *Monster) Testhashs64Fnv1() int64 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(40))
        if o != 0 {
@@ -256,6 +286,10 @@ func (rcv *Monster) Testhashs64Fnv1() int64 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashs64Fnv1(n int64) bool {
+       return rcv._tab.MutateInt64Slot(40, n)
+}
+
 func (rcv *Monster) Testhashu64Fnv1() uint64 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(42))
        if o != 0 {
@@ -264,6 +298,10 @@ func (rcv *Monster) Testhashu64Fnv1() uint64 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashu64Fnv1(n uint64) bool {
+       return rcv._tab.MutateUint64Slot(42, n)
+}
+
 func (rcv *Monster) Testhashs32Fnv1a() int32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(44))
        if o != 0 {
@@ -272,6 +310,10 @@ func (rcv *Monster) Testhashs32Fnv1a() int32 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashs32Fnv1a(n int32) bool {
+       return rcv._tab.MutateInt32Slot(44, n)
+}
+
 func (rcv *Monster) Testhashu32Fnv1a() uint32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(46))
        if o != 0 {
@@ -280,6 +322,10 @@ func (rcv *Monster) Testhashu32Fnv1a() uint32 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashu32Fnv1a(n uint32) bool {
+       return rcv._tab.MutateUint32Slot(46, n)
+}
+
 func (rcv *Monster) Testhashs64Fnv1a() int64 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(48))
        if o != 0 {
@@ -288,6 +334,10 @@ func (rcv *Monster) Testhashs64Fnv1a() int64 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashs64Fnv1a(n int64) bool {
+       return rcv._tab.MutateInt64Slot(48, n)
+}
+
 func (rcv *Monster) Testhashu64Fnv1a() uint64 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(50))
        if o != 0 {
@@ -296,6 +346,10 @@ func (rcv *Monster) Testhashu64Fnv1a() uint64 {
        return 0
 }
 
+func (rcv *Monster) MutateTesthashu64Fnv1a(n uint64) bool {
+       return rcv._tab.MutateUint64Slot(50, n)
+}
+
 func (rcv *Monster) Testarrayofbools(j int) byte {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(52))
        if o != 0 {
@@ -321,6 +375,10 @@ func (rcv *Monster) Testf() float32 {
        return 3.14159
 }
 
+func (rcv *Monster) MutateTestf(n float32) bool {
+       return rcv._tab.MutateFloat32Slot(54, n)
+}
+
 func (rcv *Monster) Testf2() float32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(56))
        if o != 0 {
@@ -329,6 +387,10 @@ func (rcv *Monster) Testf2() float32 {
        return 3.0
 }
 
+func (rcv *Monster) MutateTestf2(n float32) bool {
+       return rcv._tab.MutateFloat32Slot(56, n)
+}
+
 func (rcv *Monster) Testf3() float32 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(58))
        if o != 0 {
@@ -337,6 +399,10 @@ func (rcv *Monster) Testf3() float32 {
        return 0.0
 }
 
+func (rcv *Monster) MutateTestf3(n float32) bool {
+       return rcv._tab.MutateFloat32Slot(58, n)
+}
+
 func (rcv *Monster) Testarrayofstring2(j int) []byte {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(60))
        if o != 0 {
index 7c33716..4ccca34 100644 (file)
@@ -30,6 +30,10 @@ func (rcv *Stat) Val() int64 {
        return 0
 }
 
+func (rcv *Stat) MutateVal(n int64) bool {
+       return rcv._tab.MutateInt64Slot(6, n)
+}
+
 func (rcv *Stat) Count() uint16 {
        o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
        if o != 0 {
@@ -38,6 +42,10 @@ func (rcv *Stat) Count() uint16 {
        return 0
 }
 
+func (rcv *Stat) MutateCount(n uint16) bool {
+       return rcv._tab.MutateUint16Slot(8, n)
+}
+
 func StatStart(builder *flatbuffers.Builder) { builder.StartObject(3) }
 func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) }
 func StatAddVal(builder *flatbuffers.Builder, val int64) { builder.PrependInt64Slot(1, val, 0) }
index e849488..ee0d9aa 100644 (file)
@@ -15,7 +15,11 @@ func (rcv *Test) Init(buf []byte, i flatbuffers.UOffsetT) {
 }
 
 func (rcv *Test) A() int16 { return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
+func (rcv *Test) MutateA(n int16) bool { return rcv._tab.MutateInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) }
+
 func (rcv *Test) B() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) }
+func (rcv *Test) MutateB(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2), n) }
+
 
 func CreateTest(builder *flatbuffers.Builder, a int16, b int8) flatbuffers.UOffsetT {
     builder.Prep(2, 4)
index fe9cf2d..d59fa6b 100644 (file)
@@ -22,6 +22,10 @@ func (rcv *TestSimpleTableWithEnum) Color() int8 {
        return 2
 }
 
+func (rcv *TestSimpleTableWithEnum) MutateColor(n int8) bool {
+       return rcv._tab.MutateInt8Slot(4, n)
+}
+
 func TestSimpleTableWithEnumStart(builder *flatbuffers.Builder) { builder.StartObject(1) }
 func TestSimpleTableWithEnumAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(0, color, 2) }
 func TestSimpleTableWithEnumEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
index 082945e..8bf97da 100644 (file)
@@ -15,10 +15,20 @@ func (rcv *Vec3) Init(buf []byte, i flatbuffers.UOffsetT) {
 }
 
 func (rcv *Vec3) X() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
+func (rcv *Vec3) MutateX(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) }
+
 func (rcv *Vec3) Y() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) }
+func (rcv *Vec3) MutateY(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4), n) }
+
 func (rcv *Vec3) Z() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) }
+func (rcv *Vec3) MutateZ(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8), n) }
+
 func (rcv *Vec3) Test1() float64 { return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) }
+func (rcv *Vec3) MutateTest1(n float64) bool { return rcv._tab.MutateFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16), n) }
+
 func (rcv *Vec3) Test2() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) }
+func (rcv *Vec3) MutateTest2(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24), n) }
+
 func (rcv *Vec3) Test3(obj *Test) *Test {
        if obj == nil {
                obj = new(Test)
index 9d94277..b816eb2 100644 (file)
@@ -67,6 +67,7 @@ func TestAll(t *testing.T) {
        // Verify that the Go FlatBuffers runtime library generates the
        // expected bytes (does not use any schema):
        CheckByteLayout(t.Fatalf)
+       CheckMutateMethods(t.Fatalf)
 
        // Verify that panics are raised during exceptional conditions:
        CheckNotInObjectError(t.Fatalf)
@@ -82,6 +83,7 @@ func TestAll(t *testing.T) {
        // Verify that the buffer generated by Go code is readable by the
        // generated Go code:
        CheckReadBuffer(generated, off, t.Fatalf)
+       CheckMutateBuffer(generated, off, t.Fatalf)
 
        // Verify that the buffer generated by C++ code is readable by the
        // generated Go code:
@@ -90,6 +92,7 @@ func TestAll(t *testing.T) {
                t.Fatal(err)
        }
        CheckReadBuffer(monsterDataCpp, 0, t.Fatalf)
+       CheckMutateBuffer(monsterDataCpp, 0, t.Fatalf)
 
        // Verify that vtables are deduplicated when written:
        CheckVtableDeduplication(t.Fatalf)
@@ -280,6 +283,120 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
        }
 }
 
+// CheckMutateBuffer checks that the given buffer can be mutated correctly
+// as the example Monster. Only available scalar values are mutated.
+func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string, ...interface{})) {
+       // make a copy to mutate
+       buf := make([]byte, len(org))
+       copy(buf, org)
+
+       // load monster data from the buffer
+       monster := example.GetRootAsMonster(buf, offset)
+
+       // test case struct
+       type testcase struct {
+               field  string
+               testfn func() bool
+       }
+
+       testForOriginalValues := []testcase{
+               testcase{"Hp", func() bool { return monster.Hp() == 80 }},
+               testcase{"Mana", func() bool { return monster.Mana() == 150 }},
+               testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(1.0) }},
+               testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(2.0) }},
+               testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(3.0) }},
+               testcase{"Pos.Test1'", func() bool { return monster.Pos(nil).Test1() == float64(3.0) }},
+               testcase{"Pos.Test2'", func() bool { return monster.Pos(nil).Test2() == int8(2) }},
+               testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).A() == int16(5) }},
+               testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).B() == int8(6) }},
+       }
+
+       testMutability := []testcase{
+               testcase{"Hp", func() bool { return monster.MutateHp(70) }},
+               testcase{"Mana", func() bool { return !monster.MutateMana(140) }},
+               testcase{"Pos.X", func() bool { return monster.Pos(nil).MutateX(10.0) }},
+               testcase{"Pos.Y", func() bool { return monster.Pos(nil).MutateY(20.0) }},
+               testcase{"Pos.Z", func() bool { return monster.Pos(nil).MutateZ(30.0) }},
+               testcase{"Pos.Test1", func() bool { return monster.Pos(nil).MutateTest1(30.0) }},
+               testcase{"Pos.Test2", func() bool { return monster.Pos(nil).MutateTest2(20) }},
+               testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).MutateA(50) }},
+               testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).MutateB(60) }},
+       }
+
+       testForMutatedValues := []testcase{
+               testcase{"Hp", func() bool { return monster.Hp() == 70 }},
+               testcase{"Mana", func() bool { return monster.Mana() == 150 }},
+               testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(10.0) }},
+               testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(20.0) }},
+               testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(30.0) }},
+               testcase{"Pos.Test1'", func() bool { return monster.Pos(nil).Test1() == float64(30.0) }},
+               testcase{"Pos.Test2'", func() bool { return monster.Pos(nil).Test2() == int8(20) }},
+               testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).A() == int16(50) }},
+               testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).B() == int8(60) }},
+       }
+
+       // make sure original values are okay
+       for _, t := range testForOriginalValues {
+               if !t.testfn() {
+                       fail("field '" + t.field + "' doesn't have the expected original value")
+               }
+       }
+
+       // try to mutate fields and check mutability
+       for _, t := range testMutability {
+               if !t.testfn() {
+                       fail(FailString("field '"+t.field+"' failed mutability test", true, false))
+               }
+       }
+
+       // test whether values have changed
+       for _, t := range testForMutatedValues {
+               if !t.testfn() {
+                       fail("field '" + t.field + "' doesn't have the expected mutated value")
+               }
+       }
+
+       // make sure the buffer has changed
+       if reflect.DeepEqual(buf, org) {
+               fail("mutate buffer failed")
+       }
+
+       // To make sure the buffer has changed accordingly
+       // Read data from the buffer and verify all fields
+       monster = example.GetRootAsMonster(buf, offset)
+       for _, t := range testForMutatedValues {
+               if !t.testfn() {
+                       fail("field '" + t.field + "' doesn't have the expected mutated value")
+               }
+       }
+
+       // reverting all fields to original values should
+       // re-create the original buffer. Mutate all fields
+       // back to their original values and compare buffers.
+       // This test is done to make sure mutations do not do
+       // any unnecessary changes to the buffer.
+       monster = example.GetRootAsMonster(buf, offset)
+       monster.MutateHp(80)
+       monster.Pos(nil).MutateX(1.0)
+       monster.Pos(nil).MutateY(2.0)
+       monster.Pos(nil).MutateZ(3.0)
+       monster.Pos(nil).MutateTest1(3.0)
+       monster.Pos(nil).MutateTest2(2)
+       monster.Pos(nil).Test3(nil).MutateA(5)
+       monster.Pos(nil).Test3(nil).MutateB(6)
+
+       for _, t := range testForOriginalValues {
+               if !t.testfn() {
+                       fail("field '" + t.field + "' doesn't have the expected original value")
+               }
+       }
+
+       // buffer should have original values
+       if !reflect.DeepEqual(buf, org) {
+               fail("revert changes failed")
+       }
+}
+
 // Low level stress/fuzz test: serialize/deserialize a variety of
 // different kinds of data in different combinations
 func checkFuzz(fuzzFields, fuzzObjects int, fail func(string, ...interface{})) {
@@ -1248,6 +1365,151 @@ func CheckByteEquality(a, b []byte, fail func(string, ...interface{})) {
        }
 }
 
+// CheckMutateMethods checks all mutate methods one by one
+func CheckMutateMethods(fail func(string, ...interface{})) {
+       b := flatbuffers.NewBuilder(0)
+       b.StartObject(15)
+       b.PrependBoolSlot(0, true, false)
+       b.PrependByteSlot(1, 1, 0)
+       b.PrependUint8Slot(2, 2, 0)
+       b.PrependUint16Slot(3, 3, 0)
+       b.PrependUint32Slot(4, 4, 0)
+       b.PrependUint64Slot(5, 5, 0)
+       b.PrependInt8Slot(6, 6, 0)
+       b.PrependInt16Slot(7, 7, 0)
+       b.PrependInt32Slot(8, 8, 0)
+       b.PrependInt64Slot(9, 9, 0)
+       b.PrependFloat32Slot(10, 10, 0)
+       b.PrependFloat64Slot(11, 11, 0)
+
+       b.PrependUOffsetTSlot(12, 12, 0)
+       uoVal := b.Offset() - 12
+
+       b.PrependVOffsetT(13)
+       b.Slot(13)
+
+       b.PrependSOffsetT(14)
+       b.Slot(14)
+       soVal := flatbuffers.SOffsetT(b.Offset() - 14)
+
+       offset := b.EndObject()
+
+       t := &flatbuffers.Table{
+               Bytes: b.Bytes,
+               Pos:   flatbuffers.UOffsetT(len(b.Bytes)) - offset,
+       }
+
+       calcVOffsetT := func(slot int) (vtableOffset flatbuffers.VOffsetT) {
+               return flatbuffers.VOffsetT((flatbuffers.VtableMetadataFields + slot) * flatbuffers.SizeVOffsetT)
+       }
+       calcUOffsetT := func(vtableOffset flatbuffers.VOffsetT) (valueOffset flatbuffers.UOffsetT) {
+               return t.Pos + flatbuffers.UOffsetT(t.Offset(vtableOffset))
+       }
+
+       type testcase struct {
+               field  string
+               testfn func() bool
+       }
+
+       testForOriginalValues := []testcase{
+               testcase{"BoolSlot", func() bool { return t.GetBoolSlot(calcVOffsetT(0), true) == true }},
+               testcase{"ByteSlot", func() bool { return t.GetByteSlot(calcVOffsetT(1), 1) == 1 }},
+               testcase{"Uint8Slot", func() bool { return t.GetUint8Slot(calcVOffsetT(2), 2) == 2 }},
+               testcase{"Uint16Slot", func() bool { return t.GetUint16Slot(calcVOffsetT(3), 3) == 3 }},
+               testcase{"Uint32Slot", func() bool { return t.GetUint32Slot(calcVOffsetT(4), 4) == 4 }},
+               testcase{"Uint64Slot", func() bool { return t.GetUint64Slot(calcVOffsetT(5), 5) == 5 }},
+               testcase{"Int8Slot", func() bool { return t.GetInt8Slot(calcVOffsetT(6), 6) == 6 }},
+               testcase{"Int16Slot", func() bool { return t.GetInt16Slot(calcVOffsetT(7), 7) == 7 }},
+               testcase{"Int32Slot", func() bool { return t.GetInt32Slot(calcVOffsetT(8), 8) == 8 }},
+               testcase{"Int64Slot", func() bool { return t.GetInt64Slot(calcVOffsetT(9), 9) == 9 }},
+               testcase{"Float32Slot", func() bool { return t.GetFloat32Slot(calcVOffsetT(10), 10) == 10 }},
+               testcase{"Float64Slot", func() bool { return t.GetFloat64Slot(calcVOffsetT(11), 11) == 11 }},
+               testcase{"UOffsetTSlot", func() bool { return t.GetUOffsetT(calcUOffsetT(calcVOffsetT(12))) == uoVal }},
+               testcase{"VOffsetTSlot", func() bool { return t.GetVOffsetT(calcUOffsetT(calcVOffsetT(13))) == 13 }},
+               testcase{"SOffsetTSlot", func() bool { return t.GetSOffsetT(calcUOffsetT(calcVOffsetT(14))) == soVal }},
+       }
+
+       testMutability := []testcase{
+               testcase{"BoolSlot", func() bool { return t.MutateBoolSlot(calcVOffsetT(0), false) }},
+               testcase{"ByteSlot", func() bool { return t.MutateByteSlot(calcVOffsetT(1), 2) }},
+               testcase{"Uint8Slot", func() bool { return t.MutateUint8Slot(calcVOffsetT(2), 4) }},
+               testcase{"Uint16Slot", func() bool { return t.MutateUint16Slot(calcVOffsetT(3), 6) }},
+               testcase{"Uint32Slot", func() bool { return t.MutateUint32Slot(calcVOffsetT(4), 8) }},
+               testcase{"Uint64Slot", func() bool { return t.MutateUint64Slot(calcVOffsetT(5), 10) }},
+               testcase{"Int8Slot", func() bool { return t.MutateInt8Slot(calcVOffsetT(6), 12) }},
+               testcase{"Int16Slot", func() bool { return t.MutateInt16Slot(calcVOffsetT(7), 14) }},
+               testcase{"Int32Slot", func() bool { return t.MutateInt32Slot(calcVOffsetT(8), 16) }},
+               testcase{"Int64Slot", func() bool { return t.MutateInt64Slot(calcVOffsetT(9), 18) }},
+               testcase{"Float32Slot", func() bool { return t.MutateFloat32Slot(calcVOffsetT(10), 20) }},
+               testcase{"Float64Slot", func() bool { return t.MutateFloat64Slot(calcVOffsetT(11), 22) }},
+               testcase{"UOffsetTSlot", func() bool { return t.MutateUOffsetT(calcUOffsetT(calcVOffsetT(12)), 24) }},
+               testcase{"VOffsetTSlot", func() bool { return t.MutateVOffsetT(calcUOffsetT(calcVOffsetT(13)), 26) }},
+               testcase{"SOffsetTSlot", func() bool { return t.MutateSOffsetT(calcUOffsetT(calcVOffsetT(14)), 28) }},
+       }
+
+       testMutabilityWithoutSlot := []testcase{
+               testcase{"BoolSlot", func() bool { return t.MutateBoolSlot(calcVOffsetT(16), false) }},
+               testcase{"ByteSlot", func() bool { return t.MutateByteSlot(calcVOffsetT(16), 2) }},
+               testcase{"Uint8Slot", func() bool { return t.MutateUint8Slot(calcVOffsetT(16), 2) }},
+               testcase{"Uint16Slot", func() bool { return t.MutateUint16Slot(calcVOffsetT(16), 2) }},
+               testcase{"Uint32Slot", func() bool { return t.MutateUint32Slot(calcVOffsetT(16), 2) }},
+               testcase{"Uint64Slot", func() bool { return t.MutateUint64Slot(calcVOffsetT(16), 2) }},
+               testcase{"Int8Slot", func() bool { return t.MutateInt8Slot(calcVOffsetT(16), 2) }},
+               testcase{"Int16Slot", func() bool { return t.MutateInt16Slot(calcVOffsetT(16), 2) }},
+               testcase{"Int32Slot", func() bool { return t.MutateInt32Slot(calcVOffsetT(16), 2) }},
+               testcase{"Int64Slot", func() bool { return t.MutateInt64Slot(calcVOffsetT(16), 2) }},
+               testcase{"Float32Slot", func() bool { return t.MutateFloat32Slot(calcVOffsetT(16), 2) }},
+               testcase{"Float64Slot", func() bool { return t.MutateFloat64Slot(calcVOffsetT(16), 2) }},
+       }
+
+       testForMutatedValues := []testcase{
+               testcase{"BoolSlot", func() bool { return t.GetBoolSlot(calcVOffsetT(0), true) == false }},
+               testcase{"ByteSlot", func() bool { return t.GetByteSlot(calcVOffsetT(1), 1) == 2 }},
+               testcase{"Uint8Slot", func() bool { return t.GetUint8Slot(calcVOffsetT(2), 1) == 4 }},
+               testcase{"Uint16Slot", func() bool { return t.GetUint16Slot(calcVOffsetT(3), 1) == 6 }},
+               testcase{"Uint32Slot", func() bool { return t.GetUint32Slot(calcVOffsetT(4), 1) == 8 }},
+               testcase{"Uint64Slot", func() bool { return t.GetUint64Slot(calcVOffsetT(5), 1) == 10 }},
+               testcase{"Int8Slot", func() bool { return t.GetInt8Slot(calcVOffsetT(6), 1) == 12 }},
+               testcase{"Int16Slot", func() bool { return t.GetInt16Slot(calcVOffsetT(7), 1) == 14 }},
+               testcase{"Int32Slot", func() bool { return t.GetInt32Slot(calcVOffsetT(8), 1) == 16 }},
+               testcase{"Int64Slot", func() bool { return t.GetInt64Slot(calcVOffsetT(9), 1) == 18 }},
+               testcase{"Float32Slot", func() bool { return t.GetFloat32Slot(calcVOffsetT(10), 1) == 20 }},
+               testcase{"Float64Slot", func() bool { return t.GetFloat64Slot(calcVOffsetT(11), 1) == 22 }},
+               testcase{"UOffsetTSlot", func() bool { return t.GetUOffsetT(calcUOffsetT(calcVOffsetT(12))) == 24 }},
+               testcase{"VOffsetTSlot", func() bool { return t.GetVOffsetT(calcUOffsetT(calcVOffsetT(13))) == 26 }},
+               testcase{"SOffsetTSlot", func() bool { return t.GetSOffsetT(calcUOffsetT(calcVOffsetT(14))) == 28 }},
+       }
+
+       // make sure original values are okay
+       for _, t := range testForOriginalValues {
+               if !t.testfn() {
+                       fail(t.field + "' field doesn't have the expected original value")
+               }
+       }
+
+       // try to mutate fields and check mutability
+       for _, t := range testMutability {
+               if !t.testfn() {
+                       fail(FailString(t.field+"' field failed mutability test", "passed", "failed"))
+               }
+       }
+
+       // try to mutate fields and check mutability
+       // these have wrong slots so should fail
+       for _, t := range testMutabilityWithoutSlot {
+               if t.testfn() {
+                       fail(FailString(t.field+"' field failed no slot mutability test", "failed", "passed"))
+               }
+       }
+
+       // test whether values have changed
+       for _, t := range testForMutatedValues {
+               if !t.testfn() {
+                       fail(t.field + "' field doesn't have the expected mutated value")
+               }
+       }
+}
+
 // BenchmarkVtableDeduplication measures the speed of vtable deduplication
 // by creating prePop vtables, then populating b.N objects with a
 // different single vtable.