[golang] Add support for text parsing with json struct tags (#7353)

* [golang] Add support for text parsing with json struct tags

Add struct tags to Go native structs generated when object API is used.

This allows to use the same JSON file as for C++ text
parsing(i.e. snake_case vs CamelCase) and thus enabling text parsing
for Go(when using object API).

* [golang] Add test for text parsing

Added small test to check and demonstrate text parsing in Go.
Also, ran clang-format on cpp file changes.
This commit is contained in:
Andrei Burdulescu
2022-08-08 21:30:06 +03:00
committed by GitHub
parent ee2ced236d
commit c793621567
13 changed files with 151 additions and 120 deletions

View File

@@ -17,8 +17,9 @@
package main
import (
mygame "MyGame" // refers to generated code
example "MyGame/Example" // refers to generated code
mygame "MyGame" // refers to generated code
example "MyGame/Example" // refers to generated code
"encoding/json"
optional_scalars "optional_scalars" // refers to generated code
"bytes"
@@ -68,6 +69,35 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
// TestTextParsing test if text parsing works with object API.
func TestTextParsing(t *testing.T) {
expectedMonster := example.MonsterT{
Mana: 42,
Name: "foo",
LongEnumNormalDefault: example.LongEnumLongTwo,
}
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(expectedMonster); err != nil {
t.Fatal(err)
}
var monster example.MonsterT
if err := json.NewDecoder(buf).Decode(&monster); err != nil {
t.Fatal(err)
}
if monster.Mana != expectedMonster.Mana {
t.Fatal("wrong mana:", monster.Mana)
}
if monster.Name != expectedMonster.Name {
t.Fatal("wrong name:", monster.Name)
}
if monster.LongEnumNormalDefault != expectedMonster.LongEnumNormalDefault {
t.Fatal("wrong enum:", monster.LongEnumNormalDefault)
}
}
// TestAll runs all checks, failing if any errors occur.
func TestAll(t *testing.T) {
// Verify that the Go FlatBuffers runtime library generates the