Skip to content

FlatBuffers

Virtufin.Data carries a FlatBuffers schema of the Variant kinds, src/Virtufin.Data/FlatBuffers/variant.fbs, and FlatBuffersVariant, an IVariant that reads a variant straight from a FlatBuffers buffer without copying it.

The schema

One table per kind and a FlatBuffers union Value over them:

namespace Virtufin.Data.FlatBuffers;

table Int32Value { value:int; }
table ArrayDoubleValue { value:[double]; }
table ArrayVariantValue { value:[Variant]; }
table DictionaryEntry { key:string; value:Variant; }
table DictionaryValue { entries:[DictionaryEntry]; }
// … one table per kind: None, Bool, Byte, Int16, Int32, Int64, Float, Double, String,
// ArrayBool … ArrayString, ArrayVariant, Dictionary

union Value { None:NoneValue, Bool:BoolValue, /* … */ ArrayVariant:ArrayVariantValue,
              DictionaryEntry:DictionaryEntry, Dictionary:DictionaryValue }

table Variant { value:Value; }
root_type Variant;

Code generation

The build runs flatc --csharp on the schema before compiling (GenerateFlatBuffers in Virtufin.Data.csproj) and compiles the output from artifacts/. flatc must be exactly the version of the Google.FlatBuffers package (FlatcVersion in Directory.Build.props); the build fails naming both otherwise. On macOS the repo's tools/flatc is used; do not use a brew-installed one, which tracks the latest release. See Development.

Reading

using Google.FlatBuffers;
using Virtufin.Data;

var root = Virtufin.Data.FlatBuffers.Variant.GetRootAsVariant(new ByteBuffer(bytes));
IVariant v = new FlatBuffersVariant(root);
if (v.IsArrayDouble) Console.WriteLine(v.ArrayDouble.Length);

FlatBuffersVariant implements the Is<Kind> checks and the getters. VariantType is not implemented yet and throws NotImplementedException. There is no writer in the library: a buffer is built with the generated builder code (FlatBufferBuilder).

Users

The schema is C#-only today: no other repo or language reads or writes it. A change to variant.fbs is a change to Virtufin.Data and goes through the usual review; if another language starts to use it, the schema becomes a cross-language contract and changes go through openspec.