Skip to content

Commit 88d3f21

Browse files
dfa1claude
andcommitted
refactor: strip trailing _ from fbs schema names in generated Java classes
FlatBuffers schemas use Struct_, Bool_, etc. to avoid C++ reserved-name conflicts. That suffix is an artifact of C++ and should not appear in generated Java identifiers. - fbs-gen CodeGen.className(): strip trailing _ before prefixing with Fbs, so table Struct_ -> FbsStruct, table Bool_ -> FbsBool - Rename FbsStruct runtime base (inline struct cursor) to FbsInlineStruct to free the FbsStruct name for the generated class - Regenerate: FbsStruct_.java removed, FbsStruct.java is now the generated class for the Struct_ schema table; FbsType.FbsStruct_ -> FbsType.FbsStruct; builder factory createFbsStruct_() -> createFbsStruct() - Update all callers in reader, writer, and tests - Update CLAUDE.md to document the rule and the FbsInlineStruct rename Copying dtype.fbs verbatim from the upstream spec now works without any further generator changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent db47dcd commit 88d3f21

14 files changed

Lines changed: 259 additions & 255 deletions

File tree

CLAUDE.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ Regenerate after editing `.fbs`/`.proto` (both generators are in-house, no exter
7777
Both schema languages are compiled in-process to MemorySegment-native Java, with no
7878
`flatc`/`protoc` and no `com.google.flatbuffers`/`protobuf-java` runtime (ADR 0017):
7979
- **`.fbs``fbs-gen`** (`io.github.dfa1.vortex.fbsgen`): generates readers extending
80-
`FbsTable`/`FbsStruct` and builders over `FbsBuilder`, all in the same generated package
81-
`io.github.dfa1.vortex.core.fbs`. The runtime base classes `FbsTable`/`FbsStruct` are
82-
package-private (only generated readers extend them); `FbsBuilder` is public because the
83-
writer module assembles FlatBuffers with it.
80+
`FbsTable` (vtable-based) or `FbsInlineStruct` (fixed-offset inline) and builders over
81+
`FbsBuilder`, all in the same generated package `io.github.dfa1.vortex.core.fbs`. The
82+
runtime base classes `FbsTable`/`FbsInlineStruct` are package-private (only generated
83+
readers extend them); `FbsBuilder` is public because the writer module assembles FlatBuffers
84+
with it. Schema names with a trailing `_` (e.g. `Struct_`) have the underscore stripped in
85+
the generated Java class name (`FbsStruct`) — the upstream uses `_` to avoid C++ conflicts,
86+
which should not appear in Java.
8487
- **`.proto``proto-gen`**: one record per message with static `decode(MemorySegment, long,
8588
long)` + `encode()` operating directly on a segment.
8689

core/src/main/java/io/github/dfa1/vortex/core/fbs/FbsBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/// Reader and builder for the `Buffer` FlatBuffers struct (inline, 8 bytes).
99
@Generated("io.github.dfa1.vortex.fbsgen.CodeGen")
10-
public final class FbsBuffer extends FbsStruct {
10+
public final class FbsBuffer extends FbsInlineStruct {
1111

1212
/// Positions this reader at a struct.
1313
/// @param seg the buffer
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.github.dfa1.vortex.core.fbs;
2+
3+
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_DOUBLE;
4+
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_FLOAT;
5+
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_INT;
6+
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_LONG;
7+
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_SHORT;
8+
9+
import java.lang.foreign.MemorySegment;
10+
import java.lang.foreign.ValueLayout;
11+
12+
/// MemorySegment-native base for generated FlatBuffers struct accessors.
13+
///
14+
/// Unlike a table, a FlatBuffers struct is a fixed-size, inline record with no
15+
/// vtable: every field lives at a compile-time-constant byte offset from the
16+
/// struct position, and every field is always present. Generated accessors read
17+
/// directly at `position + fieldOffset`.
18+
///
19+
/// Package-private: only the generated struct accessors in this package extend it.
20+
class FbsInlineStruct {
21+
22+
/// The backing buffer.
23+
protected MemorySegment seg;
24+
25+
/// This struct's byte position within [#seg].
26+
protected long pos;
27+
28+
/// Creates an unpositioned cursor; call [#init(MemorySegment, long)] before use.
29+
protected FbsInlineStruct() {
30+
}
31+
32+
/// Positions this cursor at a struct.
33+
///
34+
/// @param segment backing buffer
35+
/// @param position struct position within the buffer
36+
protected final void init(MemorySegment segment, long position) {
37+
this.seg = segment;
38+
this.pos = position;
39+
}
40+
41+
/// @param fieldOffset constant byte offset of the field within the struct
42+
/// @return the signed byte field
43+
protected final byte readByte(int fieldOffset) {
44+
return seg.get(ValueLayout.JAVA_BYTE, pos + fieldOffset);
45+
}
46+
47+
/// @param fieldOffset constant byte offset of the field within the struct
48+
/// @return the little-endian 16-bit field
49+
protected final short readShort(int fieldOffset) {
50+
return seg.get(LE_SHORT, pos + fieldOffset);
51+
}
52+
53+
/// @param fieldOffset constant byte offset of the field within the struct
54+
/// @return the little-endian 32-bit field
55+
protected final int readInt(int fieldOffset) {
56+
return seg.get(LE_INT, pos + fieldOffset);
57+
}
58+
59+
/// @param fieldOffset constant byte offset of the field within the struct
60+
/// @return the little-endian 64-bit field
61+
protected final long readLong(int fieldOffset) {
62+
return seg.get(LE_LONG, pos + fieldOffset);
63+
}
64+
65+
/// @param fieldOffset constant byte offset of the field within the struct
66+
/// @return the little-endian 32-bit float field
67+
protected final float readFloat(int fieldOffset) {
68+
return seg.get(LE_FLOAT, pos + fieldOffset);
69+
}
70+
71+
/// @param fieldOffset constant byte offset of the field within the struct
72+
/// @return the little-endian 64-bit double field
73+
protected final double readDouble(int fieldOffset) {
74+
return seg.get(LE_DOUBLE, pos + fieldOffset);
75+
}
76+
}

core/src/main/java/io/github/dfa1/vortex/core/fbs/FbsSegmentSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/// Reader and builder for the `SegmentSpec` FlatBuffers struct (inline, 16 bytes).
99
@Generated("io.github.dfa1.vortex.fbsgen.CodeGen")
10-
public final class FbsSegmentSpec extends FbsStruct {
10+
public final class FbsSegmentSpec extends FbsInlineStruct {
1111

1212
/// Positions this reader at a struct.
1313
/// @param seg the buffer
Lines changed: 129 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,152 @@
1-
package io.github.dfa1.vortex.core.fbs;
1+
// Generated by vortex-fbs-gen. Do not edit.
22

3-
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_DOUBLE;
4-
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_FLOAT;
5-
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_INT;
6-
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_LONG;
7-
import static io.github.dfa1.vortex.core.io.VortexFormat.LE_SHORT;
3+
package io.github.dfa1.vortex.core.fbs;
84

95
import java.lang.foreign.MemorySegment;
10-
import java.lang.foreign.ValueLayout;
6+
import javax.annotation.processing.Generated;
7+
8+
/// Reader and builder for the `Struct_` FlatBuffers table.
9+
@Generated("io.github.dfa1.vortex.fbsgen.CodeGen")
10+
public final class FbsStruct extends FbsTable {
11+
12+
/// Positions a reader at the root table of a finished buffer.
13+
/// @param seg the buffer
14+
/// @return a reader positioned at the root
15+
public static FbsStruct getRootAsFbsStruct(MemorySegment seg) {
16+
return new FbsStruct().assign(seg, FbsTable.rootPosition(seg, 0));
17+
}
18+
19+
/// Positions this reader at a table.
20+
/// @param seg the buffer
21+
/// @param position the table position
22+
/// @return this
23+
public FbsStruct assign(MemorySegment seg, long position) {
24+
init(seg, position);
25+
return this;
26+
}
27+
28+
/// @return the length of the `names` vector
29+
public int namesLength() {
30+
int o = fieldOffset(4);
31+
return o != 0 ? vectorLength(o) : 0;
32+
}
33+
34+
/// @param j element index
35+
/// @return the j-th `names` string
36+
public String names(int j) {
37+
int o = fieldOffset(4);
38+
return readStringAt(vectorElements(o) + (long) j * 4);
39+
}
40+
41+
/// @return the length of the `dtypes` vector
42+
public int dtypesLength() {
43+
int o = fieldOffset(6);
44+
return o != 0 ? vectorLength(o) : 0;
45+
}
46+
47+
/// @param j element index
48+
/// @return the j-th `dtypes` element
49+
public FbsDType dtypes(int j) {
50+
int o = fieldOffset(6);
51+
return new FbsDType().assign(seg, indirect(vectorElements(o) + (long) j * 4));
52+
}
1153

12-
/// MemorySegment-native base for generated FlatBuffers struct accessors.
13-
///
14-
/// Unlike a table, a FlatBuffers struct is a fixed-size, inline record with no
15-
/// vtable: every field lives at a compile-time-constant byte offset from the
16-
/// struct position, and every field is always present. Generated accessors read
17-
/// directly at `position + fieldOffset`.
18-
///
19-
/// Package-private: only the generated struct accessors in this package extend it.
20-
class FbsStruct {
54+
/// @return the `nullable` field
55+
public boolean nullable() {
56+
int o = fieldOffset(8);
57+
return o != 0 ? readByte(pos + o) != 0 : false;
58+
}
2159

22-
/// The backing buffer.
23-
protected MemorySegment seg;
60+
/// Sets the `names` offset field.
61+
/// @param b the builder
62+
/// @param offset the referenced offset
63+
public static void addNames(FbsBuilder b, int offset) {
64+
b.addOffset(0, offset, 0);
65+
}
2466

25-
/// This struct's byte position within [#seg].
26-
protected long pos;
67+
/// Creates the `names` offset vector.
68+
/// @param b the builder
69+
/// @param data element offsets
70+
/// @return the vector offset
71+
public static int createNamesVector(FbsBuilder b, int[] data) {
72+
b.startVector(4, data.length, 4);
73+
for (int i = data.length - 1; i >= 0; i--) {
74+
b.addOffset(data[i]);
75+
}
76+
return b.endVector();
77+
}
78+
79+
/// Begins the `names` vector.
80+
/// @param b the builder
81+
/// @param numElems element count
82+
public static void startNamesVector(FbsBuilder b, int numElems) {
83+
b.startVector(4, numElems, 4);
84+
}
2785

28-
/// Creates an unpositioned cursor; call [#init(MemorySegment, long)] before use.
29-
protected FbsStruct() {
86+
/// Sets the `dtypes` offset field.
87+
/// @param b the builder
88+
/// @param offset the referenced offset
89+
public static void addDtypes(FbsBuilder b, int offset) {
90+
b.addOffset(1, offset, 0);
3091
}
3192

32-
/// Positions this cursor at a struct.
33-
///
34-
/// @param segment backing buffer
35-
/// @param position struct position within the buffer
36-
protected final void init(MemorySegment segment, long position) {
37-
this.seg = segment;
38-
this.pos = position;
93+
/// Creates the `dtypes` offset vector.
94+
/// @param b the builder
95+
/// @param data element offsets
96+
/// @return the vector offset
97+
public static int createDtypesVector(FbsBuilder b, int[] data) {
98+
b.startVector(4, data.length, 4);
99+
for (int i = data.length - 1; i >= 0; i--) {
100+
b.addOffset(data[i]);
101+
}
102+
return b.endVector();
39103
}
40104

41-
/// @param fieldOffset constant byte offset of the field within the struct
42-
/// @return the signed byte field
43-
protected final byte readByte(int fieldOffset) {
44-
return seg.get(ValueLayout.JAVA_BYTE, pos + fieldOffset);
105+
/// Begins the `dtypes` vector.
106+
/// @param b the builder
107+
/// @param numElems element count
108+
public static void startDtypesVector(FbsBuilder b, int numElems) {
109+
b.startVector(4, numElems, 4);
45110
}
46111

47-
/// @param fieldOffset constant byte offset of the field within the struct
48-
/// @return the little-endian 16-bit field
49-
protected final short readShort(int fieldOffset) {
50-
return seg.get(LE_SHORT, pos + fieldOffset);
112+
/// Sets the `nullable` field.
113+
/// @param b the builder
114+
/// @param nullable value
115+
public static void addNullable(FbsBuilder b, boolean nullable) {
116+
b.addBoolean(2, nullable, false);
51117
}
52118

53-
/// @param fieldOffset constant byte offset of the field within the struct
54-
/// @return the little-endian 32-bit field
55-
protected final int readInt(int fieldOffset) {
56-
return seg.get(LE_INT, pos + fieldOffset);
119+
/// Builds a `FbsStruct` table.
120+
/// @param b the builder
121+
/// @param namesOffset field value
122+
/// @param dtypesOffset field value
123+
/// @param nullable field value
124+
/// @return the table offset
125+
public static int createFbsStruct(FbsBuilder b, int namesOffset, int dtypesOffset, boolean nullable) {
126+
b.startTable(3);
127+
b.addOffset(1, dtypesOffset, 0);
128+
b.addOffset(0, namesOffset, 0);
129+
b.addBoolean(2, nullable, false);
130+
return b.endTable();
57131
}
58132

59-
/// @param fieldOffset constant byte offset of the field within the struct
60-
/// @return the little-endian 64-bit field
61-
protected final long readLong(int fieldOffset) {
62-
return seg.get(LE_LONG, pos + fieldOffset);
133+
/// Begins a `FbsStruct` table.
134+
/// @param b the builder
135+
public static void startFbsStruct(FbsBuilder b) {
136+
b.startTable(3);
63137
}
64138

65-
/// @param fieldOffset constant byte offset of the field within the struct
66-
/// @return the little-endian 32-bit float field
67-
protected final float readFloat(int fieldOffset) {
68-
return seg.get(LE_FLOAT, pos + fieldOffset);
139+
/// Finishes a `FbsStruct` table.
140+
/// @param b the builder
141+
/// @return the table offset
142+
public static int endFbsStruct(FbsBuilder b) {
143+
return b.endTable();
69144
}
70145

71-
/// @param fieldOffset constant byte offset of the field within the struct
72-
/// @return the little-endian 64-bit double field
73-
protected final double readDouble(int fieldOffset) {
74-
return seg.get(LE_DOUBLE, pos + fieldOffset);
146+
/// Finishes the buffer with a `FbsStruct` root.
147+
/// @param b the builder
148+
/// @param offset the root table offset
149+
public static void finishFbsStructBuffer(FbsBuilder b, int offset) {
150+
b.finish(offset);
75151
}
76152
}

0 commit comments

Comments
 (0)