Skip to content

Commit

Permalink
[TS] Fix generation of struct members in object api (#7148)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tira-misu committed Mar 24, 2022
1 parent 4213d91 commit 2ad4086
Show file tree
Hide file tree
Showing 27 changed files with 1,490 additions and 344 deletions.
17 changes: 12 additions & 5 deletions src/idl_gen_ts.cpp
Expand Up @@ -55,7 +55,7 @@ class TsGenerator : public BaseGenerator {
// https://github.com/microsoft/TypeScript/issues/2536
// One per line to ease comparisons to that list are easier
static const char *const keywords[] = {
"argument",
"arguments",
"break",
"case",
"catch",
Expand Down Expand Up @@ -956,15 +956,22 @@ class TsGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;

const auto curr_member_accessor =
auto curr_member_accessor =
prefix + "." + ConvertCase(field.name, Case::kLowerCamel);
if (prefix != "this") {
curr_member_accessor =
prefix + "?." + ConvertCase(field.name, Case::kLowerCamel);
}
if (IsStruct(field.value.type)) {
ret += GenStructMemberValueTS(*field.value.type.struct_def,
curr_member_accessor, delimiter);
} else {
if (nullCheck) {
ret +=
"(" + prefix + " === null ? 0 : " + curr_member_accessor + "!)";
std::string nullValue = "0";
if (field.value.type.base_type == BASE_TYPE_BOOL) {
nullValue = "false";
}
ret += "(" + curr_member_accessor + " ?? " + nullValue + ")";
} else {
ret += curr_member_accessor;
}
Expand Down Expand Up @@ -1067,7 +1074,7 @@ class TsGenerator : public BaseGenerator {
parser.opts);

const std::string field_accessor =
"this." + field_name_escaped + "()";
"this." + field_name + "()";
field_val = GenNullCheckConditional(field_accessor,
field_accessor + "!.unpack()");
auto packing = GenNullCheckConditional(
Expand Down
Expand Up @@ -611,6 +611,72 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject {
}
}

public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {

static func validateVersion() { FlatBuffersVersion_2_0_0() }

private var _a: MyGame_Example_StructOfStructs

public init(_ bb: ByteBuffer, o: Int32) {
let _accessor = Struct(bb: bb, position: o)
_a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.postion + 0)
}

public init(a: MyGame_Example_StructOfStructs) {
_a = a
}

public init() {
_a = MyGame_Example_StructOfStructs()
}

public init(_ _t: inout MyGame_Example_StructOfStructsOfStructs_Mutable) {
var _va = _t.a
_a = _va.unpack()
}

public var a: MyGame_Example_StructOfStructs { _a }

public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: MyGame_Example_StructOfStructsOfStructs.self)
}
}

extension MyGame_Example_StructOfStructsOfStructs: Encodable {

enum CodingKeys: String, CodingKey {
case a = "a"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(a, forKey: .a)
}
}

public struct MyGame_Example_StructOfStructsOfStructs_Mutable: FlatBufferObject {

static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Struct

public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }

public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.postion + 0) }


public mutating func unpack() -> MyGame_Example_StructOfStructsOfStructs {
return MyGame_Example_StructOfStructsOfStructs(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructsOfStructs?) -> Offset {
guard var obj = obj else { return Offset() }
return pack(&builder, obj: &obj)
}

public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructsOfStructs) -> Offset {
return builder.create(struct: obj)
}
}

public struct MyGame_InParentNamespace: FlatBufferObject, Verifiable, ObjectAPIPacker {

static func validateVersion() { FlatBuffersVersion_2_0_0() }
Expand Down
74 changes: 74 additions & 0 deletions tests/MyGame/Example/StructOfStructsOfStructs.cs
@@ -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();
}
}


}
63 changes: 63 additions & 0 deletions tests/MyGame/Example/StructOfStructsOfStructs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions tests/MyGame/Example/StructOfStructsOfStructs.java
@@ -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);
}
}

38 changes: 38 additions & 0 deletions tests/MyGame/Example/StructOfStructsOfStructs.kt
@@ -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()
}
}
}

0 comments on commit 2ad4086

Please sign in to comment.