mirror of
https://github.com/google/flatbuffers.git
synced 2025-04-07 16:52:29 +08:00
Add goldens directory
This commit is contained in:
parent
ef5ae488dd
commit
3308444147
26
goldens/README.md
Normal file
26
goldens/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# Golden Generated Files
|
||||
|
||||
This directory is a repository for the generated files of `flatc`.
|
||||
|
||||
We check in the generated code so we can see, during a PR review, how the
|
||||
changes affect the generated output. Its also useful as a reference to point too
|
||||
as how things work across various languages.
|
||||
|
||||
These files are **NOT** intended to be depended on by any code, such as tests or
|
||||
or compiled examples.
|
||||
|
||||
## Languages Specifics
|
||||
|
||||
Each language should keep their generated code in their respective directories.
|
||||
However, the parent schemas can, and should, be shared so we have a consistent
|
||||
view of things across languages. These are kept in the `schema/` directory.
|
||||
|
||||
Some languages may not support every generation feature, so each language is
|
||||
required to specify the `flatc` arguments individually.
|
||||
|
||||
* Try to avoid includes and nested directories, preferring it as flat as
|
||||
possible.
|
||||
|
||||
## Updating
|
||||
|
||||
Just run the `generate_goldens.py` script and it should generate them all.
|
157
goldens/cpp/basic_generated.h
Normal file
157
goldens/cpp/basic_generated.h
Normal file
@ -0,0 +1,157 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
#ifndef FLATBUFFERS_GENERATED_BASIC_H_
|
||||
#define FLATBUFFERS_GENERATED_BASIC_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
// Ensure the included flatbuffers.h is the same version as when this file was
|
||||
// generated, otherwise it may not be compatible.
|
||||
static_assert(FLATBUFFERS_VERSION_MAJOR == 23 &&
|
||||
FLATBUFFERS_VERSION_MINOR == 3 &&
|
||||
FLATBUFFERS_VERSION_REVISION == 3,
|
||||
"Non-compatible flatbuffers version included");
|
||||
|
||||
struct Galaxy;
|
||||
struct GalaxyBuilder;
|
||||
|
||||
struct Universe;
|
||||
struct UniverseBuilder;
|
||||
|
||||
struct Galaxy FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef GalaxyBuilder Builder;
|
||||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
|
||||
VT_NUM_STARS = 4
|
||||
};
|
||||
int64_t num_stars() const {
|
||||
return GetField<int64_t>(VT_NUM_STARS, 0);
|
||||
}
|
||||
bool Verify(::flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<int64_t>(verifier, VT_NUM_STARS, 8) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
|
||||
struct GalaxyBuilder {
|
||||
typedef Galaxy Table;
|
||||
::flatbuffers::FlatBufferBuilder &fbb_;
|
||||
::flatbuffers::uoffset_t start_;
|
||||
void add_num_stars(int64_t num_stars) {
|
||||
fbb_.AddElement<int64_t>(Galaxy::VT_NUM_STARS, num_stars, 0);
|
||||
}
|
||||
explicit GalaxyBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
::flatbuffers::Offset<Galaxy> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = ::flatbuffers::Offset<Galaxy>(end);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline ::flatbuffers::Offset<Galaxy> CreateGalaxy(
|
||||
::flatbuffers::FlatBufferBuilder &_fbb,
|
||||
int64_t num_stars = 0) {
|
||||
GalaxyBuilder builder_(_fbb);
|
||||
builder_.add_num_stars(num_stars);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Universe FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef UniverseBuilder Builder;
|
||||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
|
||||
VT_AGE = 4,
|
||||
VT_GALAXIES = 6
|
||||
};
|
||||
double age() const {
|
||||
return GetField<double>(VT_AGE, 0.0);
|
||||
}
|
||||
const ::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>> *galaxies() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>> *>(VT_GALAXIES);
|
||||
}
|
||||
bool Verify(::flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<double>(verifier, VT_AGE, 8) &&
|
||||
VerifyOffset(verifier, VT_GALAXIES) &&
|
||||
verifier.VerifyVector(galaxies()) &&
|
||||
verifier.VerifyVectorOfTables(galaxies()) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
|
||||
struct UniverseBuilder {
|
||||
typedef Universe Table;
|
||||
::flatbuffers::FlatBufferBuilder &fbb_;
|
||||
::flatbuffers::uoffset_t start_;
|
||||
void add_age(double age) {
|
||||
fbb_.AddElement<double>(Universe::VT_AGE, age, 0.0);
|
||||
}
|
||||
void add_galaxies(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>>> galaxies) {
|
||||
fbb_.AddOffset(Universe::VT_GALAXIES, galaxies);
|
||||
}
|
||||
explicit UniverseBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
::flatbuffers::Offset<Universe> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = ::flatbuffers::Offset<Universe>(end);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline ::flatbuffers::Offset<Universe> CreateUniverse(
|
||||
::flatbuffers::FlatBufferBuilder &_fbb,
|
||||
double age = 0.0,
|
||||
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>>> galaxies = 0) {
|
||||
UniverseBuilder builder_(_fbb);
|
||||
builder_.add_age(age);
|
||||
builder_.add_galaxies(galaxies);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline ::flatbuffers::Offset<Universe> CreateUniverseDirect(
|
||||
::flatbuffers::FlatBufferBuilder &_fbb,
|
||||
double age = 0.0,
|
||||
const std::vector<::flatbuffers::Offset<Galaxy>> *galaxies = nullptr) {
|
||||
auto galaxies__ = galaxies ? _fbb.CreateVector<::flatbuffers::Offset<Galaxy>>(*galaxies) : 0;
|
||||
return CreateUniverse(
|
||||
_fbb,
|
||||
age,
|
||||
galaxies__);
|
||||
}
|
||||
|
||||
inline const Universe *GetUniverse(const void *buf) {
|
||||
return ::flatbuffers::GetRoot<Universe>(buf);
|
||||
}
|
||||
|
||||
inline const Universe *GetSizePrefixedUniverse(const void *buf) {
|
||||
return ::flatbuffers::GetSizePrefixedRoot<Universe>(buf);
|
||||
}
|
||||
|
||||
inline bool VerifyUniverseBuffer(
|
||||
::flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifyBuffer<Universe>(nullptr);
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedUniverseBuffer(
|
||||
::flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<Universe>(nullptr);
|
||||
}
|
||||
|
||||
inline void FinishUniverseBuffer(
|
||||
::flatbuffers::FlatBufferBuilder &fbb,
|
||||
::flatbuffers::Offset<Universe> root) {
|
||||
fbb.Finish(root);
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedUniverseBuffer(
|
||||
::flatbuffers::FlatBufferBuilder &fbb,
|
||||
::flatbuffers::Offset<Universe> root) {
|
||||
fbb.FinishSizePrefixed(root);
|
||||
}
|
||||
|
||||
#endif // FLATBUFFERS_GENERATED_BASIC_H_
|
10
goldens/cpp/generate.py
Normal file
10
goldens/cpp/generate.py
Normal file
@ -0,0 +1,10 @@
|
||||
from golden_utils import flatc_golden
|
||||
|
||||
|
||||
def flatc(options, schema):
|
||||
# Wrap the golden flatc generator with C++ specifics
|
||||
flatc_golden(options=["--cpp"] + options, schema=schema, prefix="cpp")
|
||||
|
||||
|
||||
def GenerateCpp():
|
||||
flatc([], "basic.fbs")
|
45
goldens/csharp/Galaxy.cs
Normal file
45
goldens/csharp/Galaxy.cs
Normal file
@ -0,0 +1,45 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::Google.FlatBuffers;
|
||||
|
||||
public struct Galaxy : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_23_3_3(); }
|
||||
public static Galaxy GetRootAsGalaxy(ByteBuffer _bb) { return GetRootAsGalaxy(_bb, new Galaxy()); }
|
||||
public static Galaxy GetRootAsGalaxy(ByteBuffer _bb, Galaxy obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
|
||||
public Galaxy __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public long NumStars { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetLong(o + __p.bb_pos) : (long)0; } }
|
||||
|
||||
public static Offset<Galaxy> CreateGalaxy(FlatBufferBuilder builder,
|
||||
long num_stars = 0) {
|
||||
builder.StartTable(1);
|
||||
Galaxy.AddNumStars(builder, num_stars);
|
||||
return Galaxy.EndGalaxy(builder);
|
||||
}
|
||||
|
||||
public static void StartGalaxy(FlatBufferBuilder builder) { builder.StartTable(1); }
|
||||
public static void AddNumStars(FlatBufferBuilder builder, long numStars) { builder.AddLong(0, numStars, 0); }
|
||||
public static Offset<Galaxy> EndGalaxy(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<Galaxy>(o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static public class GalaxyVerify
|
||||
{
|
||||
static public bool Verify(Google.FlatBuffers.Verifier verifier, uint tablePos)
|
||||
{
|
||||
return verifier.VerifyTableStart(tablePos)
|
||||
&& verifier.VerifyField(tablePos, 4 /*NumStars*/, 8 /*long*/, 8, false)
|
||||
&& verifier.VerifyTableEnd(tablePos);
|
||||
}
|
||||
}
|
59
goldens/csharp/Universe.cs
Normal file
59
goldens/csharp/Universe.cs
Normal file
@ -0,0 +1,59 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::Google.FlatBuffers;
|
||||
|
||||
public struct Universe : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_23_3_3(); }
|
||||
public static Universe GetRootAsUniverse(ByteBuffer _bb) { return GetRootAsUniverse(_bb, new Universe()); }
|
||||
public static Universe GetRootAsUniverse(ByteBuffer _bb, Universe obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
public static bool VerifyUniverse(ByteBuffer _bb) {Google.FlatBuffers.Verifier verifier = new Google.FlatBuffers.Verifier(_bb); return verifier.VerifyBuffer("", false, UniverseVerify.Verify); }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
|
||||
public Universe __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public double Age { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetDouble(o + __p.bb_pos) : (double)0.0; } }
|
||||
public Galaxy? Galaxies(int j) { int o = __p.__offset(6); return o != 0 ? (Galaxy?)(new Galaxy()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
|
||||
public int GalaxiesLength { get { int o = __p.__offset(6); return o != 0 ? __p.__vector_len(o) : 0; } }
|
||||
|
||||
public static Offset<Universe> CreateUniverse(FlatBufferBuilder builder,
|
||||
double age = 0.0,
|
||||
VectorOffset galaxiesOffset = default(VectorOffset)) {
|
||||
builder.StartTable(2);
|
||||
Universe.AddAge(builder, age);
|
||||
Universe.AddGalaxies(builder, galaxiesOffset);
|
||||
return Universe.EndUniverse(builder);
|
||||
}
|
||||
|
||||
public static void StartUniverse(FlatBufferBuilder builder) { builder.StartTable(2); }
|
||||
public static void AddAge(FlatBufferBuilder builder, double age) { builder.AddDouble(0, age, 0.0); }
|
||||
public static void AddGalaxies(FlatBufferBuilder builder, VectorOffset galaxiesOffset) { builder.AddOffset(1, galaxiesOffset.Value, 0); }
|
||||
public static VectorOffset CreateGalaxiesVector(FlatBufferBuilder builder, Offset<Galaxy>[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); }
|
||||
public static VectorOffset CreateGalaxiesVectorBlock(FlatBufferBuilder builder, Offset<Galaxy>[] data) { builder.StartVector(4, data.Length, 4); builder.Add(data); return builder.EndVector(); }
|
||||
public static VectorOffset CreateGalaxiesVectorBlock(FlatBufferBuilder builder, ArraySegment<Offset<Galaxy>> data) { builder.StartVector(4, data.Count, 4); builder.Add(data); return builder.EndVector(); }
|
||||
public static VectorOffset CreateGalaxiesVectorBlock(FlatBufferBuilder builder, IntPtr dataPtr, int sizeInBytes) { builder.StartVector(1, sizeInBytes, 1); builder.Add<Offset<Galaxy>>(dataPtr, sizeInBytes); return builder.EndVector(); }
|
||||
public static void StartGalaxiesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); }
|
||||
public static Offset<Universe> EndUniverse(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<Universe>(o);
|
||||
}
|
||||
public static void FinishUniverseBuffer(FlatBufferBuilder builder, Offset<Universe> offset) { builder.Finish(offset.Value); }
|
||||
public static void FinishSizePrefixedUniverseBuffer(FlatBufferBuilder builder, Offset<Universe> offset) { builder.FinishSizePrefixed(offset.Value); }
|
||||
}
|
||||
|
||||
|
||||
static public class UniverseVerify
|
||||
{
|
||||
static public bool Verify(Google.FlatBuffers.Verifier verifier, uint tablePos)
|
||||
{
|
||||
return verifier.VerifyTableStart(tablePos)
|
||||
&& verifier.VerifyField(tablePos, 4 /*Age*/, 8 /*double*/, 8, false)
|
||||
&& verifier.VerifyVectorOfTables(tablePos, 6 /*Galaxies*/, GalaxyVerify.Verify, false)
|
||||
&& verifier.VerifyTableEnd(tablePos);
|
||||
}
|
||||
}
|
10
goldens/csharp/generate.py
Normal file
10
goldens/csharp/generate.py
Normal file
@ -0,0 +1,10 @@
|
||||
from golden_utils import flatc_golden
|
||||
|
||||
|
||||
def flatc(options, schema):
|
||||
# Wrap the golden flatc generator with C# specifics
|
||||
flatc_golden(options=["--csharp"] + options, schema=schema, prefix="csharp")
|
||||
|
||||
|
||||
def GenerateCSharp():
|
||||
flatc([], "basic.fbs")
|
10
goldens/generate_goldens.py
Executable file
10
goldens/generate_goldens.py
Executable file
@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from cpp.generate import GenerateCpp
|
||||
from csharp.generate import GenerateCSharp
|
||||
|
||||
# Run each language generation logic
|
||||
GenerateCpp()
|
||||
GenerateCSharp()
|
||||
|
||||
# TODO add other languages
|
30
goldens/golden_utils.py
Normal file
30
goldens/golden_utils.py
Normal file
@ -0,0 +1,30 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Get the path where this script is located so we can invoke the script from
|
||||
# any directory and have the paths work correctly.
|
||||
script_path = Path(__file__).parent.resolve()
|
||||
|
||||
# Get the root path as an absolute path, so all derived paths are absolute.
|
||||
root_path = script_path.parent.absolute()
|
||||
|
||||
# Get the location of the schema
|
||||
schema_path = Path(script_path, "schema")
|
||||
|
||||
# Too add the util package in /scripts/util.py
|
||||
sys.path.append(str(root_path.absolute()))
|
||||
|
||||
from scripts.util import flatc
|
||||
|
||||
|
||||
def flatc_golden(options, schema, prefix):
|
||||
# wrap the generic flatc call with specifis for these goldens.
|
||||
flatc(
|
||||
options=options,
|
||||
# where the files are generated, typically the language (e.g. "cpp").
|
||||
prefix=prefix,
|
||||
# The schema are relative to the schema directory.
|
||||
schema=str(Path(schema_path, schema)),
|
||||
# Run flatc from this location.
|
||||
cwd=script_path,
|
||||
)
|
13
goldens/schema/basic.fbs
Normal file
13
goldens/schema/basic.fbs
Normal file
@ -0,0 +1,13 @@
|
||||
// This file should contain the basics of flatbuffers that all languages should
|
||||
// support.
|
||||
|
||||
table Galaxy {
|
||||
num_stars:long;
|
||||
}
|
||||
|
||||
table Universe {
|
||||
age:double;
|
||||
galaxies:[Galaxy];
|
||||
}
|
||||
|
||||
root_type Universe;
|
Loading…
x
Reference in New Issue
Block a user