lib.go raw

   1  package flatbuffers
   2  
   3  // FlatBuffer is the interface that represents a flatbuffer.
   4  type FlatBuffer interface {
   5  	Table() Table
   6  	Init(buf []byte, i UOffsetT)
   7  }
   8  
   9  // GetRootAs is a generic helper to initialize a FlatBuffer with the provided buffer bytes and its data offset.
  10  func GetRootAs(buf []byte, offset UOffsetT, fb FlatBuffer) {
  11  	n := GetUOffsetT(buf[offset:])
  12  	fb.Init(buf, n+offset)
  13  }
  14  
  15  // GetSizePrefixedRootAs is a generic helper to initialize a FlatBuffer with the provided size-prefixed buffer
  16  // bytes and its data offset
  17  func GetSizePrefixedRootAs(buf []byte, offset UOffsetT, fb FlatBuffer) {
  18  	n := GetUOffsetT(buf[offset+sizePrefixLength:])
  19  	fb.Init(buf, n+offset+sizePrefixLength)
  20  }
  21  
  22  // GetSizePrefix reads the size from a size-prefixed flatbuffer
  23  func GetSizePrefix(buf []byte, offset UOffsetT) uint32 {
  24  	return GetUint32(buf[offset:])
  25  }
  26  
  27  // GetIndirectOffset retrives the relative offset in the provided buffer stored at `offset`.
  28  func GetIndirectOffset(buf []byte, offset UOffsetT) UOffsetT {
  29  	return offset + GetUOffsetT(buf[offset:])
  30  }
  31  
  32  // GetBufferIdentifier returns the file identifier as string
  33  func GetBufferIdentifier(buf []byte) string {
  34  	return string(buf[SizeUOffsetT:][:fileIdentifierLength])
  35  }
  36  
  37  // GetBufferIdentifier returns the file identifier as string for a size-prefixed buffer
  38  func GetSizePrefixedBufferIdentifier(buf []byte) string {
  39  	return string(buf[SizeUOffsetT+sizePrefixLength:][:fileIdentifierLength])
  40  }
  41  
  42  // BufferHasIdentifier checks if the identifier in a buffer has the expected value
  43  func BufferHasIdentifier(buf []byte, identifier string) bool {
  44  	return GetBufferIdentifier(buf) == identifier
  45  }
  46  
  47  // BufferHasIdentifier checks if the identifier in a buffer has the expected value for a size-prefixed buffer
  48  func SizePrefixedBufferHasIdentifier(buf []byte, identifier string) bool {
  49  	return GetSizePrefixedBufferIdentifier(buf) == identifier
  50  }
  51