[Rust] Ensure unions are referenced with the correct path (#6422)

* Add codegen test for namespaced unions

* [Rust] Handle cross-namespace union use

* [Rust] Test namespace handling

* [Rust] Drop trailing whitespace in codegen

* [Rust] Set flags in generate_code.bat to match .sh

* [C#] Add additional namespace test file
This commit is contained in:
William Bain
2021-01-25 12:33:47 -05:00
committed by GitHub
parent e581013e3d
commit efcbdc7698
32 changed files with 1448 additions and 57 deletions

View File

@@ -0,0 +1,77 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace NamespaceA.NamespaceB
{
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum UnionInNestedNS : byte
{
NONE = 0,
TableInNestedNS = 1,
};
public class UnionInNestedNSUnion {
public UnionInNestedNS Type { get; set; }
public object Value { get; set; }
public UnionInNestedNSUnion() {
this.Type = UnionInNestedNS.NONE;
this.Value = null;
}
public T As<T>() where T : class { return this.Value as T; }
public NamespaceA.NamespaceB.TableInNestedNST AsTableInNestedNS() { return this.As<NamespaceA.NamespaceB.TableInNestedNST>(); }
public static int Pack(FlatBuffers.FlatBufferBuilder builder, UnionInNestedNSUnion _o) {
switch (_o.Type) {
default: return 0;
case UnionInNestedNS.TableInNestedNS: return NamespaceA.NamespaceB.TableInNestedNS.Pack(builder, _o.AsTableInNestedNS()).Value;
}
}
}
public class UnionInNestedNSUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(UnionInNestedNSUnion) || objectType == typeof(System.Collections.Generic.List<UnionInNestedNSUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<UnionInNestedNSUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as UnionInNestedNSUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, UnionInNestedNSUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<UnionInNestedNSUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as UnionInNestedNSUnion, serializer);
}
}
public UnionInNestedNSUnion ReadJson(Newtonsoft.Json.JsonReader reader, UnionInNestedNSUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case UnionInNestedNS.TableInNestedNS: _o.Value = serializer.Deserialize<NamespaceA.NamespaceB.TableInNestedNST>(reader); break;
}
return _o;
}
}
}

View File

@@ -0,0 +1,36 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package NamespaceB
import (
"strconv"
flatbuffers "github.com/google/flatbuffers/go"
NamespaceA__NamespaceB "NamespaceA/NamespaceB"
)
type UnionInNestedNST struct {
Type UnionInNestedNS
Value interface{}
}
func (t *UnionInNestedNST) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil {
return 0
}
switch t.Type {
case UnionInNestedNSTableInNestedNS:
return t.Value.(*NamespaceA__NamespaceB.TableInNestedNST).Pack(builder)
}
return 0
}
func (rcv UnionInNestedNS) UnPack(table flatbuffers.Table) *UnionInNestedNST {
switch rcv {
case UnionInNestedNSTableInNestedNS:
x := TableInNestedNS{_tab: table}
return &NamespaceA__NamespaceB.UnionInNestedNST{ Type: UnionInNestedNSTableInNestedNS, Value: x.UnPack() }
}
return nil
}

View File

@@ -0,0 +1,14 @@
// automatically generated by the FlatBuffers compiler, do not modify
package NamespaceA.NamespaceB;
public final class UnionInNestedNS {
private UnionInNestedNS() { }
public static final byte NONE = 0;
public static final byte TableInNestedNS = 1;
public static final String[] names = { "NONE", "TableInNestedNS", };
public static String name(int e) { return names[e]; }
}

View File

@@ -0,0 +1,14 @@
// automatically generated by the FlatBuffers compiler, do not modify
package NamespaceA.NamespaceB
@Suppress("unused")
@ExperimentalUnsignedTypes
class UnionInNestedNS private constructor() {
companion object {
const val NONE: UByte = 0u
const val TableInNestedNS: UByte = 1u
val names : Array<String> = arrayOf("NONE", "TableInNestedNS")
fun name(e: Int) : String = names[e]
}
}

View File

@@ -0,0 +1,10 @@
-- automatically generated by the FlatBuffers compiler, do not modify
-- namespace: NamespaceB
local UnionInNestedNS = {
NONE = 0,
TableInNestedNS = 1,
}
return UnionInNestedNS -- return the module

View File

@@ -0,0 +1,23 @@
<?php
// automatically generated by the FlatBuffers compiler, do not modify
namespace NamespaceA\NamespaceB;
class UnionInNestedNS
{
const NONE = 0;
const TableInNestedNS = 1;
private static $names = array(
UnionInNestedNS::NONE=>"NONE",
UnionInNestedNS::TableInNestedNS=>"TableInNestedNS",
);
public static function Name($e)
{
if (!isset(self::$names[$e])) {
throw new \Exception();
}
return self::$names[$e];
}
}

View File

@@ -0,0 +1,12 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: NamespaceB
def UnionInNestedNSCreator(unionType, table):
from flatbuffers.table import Table
if not isinstance(table, Table):
return None
if unionType == UnionInNestedNS().TableInNestedNS:
return TableInNestedNST.InitFromBuf(table.Bytes, table.Pos)
return None