我遇到了从字节改变平面缓冲区的问题。根据 flatbuffer 文档(https://github.com/google/flatbuffers/blob/master/docs/source/Tutorial.md),您可以改变固定大小的字段,例如 int32。如下所示,生成的 golang TestMutate 有一个 MutateServerId() 函数。我的问题是,在我改变它之后,字节似乎没有改变。
这是我的平面缓冲区表定义:
namespace foo;
table TestMutate {
serverId:int32;
}
这是我写的单元测试:
func TestMutateFlatbuffer2(test *testing.T) {
builder := flatbuffers.NewBuilder(1024)
packageWalletStorageServicesRPC.TestMutateStart(builder)
packageWalletStorageServicesRPC.TestMutateAddServerId(builder, 1)
endMessage := packageWalletStorageServicesRPC.TestMutateEnd(builder)
builder.Finish(endMessage)
bytes := builder.FinishedBytes()
testMutate := packageWalletStorageServicesRPC.GetRootAsTestMutate(bytes, 0)
success := testMutate.MutateServerId(2)
if !success {
panic("Server id not mutated.")
} else {
logger.Logf(logger.INFO, "serverId mutated to:%d", testMutate.ServerId())
}
mutatedBytes := testMutate.Table().Bytes
if string(mutatedBytes) == string(bytes) {
panic("Bytes were not mutated.")
}
}
这是测试的输出。
=== RUN TestMutateFlatbuffer2
2019/08/01 19:33:56.801926 foo_test.go:389 : [ I ]: serverId mutated to:2
--- FAIL: TestMutateFlatbuffer2 (0.00s)
panic: Bytes were not mutated. [recovered]
panic: Bytes were not mutated.
请注意,我似乎已经改变了底层结构,但是当我获取平面缓冲区的字节时,它们并没有改变。问题 1:我是否以正确的方式获取字节?问题 2:如果我以正确的方式获取它们,为什么自从对 mutate 的调用似乎成功以来它们没有改变?
繁星coding
四季花海
相关分类