[TS] Fix generation of struct members in object api (#7148)

* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* [TS] Fix reserved words as arguments (#6955)

* [TS] Fix generation of reserved words in object api (#7106)

* [TS] Fix generation of object api

* [TS] Fix MakeCamel -> ConvertCase

* [TS] Add test for struct of struct of struct

* Update generated files

* Add missing files

* [TS] Fix query of null/undefined fields in object api
This commit is contained in:
tira-misu
2022-03-24 05:40:11 +01:00
committed by GitHub
parent 4213d91054
commit 2ad408697f
27 changed files with 1490 additions and 344 deletions

View File

@@ -0,0 +1,74 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::System.Collections.Generic;
using global::FlatBuffers;
public struct StructOfStructsOfStructs : IFlatbufferObject
{
private Struct __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); }
public StructOfStructsOfStructs __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.StructOfStructs A { get { return (new MyGame.Example.StructOfStructs()).__assign(__p.bb_pos + 0, __p.bb); } }
public static Offset<MyGame.Example.StructOfStructsOfStructs> CreateStructOfStructsOfStructs(FlatBufferBuilder builder, uint a_a_Id, uint a_a_Distance, short a_b_A, sbyte a_b_B, uint a_c_Id, uint a_c_Distance) {
builder.Prep(4, 20);
builder.Prep(4, 20);
builder.Prep(4, 8);
builder.PutUint(a_c_Distance);
builder.PutUint(a_c_Id);
builder.Prep(2, 4);
builder.Pad(1);
builder.PutSbyte(a_b_B);
builder.PutShort(a_b_A);
builder.Prep(4, 8);
builder.PutUint(a_a_Distance);
builder.PutUint(a_a_Id);
return new Offset<MyGame.Example.StructOfStructsOfStructs>(builder.Offset);
}
public StructOfStructsOfStructsT UnPack() {
var _o = new StructOfStructsOfStructsT();
this.UnPackTo(_o);
return _o;
}
public void UnPackTo(StructOfStructsOfStructsT _o) {
_o.A = this.A.UnPack();
}
public static Offset<MyGame.Example.StructOfStructsOfStructs> Pack(FlatBufferBuilder builder, StructOfStructsOfStructsT _o) {
if (_o == null) return default(Offset<MyGame.Example.StructOfStructsOfStructs>);
var _a_a_id = _o.A.A.Id;
var _a_a_distance = _o.A.A.Distance;
var _a_b_a = _o.A.B.A;
var _a_b_b = _o.A.B.B;
var _a_c_id = _o.A.C.Id;
var _a_c_distance = _o.A.C.Distance;
return CreateStructOfStructsOfStructs(
builder,
_a_a_id,
_a_a_distance,
_a_b_a,
_a_b_b,
_a_c_id,
_a_c_distance);
}
}
public class StructOfStructsOfStructsT
{
[Newtonsoft.Json.JsonProperty("a")]
public MyGame.Example.StructOfStructsT A { get; set; }
public StructOfStructsOfStructsT() {
this.A = new MyGame.Example.StructOfStructsT();
}
}
}

View File

@@ -0,0 +1,63 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package Example
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type StructOfStructsOfStructsT struct {
A *StructOfStructsT
}
func (t *StructOfStructsOfStructsT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
return CreateStructOfStructsOfStructs(builder, t.A.A.Id, t.A.A.Distance, t.A.B.A, t.A.B.B, t.A.C.Id, t.A.C.Distance)
}
func (rcv *StructOfStructsOfStructs) UnPackTo(t *StructOfStructsOfStructsT) {
t.A = rcv.A(nil).UnPack()
}
func (rcv *StructOfStructsOfStructs) UnPack() *StructOfStructsOfStructsT {
if rcv == nil { return nil }
t := &StructOfStructsOfStructsT{}
rcv.UnPackTo(t)
return t
}
type StructOfStructsOfStructs struct {
_tab flatbuffers.Struct
}
func (rcv *StructOfStructsOfStructs) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *StructOfStructsOfStructs) Table() flatbuffers.Table {
return rcv._tab.Table
}
func (rcv *StructOfStructsOfStructs) A(obj *StructOfStructs) *StructOfStructs {
if obj == nil {
obj = new(StructOfStructs)
}
obj.Init(rcv._tab.Bytes, rcv._tab.Pos+0)
return obj
}
func CreateStructOfStructsOfStructs(builder *flatbuffers.Builder, a_a_id uint32, a_a_distance uint32, a_b_a int16, a_b_b int8, a_c_id uint32, a_c_distance uint32) flatbuffers.UOffsetT {
builder.Prep(4, 20)
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(a_c_distance)
builder.PrependUint32(a_c_id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(a_b_b)
builder.PrependInt16(a_b_a)
builder.Prep(4, 8)
builder.PrependUint32(a_a_distance)
builder.PrependUint32(a_a_id)
return builder.Offset()
}

View File

@@ -0,0 +1,66 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class StructOfStructsOfStructs extends Struct {
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public StructOfStructsOfStructs __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.StructOfStructs a() { return a(new MyGame.Example.StructOfStructs()); }
public MyGame.Example.StructOfStructs a(MyGame.Example.StructOfStructs obj) { return obj.__assign(bb_pos + 0, bb); }
public static int createStructOfStructsOfStructs(FlatBufferBuilder builder, long a_a_id, long a_a_distance, short a_b_a, byte a_b_b, long a_c_id, long a_c_distance) {
builder.prep(4, 20);
builder.prep(4, 20);
builder.prep(4, 8);
builder.putInt((int) a_c_distance);
builder.putInt((int) a_c_id);
builder.prep(2, 4);
builder.pad(1);
builder.putByte(a_b_b);
builder.putShort(a_b_a);
builder.prep(4, 8);
builder.putInt((int) a_a_distance);
builder.putInt((int) a_a_id);
return builder.offset();
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public StructOfStructsOfStructs get(int j) { return get(new StructOfStructsOfStructs(), j); }
public StructOfStructsOfStructs get(StructOfStructsOfStructs obj, int j) { return obj.__assign(__element(j), bb); }
}
public StructOfStructsOfStructsT unpack() {
StructOfStructsOfStructsT _o = new StructOfStructsOfStructsT();
unpackTo(_o);
return _o;
}
public void unpackTo(StructOfStructsOfStructsT _o) {
a().unpackTo(_o.getA());
}
public static int pack(FlatBufferBuilder builder, StructOfStructsOfStructsT _o) {
if (_o == null) return 0;
int _a_a_id = _o.getA().getA().getId();
int _a_a_distance = _o.getA().getA().getDistance();
short _a_b_a = _o.getA().getB().getA();
byte _a_b_b = _o.getA().getB().getB();
int _a_c_id = _o.getA().getC().getId();
int _a_c_distance = _o.getA().getC().getDistance();
return createStructOfStructsOfStructs(
builder,
_a_a_id,
_a_a_distance,
_a_b_a,
_a_b_b,
_a_c_id,
_a_c_distance);
}
}

View File

@@ -0,0 +1,38 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
class StructOfStructsOfStructs : Struct() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : StructOfStructsOfStructs {
__init(_i, _bb)
return this
}
val a : MyGame.Example.StructOfStructs? get() = a(MyGame.Example.StructOfStructs())
fun a(obj: MyGame.Example.StructOfStructs) : MyGame.Example.StructOfStructs? = obj.__assign(bb_pos + 0, bb)
companion object {
fun createStructOfStructsOfStructs(builder: FlatBufferBuilder, a_a_id: UInt, a_a_distance: UInt, a_b_a: Short, a_b_b: Byte, a_c_id: UInt, a_c_distance: UInt) : Int {
builder.prep(4, 20)
builder.prep(4, 20)
builder.prep(4, 8)
builder.putInt(a_c_distance.toInt())
builder.putInt(a_c_id.toInt())
builder.prep(2, 4)
builder.pad(1)
builder.putByte(a_b_b)
builder.putShort(a_b_a)
builder.prep(4, 8)
builder.putInt(a_a_distance.toInt())
builder.putInt(a_a_id.toInt())
return builder.offset()
}
}
}

View File

@@ -0,0 +1,49 @@
--[[ MyGame.Example.StructOfStructsOfStructs
Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.
flatc version: 2.0.6
Declared by : //monster_test.fbs
Rooting type : MyGame.Example.Monster (//monster_test.fbs)
--]]
local flatbuffers = require('flatbuffers')
local StructOfStructsOfStructs = {}
local mt = {}
function StructOfStructsOfStructs.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:A(obj)
obj:Init(self.view.bytes, self.view.pos + 0)
return obj
end
function StructOfStructsOfStructs.CreateStructOfStructsOfStructs(builder, a_a_id, a_a_distance, a_b_a, a_b_b, a_c_id, a_c_distance)
builder:Prep(4, 20)
builder:Prep(4, 20)
builder:Prep(4, 8)
builder:PrependUint32(a_c_distance)
builder:PrependUint32(a_c_id)
builder:Prep(2, 4)
builder:Pad(1)
builder:PrependInt8(a_b_b)
builder:PrependInt16(a_b_a)
builder:Prep(4, 8)
builder:PrependUint32(a_a_distance)
builder:PrependUint32(a_a_id)
return builder:Offset()
end
return StructOfStructsOfStructs

View File

@@ -0,0 +1,55 @@
<?php
// automatically generated by the FlatBuffers compiler, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class StructOfStructsOfStructs extends Struct
{
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return StructOfStructsOfStructs
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return StructOfStructs
*/
public function getA()
{
$obj = new StructOfStructs();
$obj->init($this->bb_pos + 0, $this->bb);
return $obj;
}
/**
* @return int offset
*/
public static function createStructOfStructsOfStructs(FlatBufferBuilder $builder, $a_a_id, $a_a_distance, $a_b_a, $a_b_b, $a_c_id, $a_c_distance)
{
$builder->prep(4, 20);
$builder->prep(4, 20);
$builder->prep(4, 8);
$builder->putUint($a_c_distance);
$builder->putUint($a_c_id);
$builder->prep(2, 4);
$builder->pad(1);
$builder->putSbyte($a_b_b);
$builder->putShort($a_b_a);
$builder->prep(4, 8);
$builder->putUint($a_a_distance);
$builder->putUint($a_a_id);
return $builder->offset();
}
}

View File

@@ -0,0 +1,74 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class StructOfStructsOfStructs(object):
__slots__ = ['_tab']
@classmethod
def SizeOf(cls):
return 20
# StructOfStructsOfStructs
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# StructOfStructsOfStructs
def A(self, obj):
obj.Init(self._tab.Bytes, self._tab.Pos + 0)
return obj
def CreateStructOfStructsOfStructs(builder, a_a_id, a_a_distance, a_b_a, a_b_b, a_c_id, a_c_distance):
builder.Prep(4, 20)
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(a_c_distance)
builder.PrependUint32(a_c_id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(a_b_b)
builder.PrependInt16(a_b_a)
builder.Prep(4, 8)
builder.PrependUint32(a_a_distance)
builder.PrependUint32(a_a_id)
return builder.Offset()
import MyGame.Example.StructOfStructs
try:
from typing import Optional
except:
pass
class StructOfStructsOfStructsT(object):
# StructOfStructsOfStructsT
def __init__(self):
self.a = None # type: Optional[MyGame.Example.StructOfStructs.StructOfStructsT]
@classmethod
def InitFromBuf(cls, buf, pos):
structOfStructsOfStructs = StructOfStructsOfStructs()
structOfStructsOfStructs.Init(buf, pos)
return cls.InitFromObj(structOfStructsOfStructs)
@classmethod
def InitFromObj(cls, structOfStructsOfStructs):
x = StructOfStructsOfStructsT()
x._UnPack(structOfStructsOfStructs)
return x
# StructOfStructsOfStructsT
def _UnPack(self, structOfStructsOfStructs):
if structOfStructsOfStructs is None:
return
if structOfStructsOfStructs.A(MyGame.Example.StructOfStructs.StructOfStructs()) is not None:
self.a = MyGame.Example.StructOfStructs.StructOfStructsT.InitFromObj(structOfStructsOfStructs.A(MyGame.Example.StructOfStructs.StructOfStructs()))
# StructOfStructsOfStructsT
def Pack(self, builder):
return CreateStructOfStructsOfStructs(builder, self.a.a.id, self.a.a.distance, self.a.b.a, self.a.b.b, self.a.c.id, self.a.c.distance)

View File

@@ -0,0 +1,22 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
public class StructOfStructsOfStructsT {
private MyGame.Example.StructOfStructsT a;
public MyGame.Example.StructOfStructsT getA() { return a; }
public void setA(MyGame.Example.StructOfStructsT a) { this.a = a; }
public StructOfStructsOfStructsT() {
this.a = new MyGame.Example.StructOfStructsT();
}
}