Search code examples
flatbuffers

Flatbuffers: table bool value set with byte instead of bool


My table, when compiled for go, ends up using GetByte and PrependByteSlot instead of the Bool alternative (GetBool, PrependBoolSlot).

Is there anything I can do to change that? If not, where can I find confirmation that the byte will be equal to 0 or 1 (false/true)?

Here's a table showing this exact behaviour:

table BooleanContent {
  value:bool = false;
}

Here's the generated file:

// Code generated by the FlatBuffers compiler. DO NOT EDIT.

package GatewayProtocol

import (
    flatbuffers "github.com/google/flatbuffers/go"
)

type BooleanContent struct {
    _tab flatbuffers.Table
}

func GetRootAsBooleanContent(buf []byte, offset flatbuffers.UOffsetT) *BooleanContent {
    n := flatbuffers.GetUOffsetT(buf[offset:])
    x := &BooleanContent{}
    x.Init(buf, n+offset)
    return x
}

func (rcv *BooleanContent) Init(buf []byte, i flatbuffers.UOffsetT) {
    rcv._tab.Bytes = buf
    rcv._tab.Pos = i
}

func (rcv *BooleanContent) Table() flatbuffers.Table {
    return rcv._tab
}

func (rcv *BooleanContent) Value() byte {
    o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
    if o != 0 {
        return rcv._tab.GetByte(o + rcv._tab.Pos)
    }
    return 0
}

func (rcv *BooleanContent) MutateValue(n byte) bool {
    return rcv._tab.MutateByteSlot(4, n)
}

func BooleanContentStart(builder *flatbuffers.Builder) {
    builder.StartObject(1)
}
func BooleanContentAddValue(builder *flatbuffers.Builder, value byte) {
    builder.PrependByteSlot(0, value, 0)
}
func BooleanContentEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
    return builder.EndObject()
}


Solution

  • Booleans are certainly stored as a byte in the FlatBuffers binary format, but there is no reason it can't convert it to boolean upon access, as happens in other languages.

    So this may well be a bug in Go code generator, I'd file an issue on github, tagged by [Go].