Skip to content

Commit 7b8b8f7

Browse files
committed
Fix bfbs Lua and Nim generators to respect -o output path; add tests and goldens
- Ensure bfbs generators (Lua & Nim) create namespace directories under the configured output path using ConCatPathFileName and EnsureDirExists(full_dir). - Add and update to regenerate lua goldens for this schema. - Add Python unit tests verifying Lua and Nim generators place generated files under <out>/My/Game/<Type>.(lua|nim). - Integrate the Python test into CMake (tests/CMakeLists.txt) so CTest runs the new test. Tests: Built and ran CTest locally; both unit tests passed.
1 parent 5998472 commit 7b8b8f7

File tree

13 files changed

+374
-13
lines changed

13 files changed

+374
-13
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ if(FLATBUFFERS_BUILD_TESTS)
688688
enable_testing()
689689

690690
add_test(NAME flattests COMMAND flattests)
691+
# Add CMake tests defined in the `tests` directory (python/unit tests).
692+
add_subdirectory(tests)
691693
if(FLATBUFFERS_BUILD_CPP17)
692694
add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
693695
endif()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--[[ MyGame.Example.NamespaceTest
2+
3+
Automatically generated by the FlatBuffers compiler, do not modify.
4+
Or modify. I'm a message, not a cop.
5+
6+
flatc version: 25.9.23
7+
8+
Declared by : //namespace_first.fbs
9+
Rooting type : MyGame.Example.NamespaceTest (//namespace_first.fbs)
10+
11+
--]]
12+
13+
local flatbuffers = require('flatbuffers')
14+
15+
local NamespaceTest = {}
16+
local mt = {}
17+
18+
function NamespaceTest.New()
19+
local o = {}
20+
setmetatable(o, {__index = mt})
21+
return o
22+
end
23+
24+
function NamespaceTest.GetRootAsNamespaceTest(buf, offset)
25+
if type(buf) == "string" then
26+
buf = flatbuffers.binaryArray.New(buf)
27+
end
28+
29+
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
30+
local o = NamespaceTest.New()
31+
o:Init(buf, n + offset)
32+
return o
33+
end
34+
35+
function mt:Init(buf, pos)
36+
self.view = flatbuffers.view.New(buf, pos)
37+
end
38+
39+
function mt:Name()
40+
local o = self.view:Offset(4)
41+
if o ~= 0 then
42+
return self.view:String(self.view.pos + o)
43+
end
44+
end
45+
46+
function mt:Value()
47+
local o = self.view:Offset(6)
48+
if o ~= 0 then
49+
return self.view:Get(flatbuffers.N.Int32, self.view.pos + o)
50+
end
51+
return 0
52+
end
53+
54+
function NamespaceTest.Start(builder)
55+
builder:StartObject(2)
56+
end
57+
58+
function NamespaceTest.AddName(builder, name)
59+
builder:PrependUOffsetTRelativeSlot(0, name, 0)
60+
end
61+
62+
function NamespaceTest.AddValue(builder, value)
63+
builder:PrependInt32Slot(1, value, 0)
64+
end
65+
66+
function NamespaceTest.End(builder)
67+
return builder:EndObject()
68+
end
69+
70+
return NamespaceTest
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--[[ flatbuffers.goldens.Galaxy
2+
3+
Automatically generated by the FlatBuffers compiler, do not modify.
4+
Or modify. I'm a message, not a cop.
5+
6+
flatc version: 25.9.23
7+
8+
Declared by : //basic.fbs
9+
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
10+
11+
--]]
12+
13+
local flatbuffers = require('flatbuffers')
14+
15+
local Galaxy = {}
16+
local mt = {}
17+
18+
function Galaxy.New()
19+
local o = {}
20+
setmetatable(o, {__index = mt})
21+
return o
22+
end
23+
24+
function mt:Init(buf, pos)
25+
self.view = flatbuffers.view.New(buf, pos)
26+
end
27+
28+
function mt:NumStars()
29+
local o = self.view:Offset(4)
30+
if o ~= 0 then
31+
return self.view:Get(flatbuffers.N.Int64, self.view.pos + o)
32+
end
33+
return 0
34+
end
35+
36+
function Galaxy.Start(builder)
37+
builder:StartObject(1)
38+
end
39+
40+
function Galaxy.AddNumStars(builder, numStars)
41+
builder:PrependInt64Slot(0, numStars, 0)
42+
end
43+
44+
function Galaxy.End(builder)
45+
return builder:EndObject()
46+
end
47+
48+
return Galaxy
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--[[ flatbuffers.goldens.Universe
2+
3+
Automatically generated by the FlatBuffers compiler, do not modify.
4+
Or modify. I'm a message, not a cop.
5+
6+
flatc version: 25.9.23
7+
8+
Declared by : //basic.fbs
9+
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
10+
11+
--]]
12+
13+
local __flatbuffers_goldens_Galaxy = require('flatbuffers.goldens.Galaxy')
14+
local flatbuffers = require('flatbuffers')
15+
16+
local Universe = {}
17+
local mt = {}
18+
19+
function Universe.New()
20+
local o = {}
21+
setmetatable(o, {__index = mt})
22+
return o
23+
end
24+
25+
function Universe.GetRootAsUniverse(buf, offset)
26+
if type(buf) == "string" then
27+
buf = flatbuffers.binaryArray.New(buf)
28+
end
29+
30+
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
31+
local o = Universe.New()
32+
o:Init(buf, n + offset)
33+
return o
34+
end
35+
36+
function mt:Init(buf, pos)
37+
self.view = flatbuffers.view.New(buf, pos)
38+
end
39+
40+
function mt:Age()
41+
local o = self.view:Offset(4)
42+
if o ~= 0 then
43+
return self.view:Get(flatbuffers.N.Float64, self.view.pos + o)
44+
end
45+
return 0.0
46+
end
47+
48+
function mt:Galaxies(j)
49+
local o = self.view:Offset(6)
50+
if o ~= 0 then
51+
local x = self.view:Vector(o)
52+
x = x + ((j-1) * 4)
53+
x = self.view:Indirect(x)
54+
local obj = __flatbuffers_goldens_Galaxy.New()
55+
obj:Init(self.view.bytes, x)
56+
return obj
57+
end
58+
end
59+
60+
function mt:GalaxiesLength()
61+
local o = self.view:Offset(6)
62+
if o ~= 0 then
63+
return self.view:VectorLen(o)
64+
end
65+
return 0
66+
end
67+
68+
function Universe.Start(builder)
69+
builder:StartObject(2)
70+
end
71+
72+
function Universe.AddAge(builder, age)
73+
builder:PrependFloat64Slot(0, age, 0.0)
74+
end
75+
76+
function Universe.AddGalaxies(builder, galaxies)
77+
builder:PrependUOffsetTRelativeSlot(1, galaxies, 0)
78+
end
79+
80+
function Universe.StartGalaxiesVector(builder, numElems)
81+
return builder:StartVector(4, numElems, 4)
82+
end
83+
84+
function Universe.End(builder)
85+
return builder:EndObject()
86+
end
87+
88+
return Universe

goldens/lua/generate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ def flatc(options, schema):
88

99
def GenerateLua():
1010
flatc([], "basic.fbs")
11+
# Test schema that starts with namespace declaration
12+
flatc([], "namespace_first.fbs")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[ flatbuffers.goldens.Galaxy
2+
Automatically generated by the FlatBuffers compiler, do not modify.
3+
Or modify. I'm a message, not a cop.
4+
5+
flatc version: 25.9.23
6+
7+
Declared by : //basic.fbs
8+
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
9+
]#
10+
11+
import flatbuffers
12+
13+
type Galaxy* = object of FlatObj
14+
func numStars*(self: Galaxy): int64 =
15+
let o = self.tab.Offset(4)
16+
if o != 0:
17+
return Get[int64](self.tab, self.tab.Pos + o)
18+
return 0
19+
func `numStars=`*(self: var Galaxy, n: int64): bool =
20+
return self.tab.MutateSlot(4, n)
21+
proc GalaxyStart*(builder: var Builder) =
22+
builder.StartObject(1)
23+
proc GalaxyAddnumStars*(builder: var Builder, numStars: int64) =
24+
builder.PrependSlot(0, numStars, default(int64))
25+
proc GalaxyEnd*(builder: var Builder): uoffset =
26+
return builder.EndObject()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#[ flatbuffers.goldens.Universe
2+
Automatically generated by the FlatBuffers compiler, do not modify.
3+
Or modify. I'm a message, not a cop.
4+
5+
flatc version: 25.9.23
6+
7+
Declared by : //basic.fbs
8+
Rooting type : flatbuffers.goldens.Universe (//basic.fbs)
9+
]#
10+
11+
import Galaxy as flatbuffers_goldens_Galaxy
12+
import flatbuffers
13+
import std/options
14+
15+
type Universe* = object of FlatObj
16+
func age*(self: Universe): float64 =
17+
let o = self.tab.Offset(4)
18+
if o != 0:
19+
return Get[float64](self.tab, self.tab.Pos + o)
20+
return 0.0
21+
func `age=`*(self: var Universe, n: float64): bool =
22+
return self.tab.MutateSlot(4, n)
23+
func galaxiesLength*(self: Universe): int =
24+
let o = self.tab.Offset(6)
25+
if o != 0:
26+
return self.tab.VectorLen(o)
27+
func galaxies*(self: Universe, j: int): flatbuffers_goldens_Galaxy.Galaxy =
28+
let o = self.tab.Offset(6)
29+
if o != 0:
30+
var x = self.tab.Vector(o)
31+
x += j.uoffset * 4.uoffset
32+
return flatbuffers_goldens_Galaxy.Galaxy(tab: Vtable(Bytes: self.tab.Bytes, Pos: x))
33+
func galaxies*(self: Universe): seq[flatbuffers_goldens_Galaxy.Galaxy] =
34+
let len = self.galaxiesLength
35+
for i in countup(0, len - 1):
36+
result.add(self.galaxies(i))
37+
proc UniverseStart*(builder: var Builder) =
38+
builder.StartObject(2)
39+
proc UniverseAddage*(builder: var Builder, age: float64) =
40+
builder.PrependSlot(0, age, default(float64))
41+
proc UniverseAddgalaxies*(builder: var Builder, galaxies: uoffset) =
42+
builder.PrependSlot(1, galaxies, default(uoffset))
43+
proc UniverseStartgalaxiesVector*(builder: var Builder, numElems: uoffset) =
44+
builder.StartVector(4, numElems, 4)
45+
proc UniverseEnd*(builder: var Builder): uoffset =
46+
return builder.EndObject()

goldens/schema/namespace_first.fbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MyGame.Example;
2+
3+
table NamespaceTest {
4+
name:string;
5+
value:int;
6+
}
7+
8+
root_type NamespaceTest;

goldens/swift/basic_generated.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Common
88

99
import FlatBuffers
1010

11-
public struct flatbuffers_goldens_Galaxy: FlatBufferObject, Verifiable {
11+
public struct flatbuffers_goldens_Galaxy: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable {
1212

1313
static func validateVersion() { FlatBuffersVersion_25_9_23() }
1414
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -43,7 +43,7 @@ public struct flatbuffers_goldens_Galaxy: FlatBufferObject, Verifiable {
4343
}
4444
}
4545

46-
public struct flatbuffers_goldens_Universe: FlatBufferObject, Verifiable {
46+
public struct flatbuffers_goldens_Universe: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable {
4747

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

6262
public var age: Double { let o = _accessor.offset(VTOFFSET.age.v); return o == 0 ? 0.0 : _accessor.readBuffer(of: Double.self, at: o) }
63-
public var hasGalaxies: Bool { let o = _accessor.offset(VTOFFSET.galaxies.v); return o == 0 ? false : true }
64-
public var galaxiesCount: Int32 { let o = _accessor.offset(VTOFFSET.galaxies.v); return o == 0 ? 0 : _accessor.vector(count: o) }
65-
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)) }
63+
public var galaxies: FlatbufferVector<flatbuffers_goldens_Galaxy> { return _accessor.vector(at: VTOFFSET.galaxies.v, byteSize: 4) }
6664
public static func startUniverse(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 2) }
6765
public static func add(age: Double, _ fbb: inout FlatBufferBuilder) { fbb.add(element: age, def: 0.0, at: VTOFFSET.age.p) }
6866
public static func addVectorOf(galaxies: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: galaxies, at: VTOFFSET.galaxies.p) }

src/bfbs_gen_lua.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,12 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
694694
std::replace(path.begin(), path.end(), '.', '/');
695695
}
696696

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

0 commit comments

Comments
 (0)