adds code gen for optional scalars in swift (#6038)

Adds swift test code

Replaces if statments

Adds swift to supported languages for optionals

Moved std::string to auto

Adds nullable scalars support in object api
This commit is contained in:
mustiikhalil
2020-07-25 17:59:34 +03:00
committed by GitHub
parent 82fac326c5
commit d76e93f277
11 changed files with 365 additions and 126 deletions

View File

@@ -439,14 +439,16 @@ class SwiftGenerator : public BaseGenerator {
code_ += "; return end }";
if (should_generate_create) {
code_ +=
"public static func create{{STRUCTNAME}}(_ fbb: inout "
"FlatBufferBuilder,";
code_ += "public static func create{{STRUCTNAME}}(";
Indent();
code_ += "_ fbb: inout FlatBufferBuilder,";
for (auto it = create_func_header.begin(); it < create_func_header.end();
++it) {
code_ += *it + "\\";
if (it < create_func_header.end() - 1) code_ += ",";
}
code_ += "";
Outdent();
code_ += ") -> Offset<UOffset> {";
Indent();
code_ += "let __start = {{STRUCTNAME}}.start{{STRUCTNAME}}(&fbb)";
@@ -491,8 +493,9 @@ class SwiftGenerator : public BaseGenerator {
auto &create_func_header = *create_header;
auto name = Name(field);
auto type = GenType(field.value.type);
auto nullable_type = (field.nullable ? type + "?" : type);
code_.SetValue("VALUENAME", name);
code_.SetValue("VALUETYPE", type);
code_.SetValue("VALUETYPE", nullable_type);
code_.SetValue("OFFSET", name);
code_.SetValue("CONSTANT", field.value.constant);
std::string check_if_vector =
@@ -507,26 +510,33 @@ 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" : "";
code_ +=
"{{VALUETYPE}}" + builder_string + "fbb.add(element: {{VALUENAME}}\\";
code_ += field.nullable ? "\\" : (is_enum + ", def: {{CONSTANT}}\\");
code_ += ", at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
auto default_value = IsEnum(field.value.type) ? GenEnumDefaultValue(field)
: field.value.constant;
auto is_enum = IsEnum(field.value.type) ? ".rawValue" : "";
code_ += "{{VALUETYPE}}" + builder_string +
"fbb.add(element: {{VALUENAME}}" + is_enum +
", def: {{CONSTANT}}, at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
create_func_header.push_back("" + name + ": " + type + " = " +
default_value);
create_func_header.push_back("" + name + ": " + nullable_type + " = " +
(field.nullable ? "nil" : default_value));
return;
}
if (IsBool(field.value.type.base_type)) {
std::string default_value =
"0" == field.value.constant ? "false" : "true";
code_.SetValue("VALUETYPE", "Bool");
code_.SetValue("CONSTANT", default_value);
code_.SetValue("VALUETYPE", field.nullable ? "Bool?" : "Bool");
code_ += "{{VALUETYPE}}" + builder_string +
"fbb.add(element: {{VALUENAME}}, def: {{CONSTANT}}, at: "
"{{TABLEOFFSET}}.{{OFFSET}}.p) }";
create_func_header.push_back(name + ": " + type + " = " + default_value);
"fbb.add(element: {{VALUENAME}},\\";
code_ += field.nullable ? "\\" : " def: {{CONSTANT}},";
code_ += " at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
create_func_header.push_back(name + ": " + nullable_type + " = " +
(field.nullable ? "nil" : default_value));
return;
}
@@ -570,11 +580,13 @@ class SwiftGenerator : public BaseGenerator {
code_.SetValue("VALUETYPE", type);
code_.SetValue("OFFSET", name);
code_.SetValue("CONSTANT", field.value.constant);
std::string const_string = "return o == 0 ? {{CONSTANT}} : ";
std::string nullable = field.nullable ? "nil" : "{{CONSTANT}}";
std::string optional = field.nullable ? "?" : "";
auto const_string = "return o == 0 ? " + nullable + " : ";
GenComment(field.doc_comment);
if (IsScalar(field.value.type.base_type) && !IsEnum(field.value.type) &&
!IsBool(field.value.type.base_type)) {
code_ += GenReaderMainBody() + GenOffset() + const_string +
code_ += GenReaderMainBody(optional) + GenOffset() + const_string +
GenReader("VALUETYPE", "o") + " }";
if (parser_.opts.mutable_buffer) code_ += GenMutate("o", GenOffset());
return;
@@ -585,7 +597,7 @@ class SwiftGenerator : public BaseGenerator {
"0" == field.value.constant ? "false" : "true";
code_.SetValue("CONSTANT", default_value);
code_.SetValue("VALUETYPE", "Bool");
code_ += GenReaderMainBody() + "\\";
code_ += GenReaderMainBody(optional) + "\\";
code_.SetValue("VALUETYPE", "Byte");
code_ += GenOffset() + "return o == 0 ? {{CONSTANT}} : 0 != " +
GenReader("VALUETYPE", "o") + " }";
@@ -596,7 +608,7 @@ class SwiftGenerator : public BaseGenerator {
if (IsEnum(field.value.type)) {
auto default_value = GenEnumDefaultValue(field);
code_.SetValue("BASEVALUE", GenTypeBasic(field.value.type, false));
code_ += GenReaderMainBody() + "\\";
code_ += GenReaderMainBody(optional) + "\\";
code_ += GenOffset() + "return o == 0 ? " + default_value + " : " +
GenEnumConstructor("o") + "?? " + default_value + " }";
if (parser_.opts.mutable_buffer && !IsUnion(field.value.type))
@@ -1063,11 +1075,12 @@ class SwiftGenerator : public BaseGenerator {
}
default: {
buffer_constructor.push_back(name + " = _t." + name);
std::string nullable = field.nullable ? "?" : "";
if (IsScalar(field.value.type.base_type) &&
!IsBool(field.value.type.base_type) && !IsEnum(field.value.type)) {
code_ += "var {{VALUENAME}}: {{VALUETYPE}}";
base_constructor.push_back(name + " = " + field.value.constant);
code_ += "var {{VALUENAME}}: {{VALUETYPE}}" + nullable;
if (!field.nullable)
base_constructor.push_back(name + " = " + field.value.constant);
break;
}
@@ -1081,10 +1094,11 @@ class SwiftGenerator : public BaseGenerator {
}
if (IsBool(field.value.type.base_type)) {
code_ += "var {{VALUENAME}}: Bool";
code_ += "var {{VALUENAME}}: Bool" + nullable;
std::string default_value =
"0" == field.value.constant ? "false" : "true";
base_constructor.push_back(name + " = " + default_value);
if (!field.nullable)
base_constructor.push_back(name + " = " + default_value);
}
}
}

View File

@@ -2258,7 +2258,7 @@ CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
bool Parser::SupportsNullableScalars() const {
return !(opts.lang_to_generate & ~IDLOptions::kRust);
return !(opts.lang_to_generate & ~(IDLOptions::kRust | IDLOptions::kSwift));
}
bool Parser::SupportsAdvancedUnionFeatures() const {