Skip to content

Instantly share code, notes, and snippets.

@duythinht
Last active March 29, 2018 14:32
Show Gist options
  • Save duythinht/6952732ee51a3a1ee8ad3c48822a3c39 to your computer and use it in GitHub Desktop.
Save duythinht/6952732ee51a3a1ee8ad3c48822a3c39 to your computer and use it in GitHub Desktop.
Flatbuffers for mirage
// automatically generated by the FlatBuffers compiler, do not modify
package buffers
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type Image struct {
_tab flatbuffers.Table
}
func GetRootAsImage(buf []byte, offset flatbuffers.UOffsetT) *Image {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &Image{}
x.Init(buf, n+offset)
return x
}
func (rcv *Image) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *Image) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *Image) Id() []byte {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
return rcv._tab.ByteVector(o + rcv._tab.Pos)
}
return nil
}
func (rcv *Image) Data(j int) byte {
o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
if o != 0 {
a := rcv._tab.Vector(o)
return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1))
}
return 0
}
func (rcv *Image) DataLength() int {
o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
if o != 0 {
return rcv._tab.VectorLen(o)
}
return 0
}
func (rcv *Image) DataBytes() []byte {
o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
if o != 0 {
return rcv._tab.ByteVector(o + rcv._tab.Pos)
}
return nil
}
func ImageStart(builder *flatbuffers.Builder) {
builder.StartObject(2)
}
func ImageAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0)
}
func ImageAddData(builder *flatbuffers.Builder, data flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(data), 0)
}
func ImageStartDataVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
return builder.StartVector(1, numElems, 1)
}
func ImageEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}
namespace buffers;
table Image {
id:string;
data:[ubyte];
}
package main
import (
"fmt"
"github.com/duythinht/play/buffers"
flatbuffers "github.com/google/flatbuffers/go"
)
func MakeImage(id string, data []byte) []byte {
builder := flatbuffers.NewBuilder(0)
builder.Reset()
idOffset := builder.CreateString(id)
dataOffset := builder.CreateByteVector(data)
buffers.ImageStart(builder)
buffers.ImageAddId(builder, idOffset)
buffers.ImageAddData(builder, dataOffset)
image := buffers.ImageEnd(builder)
builder.Finish(image)
return builder.Bytes[builder.Head():]
}
func ReadImage(buf []byte) *buffers.Image {
return buffers.GetRootAsImage(buf, 0)
}
func main() {
buf := MakeImage("hello/123", []byte("this is data data data"))
image := ReadImage(buf)
fmt.Printf("%s\n", image.Id())
fmt.Printf("%s\n", image.Id())
fmt.Printf("%s\n", image.Id())
fmt.Printf("%s\n", image.Id())
fmt.Printf("%s\n", image.Id())
fmt.Printf("%s\n", image.Id())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment