mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-19 19:45:43 +00:00
Fix Rust codegen escaping field in tables. (#7659)
* Fix Rust codegen escaping field in tables. * other gencode * gencode * removed a debug print * regen code Co-authored-by: Casper Neo <cneo@google.com> Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
94
tests/KeywordTest/Table2.cs
Normal file
94
tests/KeywordTest/Table2.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
namespace KeywordTest
|
||||
{
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::Google.FlatBuffers;
|
||||
|
||||
public struct Table2 : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_22_12_06(); }
|
||||
public static Table2 GetRootAsTable2(ByteBuffer _bb) { return GetRootAsTable2(_bb, new Table2()); }
|
||||
public static Table2 GetRootAsTable2(ByteBuffer _bb, Table2 obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
|
||||
public Table2 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public KeywordTest.KeywordsInUnion TypeType { get { int o = __p.__offset(4); return o != 0 ? (KeywordTest.KeywordsInUnion)__p.bb.Get(o + __p.bb_pos) : KeywordTest.KeywordsInUnion.NONE; } }
|
||||
public TTable? Type<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||
public KeywordTest.KeywordsInTable TypeAsstatic() { return Type<KeywordTest.KeywordsInTable>().Value; }
|
||||
public KeywordTest.KeywordsInTable TypeAsinternal() { return Type<KeywordTest.KeywordsInTable>().Value; }
|
||||
|
||||
public static Offset<KeywordTest.Table2> CreateTable2(FlatBufferBuilder builder,
|
||||
KeywordTest.KeywordsInUnion type_type = KeywordTest.KeywordsInUnion.NONE,
|
||||
int typeOffset = 0) {
|
||||
builder.StartTable(2);
|
||||
Table2.AddType(builder, typeOffset);
|
||||
Table2.AddTypeType(builder, type_type);
|
||||
return Table2.EndTable2(builder);
|
||||
}
|
||||
|
||||
public static void StartTable2(FlatBufferBuilder builder) { builder.StartTable(2); }
|
||||
public static void AddTypeType(FlatBufferBuilder builder, KeywordTest.KeywordsInUnion typeType) { builder.AddByte(0, (byte)typeType, 0); }
|
||||
public static void AddType(FlatBufferBuilder builder, int typeOffset) { builder.AddOffset(1, typeOffset, 0); }
|
||||
public static Offset<KeywordTest.Table2> EndTable2(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<KeywordTest.Table2>(o);
|
||||
}
|
||||
public Table2T UnPack() {
|
||||
var _o = new Table2T();
|
||||
this.UnPackTo(_o);
|
||||
return _o;
|
||||
}
|
||||
public void UnPackTo(Table2T _o) {
|
||||
_o.Type = new KeywordTest.KeywordsInUnionUnion();
|
||||
_o.Type.Type = this.TypeType;
|
||||
switch (this.TypeType) {
|
||||
default: break;
|
||||
case KeywordTest.KeywordsInUnion.static:
|
||||
_o.Type.Value = this.Type<KeywordTest.KeywordsInTable>().HasValue ? this.Type<KeywordTest.KeywordsInTable>().Value.UnPack() : null;
|
||||
break;
|
||||
case KeywordTest.KeywordsInUnion.internal:
|
||||
_o.Type.Value = this.Type<KeywordTest.KeywordsInTable>().HasValue ? this.Type<KeywordTest.KeywordsInTable>().Value.UnPack() : null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static Offset<KeywordTest.Table2> Pack(FlatBufferBuilder builder, Table2T _o) {
|
||||
if (_o == null) return default(Offset<KeywordTest.Table2>);
|
||||
var _type_type = _o.Type == null ? KeywordTest.KeywordsInUnion.NONE : _o.Type.Type;
|
||||
var _type = _o.Type == null ? 0 : KeywordTest.KeywordsInUnionUnion.Pack(builder, _o.Type);
|
||||
return CreateTable2(
|
||||
builder,
|
||||
_type_type,
|
||||
_type);
|
||||
}
|
||||
}
|
||||
|
||||
public class Table2T
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("type_type")]
|
||||
private KeywordTest.KeywordsInUnion TypeType {
|
||||
get {
|
||||
return this.Type != null ? this.Type.Type : KeywordTest.KeywordsInUnion.NONE;
|
||||
}
|
||||
set {
|
||||
this.Type = new KeywordTest.KeywordsInUnionUnion();
|
||||
this.Type.Type = value;
|
||||
}
|
||||
}
|
||||
[Newtonsoft.Json.JsonProperty("type")]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(KeywordTest.KeywordsInUnionUnion_JsonConverter))]
|
||||
public KeywordTest.KeywordsInUnionUnion Type { get; set; }
|
||||
|
||||
public Table2T() {
|
||||
this.Type = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -15,3 +15,7 @@ union KeywordsInUnion {
|
||||
static: KeywordsInTable,
|
||||
internal: KeywordsInTable,
|
||||
}
|
||||
|
||||
table Table2 {
|
||||
type: KeywordsInUnion;
|
||||
}
|
||||
|
||||
227
tests/keyword_test/keyword_test/table_2_generated.rs
Normal file
227
tests/keyword_test/keyword_test/table_2_generated.rs
Normal file
@@ -0,0 +1,227 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// @generated
|
||||
extern crate alloc;
|
||||
extern crate flatbuffers;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec::Vec;
|
||||
use core::mem;
|
||||
use core::cmp::Ordering;
|
||||
use self::flatbuffers::{EndianScalar, Follow};
|
||||
use super::*;
|
||||
pub enum Table2Offset {}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct Table2<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Table2<'a> {
|
||||
type Inner = Table2<'a>;
|
||||
#[inline]
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table::new(buf, loc) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Table2<'a> {
|
||||
pub const VT_TYPE_TYPE: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_TYPE_: flatbuffers::VOffsetT = 6;
|
||||
|
||||
pub const fn get_fully_qualified_name() -> &'static str {
|
||||
"KeywordTest.Table2"
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
Table2 { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args Table2Args
|
||||
) -> flatbuffers::WIPOffset<Table2<'bldr>> {
|
||||
let mut builder = Table2Builder::new(_fbb);
|
||||
if let Some(x) = args.type_ { builder.add_type_(x); }
|
||||
builder.add_type_type(args.type_type);
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub fn unpack(&self) -> Table2T {
|
||||
let type_ = match self.type_type() {
|
||||
KeywordsInUnion::NONE => KeywordsInUnionT::NONE,
|
||||
KeywordsInUnion::static_ => KeywordsInUnionT::Static_(Box::new(
|
||||
self.type__as_static_()
|
||||
.expect("Invalid union table, expected `KeywordsInUnion::static_`.")
|
||||
.unpack()
|
||||
)),
|
||||
KeywordsInUnion::internal => KeywordsInUnionT::Internal(Box::new(
|
||||
self.type__as_internal()
|
||||
.expect("Invalid union table, expected `KeywordsInUnion::internal`.")
|
||||
.unpack()
|
||||
)),
|
||||
_ => KeywordsInUnionT::NONE,
|
||||
};
|
||||
Table2T {
|
||||
type_,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn type_type(&self) -> KeywordsInUnion {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<KeywordsInUnion>(Table2::VT_TYPE_TYPE, Some(KeywordsInUnion::NONE)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn type_(&self) -> Option<flatbuffers::Table<'a>> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Table2::VT_TYPE_, None)}
|
||||
}
|
||||
#[inline]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn type__as_static_(&self) -> Option<KeywordsInTable<'a>> {
|
||||
if self.type_type() == KeywordsInUnion::static_ {
|
||||
self.type_().map(|t| {
|
||||
// Safety:
|
||||
// Created from a valid Table for this object
|
||||
// Which contains a valid union in this slot
|
||||
unsafe { KeywordsInTable::init_from_table(t) }
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn type__as_internal(&self) -> Option<KeywordsInTable<'a>> {
|
||||
if self.type_type() == KeywordsInUnion::internal {
|
||||
self.type_().map(|t| {
|
||||
// Safety:
|
||||
// Created from a valid Table for this object
|
||||
// Which contains a valid union in this slot
|
||||
unsafe { KeywordsInTable::init_from_table(t) }
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl flatbuffers::Verifiable for Table2<'_> {
|
||||
#[inline]
|
||||
fn run_verifier(
|
||||
v: &mut flatbuffers::Verifier, pos: usize
|
||||
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
|
||||
use self::flatbuffers::Verifiable;
|
||||
v.visit_table(pos)?
|
||||
.visit_union::<KeywordsInUnion, _>("type_type", Self::VT_TYPE_TYPE, "type_", Self::VT_TYPE_, false, |key, v, pos| {
|
||||
match key {
|
||||
KeywordsInUnion::static_ => v.verify_union_variant::<flatbuffers::ForwardsUOffset<KeywordsInTable>>("KeywordsInUnion::static_", pos),
|
||||
KeywordsInUnion::internal => v.verify_union_variant::<flatbuffers::ForwardsUOffset<KeywordsInTable>>("KeywordsInUnion::internal", pos),
|
||||
_ => Ok(()),
|
||||
}
|
||||
})?
|
||||
.finish();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub struct Table2Args {
|
||||
pub type_type: KeywordsInUnion,
|
||||
pub type_: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
|
||||
}
|
||||
impl<'a> Default for Table2Args {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Table2Args {
|
||||
type_type: KeywordsInUnion::NONE,
|
||||
type_: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Table2Builder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> Table2Builder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_type_type(&mut self, type_type: KeywordsInUnion) {
|
||||
self.fbb_.push_slot::<KeywordsInUnion>(Table2::VT_TYPE_TYPE, type_type, KeywordsInUnion::NONE);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_type_(&mut self, type_: flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Table2::VT_TYPE_, type_);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> Table2Builder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
Table2Builder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<Table2<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for Table2<'_> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let mut ds = f.debug_struct("Table2");
|
||||
ds.field("type_type", &self.type_type());
|
||||
match self.type_type() {
|
||||
KeywordsInUnion::static_ => {
|
||||
if let Some(x) = self.type__as_static_() {
|
||||
ds.field("type_", &x)
|
||||
} else {
|
||||
ds.field("type_", &"InvalidFlatbuffer: Union discriminant does not match value.")
|
||||
}
|
||||
},
|
||||
KeywordsInUnion::internal => {
|
||||
if let Some(x) = self.type__as_internal() {
|
||||
ds.field("type_", &x)
|
||||
} else {
|
||||
ds.field("type_", &"InvalidFlatbuffer: Union discriminant does not match value.")
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
let x: Option<()> = None;
|
||||
ds.field("type_", &x)
|
||||
},
|
||||
};
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Table2T {
|
||||
pub type_: KeywordsInUnionT,
|
||||
}
|
||||
impl Default for Table2T {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
type_: KeywordsInUnionT::NONE,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Table2T {
|
||||
pub fn pack<'b>(
|
||||
&self,
|
||||
_fbb: &mut flatbuffers::FlatBufferBuilder<'b>
|
||||
) -> flatbuffers::WIPOffset<Table2<'b>> {
|
||||
let type_type = self.type_.keywords_in_union_type();
|
||||
let type_ = self.type_.pack(_fbb);
|
||||
Table2::create(_fbb, &Table2Args{
|
||||
type_type,
|
||||
type_,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,6 @@ pub mod keyword_test {
|
||||
pub use self::keywords_in_union_generated::*;
|
||||
mod keywords_in_table_generated;
|
||||
pub use self::keywords_in_table_generated::*;
|
||||
mod table_2_generated;
|
||||
pub use self::table_2_generated::*;
|
||||
} // keyword_test
|
||||
|
||||
Reference in New Issue
Block a user