Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ if(FLATBUFFERS_BUILD_TESTS)
enable_testing()

add_test(NAME flattests COMMAND flattests)
# Add CMake tests defined in the `tests` directory (python/unit tests).
add_subdirectory(tests)
if(FLATBUFFERS_BUILD_CPP17)
add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
endif()
Expand Down
70 changes: 70 additions & 0 deletions goldens/lua/MyGame/Example/NamespaceTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--[[ MyGame.Example.NamespaceTest

Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 25.9.23

Declared by : //namespace_first.fbs
Rooting type : MyGame.Example.NamespaceTest (//namespace_first.fbs)

--]]

local flatbuffers = require('flatbuffers')

local NamespaceTest = {}
local mt = {}

function NamespaceTest.New()
local o = {}
setmetatable(o, {__index = mt})
return o
end

function NamespaceTest.GetRootAsNamespaceTest(buf, offset)
if type(buf) == "string" then
buf = flatbuffers.binaryArray.New(buf)
end

local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
local o = NamespaceTest.New()
o:Init(buf, n + offset)
return o
end

function mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end

function mt:Name()
local o = self.view:Offset(4)
if o ~= 0 then
return self.view:String(self.view.pos + o)
end
end

function mt:Value()
local o = self.view:Offset(6)
if o ~= 0 then
return self.view:Get(flatbuffers.N.Int32, self.view.pos + o)
end
return 0
end

function NamespaceTest.Start(builder)
builder:StartObject(2)
end

function NamespaceTest.AddName(builder, name)
builder:PrependUOffsetTRelativeSlot(0, name, 0)
end

function NamespaceTest.AddValue(builder, value)
builder:PrependInt32Slot(1, value, 0)
end

function NamespaceTest.End(builder)
return builder:EndObject()
end

return NamespaceTest
48 changes: 48 additions & 0 deletions goldens/lua/flatbuffers/goldens/Galaxy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--[[ flatbuffers.goldens.Galaxy

Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 25.9.23

Declared by : //basic.fbs
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)

--]]

local flatbuffers = require('flatbuffers')

local Galaxy = {}
local mt = {}

function Galaxy.New()
local o = {}
setmetatable(o, {__index = mt})
return o
end

function mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end

function mt:NumStars()
local o = self.view:Offset(4)
if o ~= 0 then
return self.view:Get(flatbuffers.N.Int64, self.view.pos + o)
end
return 0
end

function Galaxy.Start(builder)
builder:StartObject(1)
end

function Galaxy.AddNumStars(builder, numStars)
builder:PrependInt64Slot(0, numStars, 0)
end

function Galaxy.End(builder)
return builder:EndObject()
end

return Galaxy
88 changes: 88 additions & 0 deletions goldens/lua/flatbuffers/goldens/Universe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--[[ flatbuffers.goldens.Universe

Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 25.9.23

Declared by : //basic.fbs
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)

--]]

local __flatbuffers_goldens_Galaxy = require('flatbuffers.goldens.Galaxy')
local flatbuffers = require('flatbuffers')

local Universe = {}
local mt = {}

function Universe.New()
local o = {}
setmetatable(o, {__index = mt})
return o
end

function Universe.GetRootAsUniverse(buf, offset)
if type(buf) == "string" then
buf = flatbuffers.binaryArray.New(buf)
end

local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
local o = Universe.New()
o:Init(buf, n + offset)
return o
end

function mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end

function mt:Age()
local o = self.view:Offset(4)
if o ~= 0 then
return self.view:Get(flatbuffers.N.Float64, self.view.pos + o)
end
return 0.0
end

function mt:Galaxies(j)
local o = self.view:Offset(6)
if o ~= 0 then
local x = self.view:Vector(o)
x = x + ((j-1) * 4)
x = self.view:Indirect(x)
local obj = __flatbuffers_goldens_Galaxy.New()
obj:Init(self.view.bytes, x)
return obj
end
end

function mt:GalaxiesLength()
local o = self.view:Offset(6)
if o ~= 0 then
return self.view:VectorLen(o)
end
return 0
end

function Universe.Start(builder)
builder:StartObject(2)
end

function Universe.AddAge(builder, age)
builder:PrependFloat64Slot(0, age, 0.0)
end

function Universe.AddGalaxies(builder, galaxies)
builder:PrependUOffsetTRelativeSlot(1, galaxies, 0)
end

function Universe.StartGalaxiesVector(builder, numElems)
return builder:StartVector(4, numElems, 4)
end

function Universe.End(builder)
return builder:EndObject()
end

return Universe
2 changes: 2 additions & 0 deletions goldens/lua/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ def flatc(options, schema):

def GenerateLua():
flatc([], "basic.fbs")
# Test schema that starts with namespace declaration
flatc([], "namespace_first.fbs")
26 changes: 26 additions & 0 deletions goldens/nim/flatbuffers/goldens/Galaxy.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[ flatbuffers.goldens.Galaxy
Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 25.9.23

Declared by : //basic.fbs
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
]#

import flatbuffers

type Galaxy* = object of FlatObj
func numStars*(self: Galaxy): int64 =
let o = self.tab.Offset(4)
if o != 0:
return Get[int64](self.tab, self.tab.Pos + o)
return 0
func `numStars=`*(self: var Galaxy, n: int64): bool =
return self.tab.MutateSlot(4, n)
proc GalaxyStart*(builder: var Builder) =
builder.StartObject(1)
proc GalaxyAddnumStars*(builder: var Builder, numStars: int64) =
builder.PrependSlot(0, numStars, default(int64))
proc GalaxyEnd*(builder: var Builder): uoffset =
return builder.EndObject()
46 changes: 46 additions & 0 deletions goldens/nim/flatbuffers/goldens/Universe.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#[ flatbuffers.goldens.Universe
Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 25.9.23

Declared by : //basic.fbs
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
]#

import Galaxy as flatbuffers_goldens_Galaxy
import flatbuffers
import std/options

type Universe* = object of FlatObj
func age*(self: Universe): float64 =
let o = self.tab.Offset(4)
if o != 0:
return Get[float64](self.tab, self.tab.Pos + o)
return 0.0
func `age=`*(self: var Universe, n: float64): bool =
return self.tab.MutateSlot(4, n)
func galaxiesLength*(self: Universe): int =
let o = self.tab.Offset(6)
if o != 0:
return self.tab.VectorLen(o)
func galaxies*(self: Universe, j: int): flatbuffers_goldens_Galaxy.Galaxy =
let o = self.tab.Offset(6)
if o != 0:
var x = self.tab.Vector(o)
x += j.uoffset * 4.uoffset
return flatbuffers_goldens_Galaxy.Galaxy(tab: Vtable(Bytes: self.tab.Bytes, Pos: x))
func galaxies*(self: Universe): seq[flatbuffers_goldens_Galaxy.Galaxy] =
let len = self.galaxiesLength
for i in countup(0, len - 1):
result.add(self.galaxies(i))
proc UniverseStart*(builder: var Builder) =
builder.StartObject(2)
proc UniverseAddage*(builder: var Builder, age: float64) =
builder.PrependSlot(0, age, default(float64))
proc UniverseAddgalaxies*(builder: var Builder, galaxies: uoffset) =
builder.PrependSlot(1, galaxies, default(uoffset))
proc UniverseStartgalaxiesVector*(builder: var Builder, numElems: uoffset) =
builder.StartVector(4, numElems, 4)
proc UniverseEnd*(builder: var Builder): uoffset =
return builder.EndObject()
8 changes: 8 additions & 0 deletions goldens/schema/namespace_first.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MyGame.Example;

table NamespaceTest {
name:string;
value:int;
}

root_type NamespaceTest;
8 changes: 3 additions & 5 deletions goldens/swift/basic_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Common

import FlatBuffers

public struct flatbuffers_goldens_Galaxy: FlatBufferObject, Verifiable {
public struct flatbuffers_goldens_Galaxy: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable {

static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }
Expand Down Expand Up @@ -43,7 +43,7 @@ public struct flatbuffers_goldens_Galaxy: FlatBufferObject, Verifiable {
}
}

public struct flatbuffers_goldens_Universe: FlatBufferObject, Verifiable {
public struct flatbuffers_goldens_Universe: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable {

static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }
Expand All @@ -60,9 +60,7 @@ public struct flatbuffers_goldens_Universe: FlatBufferObject, Verifiable {
}

public var age: Double { let o = _accessor.offset(VTOFFSET.age.v); return o == 0 ? 0.0 : _accessor.readBuffer(of: Double.self, at: o) }
public var hasGalaxies: Bool { let o = _accessor.offset(VTOFFSET.galaxies.v); return o == 0 ? false : true }
public var galaxiesCount: Int32 { let o = _accessor.offset(VTOFFSET.galaxies.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func galaxies(at index: Int32) -> flatbuffers_goldens_Galaxy? { let o = _accessor.offset(VTOFFSET.galaxies.v); return o == 0 ? nil : flatbuffers_goldens_Galaxy(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
public var galaxies: FlatbufferVector<flatbuffers_goldens_Galaxy> { return _accessor.vector(at: VTOFFSET.galaxies.v, byteSize: 4) }
public static func startUniverse(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 2) }
public static func add(age: Double, _ fbb: inout FlatBufferBuilder) { fbb.add(element: age, def: 0.0, at: VTOFFSET.age.p) }
public static func addVectorOf(galaxies: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: galaxies, at: VTOFFSET.galaxies.p) }
Expand Down
10 changes: 6 additions & 4 deletions src/bfbs_gen_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,12 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
std::replace(path.begin(), path.end(), '.', '/');
}

// TODO(derekbailey): figure out a save file without depending on util.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the old TODO which was here is still relevant

EnsureDirExists(path);
const std::string file_name =
options_.output_path + path + "/" + namer_.File(name);
// Create the full directory under the configured output path. Use
// ConCatPathFileName to safely join path components (it will add the
// trailing separator if needed and strip a leading './').
const std::string full_dir = ConCatPathFileName(options_.output_path, path);
EnsureDirExists(full_dir);
const std::string file_name = ConCatPathFileName(full_dir, namer_.File(name));
SaveFile(file_name.c_str(), code, false);
}

Expand Down
10 changes: 6 additions & 4 deletions src/bfbs_gen_nim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,12 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
std::replace(path.begin(), path.end(), '.', '/');
}

// TODO(derekbailey): figure out a save file without depending on util.h
EnsureDirExists(path);
const std::string file_name =
options_.output_path + path + "/" + namer_.File(name);
// Create the full directory under the configured output path. Use
// ConCatPathFileName to safely join path components (it will add the
// trailing separator if needed and strip a leading './').
const std::string full_dir = ConCatPathFileName(options_.output_path, path);
EnsureDirExists(full_dir);
const std::string file_name = ConCatPathFileName(full_dir, namer_.File(name));
SaveFile(file_name.c_str(), code, false);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test that bfbs generators handle namespace-first schemas correctly
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this change since it integrates with IDEs better. Would you be able to add tests/py_test.py here as well?

find_package(Python3 REQUIRED COMPONENTS Interpreter)

add_test(
NAME test_bfbs_namespace_output
COMMAND ${Python3_EXECUTABLE} -m unittest test_bfbs_namespace_output.py -v
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
Loading
Loading