mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 20:48:59 +00:00
BugFix: Optional enum when it is null (#6835)
* Test to make sure optional enum is written properly * Handle optional enum codegen: when cast optional enum add `?` * Run `tests/generate_code.sh` to generate code from schema * Fix type casting in case of CreateXXXTypeVector * Reason why vector's type is not optional
This commit is contained in:
@@ -309,17 +309,17 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// would be cast down to int before being put onto the buffer. In C#, one cast
|
||||
// directly cast an Enum to its underlying type, which is essential before
|
||||
// putting it onto the buffer.
|
||||
std::string SourceCast(const Type &type) const {
|
||||
std::string SourceCast(const Type &type, const bool isOptional=false) const {
|
||||
if (IsSeries(type)) {
|
||||
return SourceCast(type.VectorType());
|
||||
} else {
|
||||
if (IsEnum(type)) return "(" + GenTypeBasic(type, false) + ")";
|
||||
if (IsEnum(type)) return "(" + GenTypeBasic(type, false) + (isOptional ? "?": "") + ")";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string SourceCastBasic(const Type &type) const {
|
||||
return IsScalar(type.base_type) ? SourceCast(type) : "";
|
||||
std::string SourceCastBasic(const Type &type, const bool isOptional) const {
|
||||
return IsScalar(type.base_type) ? SourceCast(type, isOptional) : "";
|
||||
}
|
||||
|
||||
std::string GenEnumDefaultValue(const FieldDef &field) const {
|
||||
@@ -1191,7 +1191,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code += " " + EscapeKeyword(argname) + ") { builder.Add";
|
||||
code += GenMethod(field.value.type) + "(";
|
||||
code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
|
||||
code += SourceCastBasic(field.value.type);
|
||||
code += SourceCastBasic(field.value.type, field.IsScalarOptional());
|
||||
code += EscapeKeyword(argname);
|
||||
if (!IsScalar(field.value.type.base_type) &&
|
||||
field.value.type.base_type != BASE_TYPE_UNION) {
|
||||
@@ -1225,7 +1225,9 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code += "Add";
|
||||
code += GenMethod(vector_type);
|
||||
code += "(";
|
||||
code += SourceCastBasic(vector_type);
|
||||
// At the moment there is no support of the type Vector with optional enum,
|
||||
// e.g. if we have enum type SomeEnum there is no way to define `SomeEmum?[] enums` in FlatBuffer schema, so isOptional = false
|
||||
code += SourceCastBasic(vector_type, false);
|
||||
code += "data[i]";
|
||||
if (vector_type.base_type == BASE_TYPE_STRUCT ||
|
||||
IsString(vector_type))
|
||||
|
||||
Reference in New Issue
Block a user