diff options
author | mustiikhalil <mustii@mmk.one> | 2020-10-06 19:00:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-06 19:00:14 +0300 |
commit | 5975658ebdfd55bb83080f8c8bee0ee7bbef633e (patch) | |
tree | 55c52d032bffc2483cb97cdb3dc73b843ddab9e1 /src | |
parent | 5d3cf440e50186bf6a1e841038ac887c2da06141 (diff) | |
download | flatbuffers-5975658ebdfd55bb83080f8c8bee0ee7bbef633e.tar.gz flatbuffers-5975658ebdfd55bb83080f8c8bee0ee7bbef633e.tar.bz2 flatbuffers-5975658ebdfd55bb83080f8c8bee0ee7bbef633e.zip |
Enables optional enums in swift (#6160)
[Swift] Enables optional enums
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_gen_swift.cpp | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index cc8a4d98..6116d459 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -520,15 +520,20 @@ class SwiftGenerator : public BaseGenerator { if (IsScalar(field.value.type.base_type) && !IsBool(field.value.type.base_type)) { std::string is_enum = IsEnum(field.value.type) ? ".rawValue" : ""; + std::string optional_enum = + IsEnum(field.value.type) ? ("?" + is_enum) : ""; code_ += "{{VALUETYPE}}" + builder_string + "fbb.add(element: {{VALUENAME}}\\"; - code_ += field.optional ? "\\" : (is_enum + ", def: {{CONSTANT}}\\"); + code_ += field.optional ? (optional_enum + "\\") + : (is_enum + ", def: {{CONSTANT}}\\"); code_ += ", at: {{TABLEOFFSET}}.{{OFFSET}}.p) }"; - auto default_value = IsEnum(field.value.type) ? GenEnumDefaultValue(field) - : field.value.constant; + auto default_value = + IsEnum(field.value.type) + ? (field.optional ? "nil" : GenEnumDefaultValue(field)) + : field.value.constant; create_func_header.push_back("" + name + ": " + nullable_type + " = " + (field.optional ? "nil" : default_value)); return; @@ -632,7 +637,7 @@ class SwiftGenerator : public BaseGenerator { } if (IsEnum(field.value.type)) { - auto default_value = GenEnumDefaultValue(field); + auto default_value = field.optional ? "nil" : GenEnumDefaultValue(field); code_.SetValue("BASEVALUE", GenTypeBasic(field.value.type, false)); code_ += GenReaderMainBody(optional) + "\\"; code_ += GenOffset() + "return o == 0 ? " + default_value + " : " + |