summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2020-10-20 08:00:31 -0700
committerGitHub <noreply@github.com>2020-10-20 08:00:31 -0700
commitfe8e3c7e5d27a4e7680bc2ba9c1c657832903848 (patch)
treeaa50605ec74561e2a4f956dadf57b7bb33abcb08 /src
parent8f6fa4b71549c9dd95565a839ff9817ec65b26a8 (diff)
downloadflatbuffers-fe8e3c7e5d27a4e7680bc2ba9c1c657832903848.tar.gz
flatbuffers-fe8e3c7e5d27a4e7680bc2ba9c1c657832903848.tar.bz2
flatbuffers-fe8e3c7e5d27a4e7680bc2ba9c1c657832903848.zip
Mass Refactoring to use `IsString` and other BASE_TYPE helpers (#6193)
* Updated SupportsAdvancedUnionFeatures to look out for string * Mass refactoring to use BASE_TYPE helper functions. Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_cpp.cpp36
-rw-r--r--src/idl_gen_csharp.cpp38
-rw-r--r--src/idl_gen_dart.cpp8
-rw-r--r--src/idl_gen_go.cpp24
-rw-r--r--src/idl_gen_java.cpp22
-rw-r--r--src/idl_gen_js_ts.cpp35
-rw-r--r--src/idl_gen_json_schema.cpp4
-rw-r--r--src/idl_gen_kotlin.cpp6
-rw-r--r--src/idl_gen_lobster.cpp8
-rw-r--r--src/idl_gen_lua.cpp6
-rw-r--r--src/idl_gen_php.cpp6
-rw-r--r--src/idl_gen_python.cpp11
-rw-r--r--src/idl_gen_rust.cpp4
-rw-r--r--src/idl_gen_swift.cpp30
-rw-r--r--src/idl_parser.cpp15
15 files changed, 125 insertions, 128 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp
index 72f9a88e..a56dad91 100644
--- a/src/idl_gen_cpp.cpp
+++ b/src/idl_gen_cpp.cpp
@@ -885,7 +885,7 @@ class CppGenerator : public BaseGenerator {
return wrap ? WrapInNameSpace(ev.union_type.struct_def->defined_namespace,
name)
: name;
- } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(ev.union_type)) {
return actual_type ? (native_type ? "std::string" : "flatbuffers::String")
: Name(ev);
} else {
@@ -996,8 +996,8 @@ class CppGenerator : public BaseGenerator {
for (auto it = types.begin(); it != types.end(); ++it) {
auto &type = *it;
if (!ts.empty()) ts += ",\n ";
- auto is_vector = type.base_type == BASE_TYPE_VECTOR;
- auto is_array = type.base_type == BASE_TYPE_ARRAY;
+ auto is_vector = IsVector(type);
+ auto is_array = IsArray(type);
auto bt = is_vector || is_array ? type.element : type.base_type;
auto et = IsScalar(bt) || bt == BASE_TYPE_STRING
? bt - BASE_TYPE_UTYPE + ET_UTYPE
@@ -1424,7 +1424,7 @@ class CppGenerator : public BaseGenerator {
code_ += getptr;
code_ += " return verifier.VerifyTable(ptr);";
}
- } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(ev.union_type)) {
code_ += getptr;
code_ += " return verifier.VerifyString(ptr);";
} else {
@@ -1476,7 +1476,7 @@ class CppGenerator : public BaseGenerator {
} else {
code_ += " return ptr->UnPack(resolver);";
}
- } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(ev.union_type)) {
code_ += " return new std::string(ptr->c_str(), ptr->size());";
} else {
FLATBUFFERS_ASSERT(false);
@@ -1508,7 +1508,7 @@ class CppGenerator : public BaseGenerator {
code_ +=
" return Create{{NAME}}(_fbb, ptr, _rehasher).Union();";
}
- } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(ev.union_type)) {
code_ += " return _fbb.CreateString(*ptr).Union();";
} else {
FLATBUFFERS_ASSERT(false);
@@ -1667,10 +1667,10 @@ class CppGenerator : public BaseGenerator {
void GenParam(const FieldDef &field, bool direct, const char *prefix) {
code_.SetValue("PRE", prefix);
code_.SetValue("PARAM_NAME", Name(field));
- if (direct && field.value.type.base_type == BASE_TYPE_STRING) {
+ if (direct && IsString(field.value.type)) {
code_.SetValue("PARAM_TYPE", "const char *");
code_.SetValue("PARAM_VALUE", "nullptr");
- } else if (direct && field.value.type.base_type == BASE_TYPE_VECTOR) {
+ } else if (direct && IsVector(field.value.type)) {
const auto vtype = field.value.type.VectorType();
std::string type;
if (IsStruct(vtype)) {
@@ -1705,7 +1705,7 @@ class CppGenerator : public BaseGenerator {
auto cpp_type = field.attributes.Lookup("cpp_type");
auto full_type =
(cpp_type
- ? (field.value.type.base_type == BASE_TYPE_VECTOR
+ ? (IsVector(field.value.type)
? "std::vector<" +
GenTypeNativePtr(cpp_type->constant, &field,
false) +
@@ -1934,7 +1934,7 @@ class CppGenerator : public BaseGenerator {
// Generate CompareWithValue method for a key field.
void GenKeyFieldMethods(const FieldDef &field) {
FLATBUFFERS_ASSERT(field.key);
- const bool is_string = (field.value.type.base_type == BASE_TYPE_STRING);
+ const bool is_string = (IsString(field.value.type));
code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
if (is_string) {
@@ -2257,7 +2257,7 @@ class CppGenerator : public BaseGenerator {
// that doesn't need alignment code.
std::string GenVectorForceAlign(const FieldDef &field,
const std::string &field_size) {
- FLATBUFFERS_ASSERT(field.value.type.base_type == BASE_TYPE_VECTOR);
+ FLATBUFFERS_ASSERT(IsVector(field.value.type));
// Get the value of the force_align attribute.
const auto *force_align = field.attributes.Lookup("force_align");
const int align = force_align ? atoi(force_align->constant.c_str()) : 1;
@@ -2288,8 +2288,8 @@ class CppGenerator : public BaseGenerator {
if (field.deprecated) continue;
const bool is_scalar = IsScalar(field.value.type.base_type);
const bool is_default_scalar = is_scalar && !field.IsScalarOptional();
- const bool is_string = field.value.type.base_type == BASE_TYPE_STRING;
- const bool is_vector = field.value.type.base_type == BASE_TYPE_VECTOR;
+ const bool is_string = IsString(field.value.type);
+ const bool is_vector = IsVector(field.value.type);
if (is_string || is_vector) { has_string_or_vector_fields = true; }
std::string offset = GenFieldOffsetName(field);
@@ -2418,7 +2418,7 @@ class CppGenerator : public BaseGenerator {
const auto &field = **it;
if (!field.deprecated) {
code_.SetValue("FIELD_NAME", Name(field));
- if (field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type)) {
if (!field.shared) {
code_.SetValue("CREATE_STRING", "CreateString");
} else {
@@ -2427,7 +2427,7 @@ class CppGenerator : public BaseGenerator {
code_ +=
" auto {{FIELD_NAME}}__ = {{FIELD_NAME}} ? "
"_fbb.{{CREATE_STRING}}({{FIELD_NAME}}) : 0;";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(field.value.type)) {
const std::string force_align_code =
GenVectorForceAlign(field, Name(field) + "->size()");
if (!force_align_code.empty()) {
@@ -2462,8 +2462,8 @@ class CppGenerator : public BaseGenerator {
if (!field.deprecated) {
code_.SetValue("FIELD_NAME", Name(field));
code_ += ",\n {{FIELD_NAME}}\\";
- if (field.value.type.base_type == BASE_TYPE_STRING ||
- field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsString(field.value.type) ||
+ IsVector(field.value.type)) {
code_ += "__\\";
}
}
@@ -2915,7 +2915,7 @@ class CppGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) { continue; }
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
const std::string force_align_code =
GenVectorForceAlign(field, "_o->" + Name(field) + ".size()");
if (!force_align_code.empty()) { code_ += " " + force_align_code; }
diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp
index 0dd07d3b..3c1331e3 100644
--- a/src/idl_gen_csharp.cpp
+++ b/src/idl_gen_csharp.cpp
@@ -313,7 +313,7 @@ class CSharpGenerator : public BaseGenerator {
if (!enum_def.is_union) return false;
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &val = **it;
- if (val.union_type.base_type == BASE_TYPE_STRING) { return true; }
+ if (IsString(val.union_type)) { return true; }
}
return false;
}
@@ -486,7 +486,7 @@ class CSharpGenerator : public BaseGenerator {
key_getter += "int tableOffset = Table.";
key_getter += "__indirect(vectorLocation + 4 * (start + middle)";
key_getter += ", bb);\n ";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
key_getter += "int comp = Table.";
key_getter += "CompareStrings(";
key_getter += GenOffsetGetter(key_field);
@@ -501,7 +501,7 @@ class CSharpGenerator : public BaseGenerator {
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) const {
std::string key_getter = "";
auto data_buffer = "builder.DataBuffer";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
key_getter += "Table.CompareStrings(";
key_getter += GenOffsetGetter(key_field, "o1") + ", ";
key_getter += GenOffsetGetter(key_field, "o2") + ", " + data_buffer + ")";
@@ -604,7 +604,7 @@ class CSharpGenerator : public BaseGenerator {
if (!struct_def.fixed &&
(field.value.type.base_type == BASE_TYPE_STRUCT ||
field.value.type.base_type == BASE_TYPE_UNION ||
- (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ (IsVector(field.value.type) &&
(field.value.type.element == BASE_TYPE_STRUCT ||
field.value.type.element == BASE_TYPE_UNION)))) {
optional = "?";
@@ -626,10 +626,10 @@ class CSharpGenerator : public BaseGenerator {
"); return o != 0 ? ");
// Generate the accessors that don't do object reuse.
if (field.value.type.base_type == BASE_TYPE_STRUCT) {
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ } else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_STRUCT) {
} else if (field.value.type.base_type == BASE_TYPE_UNION ||
- (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ (IsVector(field.value.type) &&
field.value.type.VectorType().base_type == BASE_TYPE_UNION)) {
method_start += "<TTable>";
type_name = type_name_dest;
@@ -639,7 +639,7 @@ class CSharpGenerator : public BaseGenerator {
std::string default_cast = "";
// only create default casts for c# scalars or vectors of scalars
if ((IsScalar(field.value.type.base_type) ||
- (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ (IsVector(field.value.type) &&
IsScalar(field.value.type.element)))) {
// For scalars, default value will be returned by GetDefaultValue().
// If the scalar is an enum, GetDefaultValue() returns an actual c# enum
@@ -647,7 +647,7 @@ class CSharpGenerator : public BaseGenerator {
// elements of vectors are integer literals ("0") and are still casted
// for clarity.
if (field.value.type.enum_def == nullptr ||
- field.value.type.base_type == BASE_TYPE_VECTOR) {
+ IsVector(field.value.type)) {
default_cast = "(" + type_name_dest + ")";
}
}
@@ -762,7 +762,7 @@ class CSharpGenerator : public BaseGenerator {
}
code += member_suffix;
code += "}\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code += " public int " + MakeCamel(field.name, true);
code += "Length";
code += " { get";
@@ -793,9 +793,9 @@ class CSharpGenerator : public BaseGenerator {
}
}
// Generate a ByteBuffer accessor for strings & vectors of scalars.
- if ((field.value.type.base_type == BASE_TYPE_VECTOR &&
+ if ((IsVector(field.value.type) &&
IsScalar(field.value.type.VectorType().base_type)) ||
- field.value.type.base_type == BASE_TYPE_STRING) {
+ IsString(field.value.type)) {
code += "#if ENABLE_SPAN_T\n";
code += " public Span<" + GenTypeBasic(field.value.type.VectorType()) +
"> Get";
@@ -1046,7 +1046,7 @@ class CSharpGenerator : public BaseGenerator {
code += ", ";
code += GenDefaultValue(field, false);
code += "); }\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
auto vector_type = field.value.type.VectorType();
auto alignment = InlineAlignment(vector_type);
auto elem_size = InlineSize(vector_type);
@@ -1068,8 +1068,8 @@ class CSharpGenerator : public BaseGenerator {
code += "(";
code += SourceCastBasic(vector_type);
code += "data[i]";
- if ((vector_type.base_type == BASE_TYPE_STRUCT ||
- vector_type.base_type == BASE_TYPE_STRING))
+ if (vector_type.base_type == BASE_TYPE_STRUCT ||
+ IsString(vector_type))
code += ".Value";
code += "); return ";
code += "builder.EndVector(); }\n";
@@ -1147,7 +1147,7 @@ class CSharpGenerator : public BaseGenerator {
code += "int vectorLocation, ";
code += GenTypeGet(key_field->value.type);
code += " key, ByteBuffer bb) {\n";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
code += " byte[] byteKey = ";
code += "System.Text.Encoding.UTF8.GetBytes(key);\n";
}
@@ -1291,7 +1291,7 @@ class CSharpGenerator : public BaseGenerator {
code += " default: return 0;\n";
} else {
code += " case " + enum_def.name + "." + ev.name + ": return ";
- if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ if (IsString(ev.union_type)) {
code += "builder.CreateString(_o.As" + ev.name + "()).Value;\n";
} else {
code += GenTypeGet(ev.union_type) + ".Pack(builder, _o.As" + ev.name +
@@ -1425,7 +1425,7 @@ class CSharpGenerator : public BaseGenerator {
code += indent + " case " + WrapInNameSpace(enum_def) + "." + ev.name +
":\n";
code += indent + " " + varialbe_name + ".Value = this." + camel_name;
- if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ if (IsString(ev.union_type)) {
code += "AsString" + func_suffix + ";\n";
} else {
code += "<" + GenTypeGet(ev.union_type) + ">" + func_suffix;
@@ -1927,7 +1927,7 @@ class CSharpGenerator : public BaseGenerator {
auto utype_name = WrapInNameSpace(*field.value.type.enum_def);
code +=
" [Newtonsoft.Json.JsonProperty(\"" + field.name + "_type\")]\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code += " private " + utype_name + "[] " + camel_name + "Type {\n";
code += " get {\n";
code += " if (this." + camel_name + " == null) return null;\n";
@@ -1966,7 +1966,7 @@ class CSharpGenerator : public BaseGenerator {
code += " [Newtonsoft.Json.JsonProperty(\"" + field.name + "\")]\n";
if (IsUnion(field.value.type)) {
auto union_name =
- (field.value.type.base_type == BASE_TYPE_VECTOR)
+ (IsVector(field.value.type))
? GenTypeGet_ObjectAPI(field.value.type.VectorType(), opts)
: type_name;
code += " [Newtonsoft.Json.JsonConverter(typeof(" + union_name +
diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp
index 80fd5834..eec05a71 100644
--- a/src/idl_gen_dart.cpp
+++ b/src/idl_gen_dart.cpp
@@ -330,13 +330,13 @@ class DartGenerator : public BaseGenerator {
bool parent_is_vector = false) {
if (type.base_type == BASE_TYPE_BOOL) {
return "const " + _kFb + ".BoolReader()";
- } else if (type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(type)) {
return "const " + _kFb + ".ListReader<" +
GenDartTypeName(type.VectorType(), current_namespace, def) + ">(" +
GenReaderTypeName(type.VectorType(), current_namespace, def,
true) +
")";
- } else if (type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(type)) {
return "const " + _kFb + ".StringReader()";
}
if (IsScalar(type.base_type)) {
@@ -808,7 +808,7 @@ class DartGenerator : public BaseGenerator {
continue;
code += " final int " + MakeCamel(field.name, false) + "Offset";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code +=
" = _" + MakeCamel(field.name, false) + "?.isNotEmpty == true\n";
code += " ? fbBuilder.writeList";
@@ -832,7 +832,7 @@ class DartGenerator : public BaseGenerator {
code += ")";
}
code += "\n : null;\n";
- } else if (field.value.type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(field.value.type)) {
code += " = fbBuilder.writeString(_" + MakeCamel(field.name, false) +
");\n";
} else {
diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp
index a1a277ca..95e9a551 100644
--- a/src/idl_gen_go.cpp
+++ b/src/idl_gen_go.cpp
@@ -458,7 +458,7 @@ class GoGenerator : public BaseGenerator {
"(a + flatbuffers.UOffsetT(j*" +
NumToString(InlineSize(vectortype)) + "))");
code += "\n\t}\n";
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code += "\treturn nil\n";
} else if (vectortype.base_type == BASE_TYPE_BOOL) {
code += "\treturn false\n";
@@ -640,7 +640,7 @@ class GoGenerator : public BaseGenerator {
default: FLATBUFFERS_ASSERT(0);
}
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
GetVectorLen(struct_def, field, code_ptr);
if (field.value.type.element == BASE_TYPE_UCHAR) {
GetUByteSlice(struct_def, field, code_ptr);
@@ -708,7 +708,7 @@ class GoGenerator : public BaseGenerator {
} else {
MutateScalarFieldOfTable(struct_def, field, code_ptr);
}
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(field.value.type)) {
if (IsScalar(field.value.type.element)) {
MutateElementOfVectorOfNonStruct(struct_def, field, code_ptr);
}
@@ -726,7 +726,7 @@ class GoGenerator : public BaseGenerator {
auto offset = it - struct_def.fields.vec.begin();
BuildFieldOfTable(struct_def, field, offset, code_ptr);
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
BuildVectorOfTable(struct_def, field, code_ptr);
}
}
@@ -869,10 +869,10 @@ class GoGenerator : public BaseGenerator {
std::string offset = MakeCamel(field.name, false) + "Offset";
- if (field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type)) {
code += "\t" + offset + " := builder.CreateString(t." +
MakeCamel(field.name) + ")\n";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ } else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_UCHAR &&
field.value.type.enum_def == nullptr) {
code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n";
@@ -880,7 +880,7 @@ class GoGenerator : public BaseGenerator {
code += "\t\t" + offset + " = builder.CreateByteString(t." +
MakeCamel(field.name) + ")\n";
code += "\t}\n";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(field.value.type)) {
code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n";
code += "\tif t." + MakeCamel(field.name) + " != nil {\n";
std::string length = MakeCamel(field.name, false) + "Length";
@@ -984,15 +984,15 @@ class GoGenerator : public BaseGenerator {
continue;
code +=
"\tt." + field_name_camel + " = rcv." + field_name_camel + "()\n";
- } else if (field.value.type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(field.value.type)) {
code += "\tt." + field_name_camel + " = string(rcv." +
field_name_camel + "())\n";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ } else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_UCHAR &&
field.value.type.enum_def == nullptr) {
code += "\tt." + field_name_camel + " = rcv." + field_name_camel +
"Bytes()\n";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(field.value.type)) {
code += "\t" + length + " := rcv." + field_name_camel + "Length()\n";
code += "\tt." + field_name_camel + " = make(" +
NativeType(field.value.type) + ", " + length + ")\n";
@@ -1225,9 +1225,9 @@ class GoGenerator : public BaseGenerator {
} else {
return GetEnumTypeName(*type.enum_def);
}
- } else if (type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(type)) {
return "string";
- } else if (type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(type)) {
return "[]" + NativeType(type.VectorType());
} else if (type.base_type == BASE_TYPE_STRUCT) {
return "*" + WrapInNameSpaceAndTrack(type.struct_def->defined_namespace,
diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp
index 9679a1aa..e2ce1151 100644
--- a/src/idl_gen_java.cpp
+++ b/src/idl_gen_java.cpp
@@ -526,7 +526,7 @@ class JavaGenerator : public BaseGenerator {
key_getter += "int tableOffset = ";
key_getter += "__indirect(vectorLocation + 4 * (start + middle)";
key_getter += ", bb);\n ";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
key_getter += "int comp = ";
key_getter += "compareStrings(";
key_getter += GenOffsetGetter(key_field);
@@ -543,7 +543,7 @@ class JavaGenerator : public BaseGenerator {
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) const {
std::string key_getter = "";
auto data_buffer = "_bb";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
key_getter += " return ";
key_getter += "";
key_getter += "compareStrings(";
@@ -669,7 +669,7 @@ class JavaGenerator : public BaseGenerator {
code += MakeCamel(field.name, false);
code += "(new ";
code += type_name + "()); }\n";
- } else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
+ } else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_STRUCT) {
// Accessors for vectors of structs also take accessor objects, this
// generates a variant without that argument.
@@ -772,7 +772,7 @@ class JavaGenerator : public BaseGenerator {
}
code += member_suffix;
code += "}\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code += " public int " + MakeCamel(field.name, false);
code += "Length";
code += "()";
@@ -813,7 +813,7 @@ class JavaGenerator : public BaseGenerator {
}
}
// Generate the accessors for vector of structs with vector access object
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
std::string vector_type_name;
const auto &element_base_type = field.value.type.VectorType().base_type;
if (IsScalar(element_base_type)) {
@@ -842,15 +842,15 @@ class JavaGenerator : public BaseGenerator {
code += "bb) : null" + member_suffix + "}\n";
}
// Generate a ByteBuffer accessor for strings & vectors of scalars.
- if ((field.value.type.base_type == BASE_TYPE_VECTOR &&
+ if ((IsVector(field.value.type) &&
IsScalar(field.value.type.VectorType().base_type)) ||
- field.value.type.base_type == BASE_TYPE_STRING) {
+ IsString(field.value.type)) {
code += " public ByteBuffer ";
code += MakeCamel(field.name, false);
code += "AsByteBuffer() { return ";
code += "__vector_as_bytebuffer(";
code += NumToString(field.value.offset) + ", ";
- code += NumToString(field.value.type.base_type == BASE_TYPE_STRING
+ code += NumToString(IsString(field.value.type)
? 1
: InlineSize(field.value.type.VectorType()));
code += "); }\n";
@@ -859,7 +859,7 @@ class JavaGenerator : public BaseGenerator {
code += "InByteBuffer(ByteBuffer _bb) { return ";
code += "__vector_in_bytebuffer(_bb, ";
code += NumToString(field.value.offset) + ", ";
- code += NumToString(field.value.type.base_type == BASE_TYPE_STRING
+ code += NumToString(IsString(field.value.type)
? 1
: InlineSize(field.value.type.VectorType()));
code += "); }\n";
@@ -1036,7 +1036,7 @@ class JavaGenerator : public BaseGenerator {
code += SourceCastBasic(field.value.type);
code += GenDefaultValue(field);
code += "); }\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
auto vector_type = field.value.type.VectorType();
auto alignment = InlineAlignment(vector_type);
auto elem_size = InlineSize(vector_type);
@@ -1132,7 +1132,7 @@ class JavaGenerator : public BaseGenerator {
code += "int vectorLocation, ";
code += GenTypeNameDest(key_field->value.type);
code += " key, ByteBuffer bb) {\n";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
code += " byte[] byteKey = ";
code += "key.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n";
}
diff --git a/src/idl_gen_js_ts.cpp b/src/idl_gen_js_ts.cpp
index 71b68b85..cd3b9221 100644
--- a/src/idl_gen_js_ts.cpp
+++ b/src/idl_gen_js_ts.cpp
@@ -491,10 +491,9 @@ class JsTsGenerator : public BaseGenerator {
std::string GenTypeName(const Type &type, bool input,
bool allowNull = false) {
if (!input) {
- if (type.base_type == BASE_TYPE_STRING ||
- type.base_type == BASE_TYPE_STRUCT) {
+ if (IsString(type) || type.base_type == BASE_TYPE_STRUCT) {
std::string name;
- if (type.base_type == BASE_TYPE_STRING) {
+ if (IsString(type)) {
name = "string|Uint8Array";
} else {
name = WrapInNameSpace(*type.struct_def);
@@ -714,7 +713,7 @@ class JsTsGenerator : public BaseGenerator {
return std::any_of(union_enum.Vals().begin(), union_enum.Vals().end(),
[](const EnumVal *ev) {
return !(ev->IsZero()) &&
- (ev->union_type.base_type == BASE_TYPE_STRING);
+ (IsString(ev->union_type));
});
}
@@ -733,7 +732,7 @@ class JsTsGenerator : public BaseGenerator {
if (ev.IsZero()) { continue; }
std::string type = "";
- if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ if (IsString(ev.union_type)) {
type = "string"; // no need to wrap string type in namespace
} else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
if (!parser_.opts.generate_all) {
@@ -767,7 +766,7 @@ class JsTsGenerator : public BaseGenerator {
if (ev.IsZero()) { continue; }
std::string type = "";
- if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ if (IsString(ev.union_type)) {
type = "string"; // no need to wrap string type in namespace
} else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
type = GenPrefixedTypeName(
@@ -830,7 +829,7 @@ class JsTsGenerator : public BaseGenerator {
ret += " case '" + ev.name + "': ";
- if (ev.union_type.base_type == BASE_TYPE_STRING) {
+ if (IsString(ev.union_type)) {
ret += "return " + accessor_str + "'') as string;";
} else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
const auto type = GenPrefixedTypeName(
@@ -1044,7 +1043,7 @@ class JsTsGenerator : public BaseGenerator {
// Emit a scalar field
if (IsScalar(field.value.type.base_type) ||
- field.value.type.base_type == BASE_TYPE_STRING) {
+ IsString(field.value.type)) {
if (field.value.type.enum_def) {
if (!parser_.opts.generate_all) {
imported_files.insert(field.value.type.enum_def->file);
@@ -1413,10 +1412,10 @@ class JsTsGenerator : public BaseGenerator {
// Emit a scalar field
if (IsScalar(field.value.type.base_type) ||
- field.value.type.base_type == BASE_TYPE_STRING) {
+ IsString(field.value.type)) {
GenDocComment(
field.doc_comment, code_ptr,
- std::string(field.value.type.base_type == BASE_TYPE_STRING
+ std::string(IsString(field.value.type)
? GenTypeAnnotation(kParam, "flatbuffers.Encoding=",
"optionalEncoding")
: "") +
@@ -1425,7 +1424,7 @@ class JsTsGenerator : public BaseGenerator {
"", false));
if (lang_.language == IDLOptions::kTs) {
std::string prefix = MakeCamel(field.name, false) + "(";
- if (field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type)) {
code += prefix + "):string|null\n";
code += prefix + "optionalEncoding:flatbuffers.Encoding" +
"):" + GenTypeName(field.value.type, false, true) + "\n";
@@ -1449,7 +1448,7 @@ class JsTsGenerator : public BaseGenerator {
} else {
code += object_name + ".prototype." + MakeCamel(field.name, false);
code += " = function(";
- if (field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type)) {
code += "optionalEncoding";
}
code += ") {\n";
@@ -1463,7 +1462,7 @@ class JsTsGenerator : public BaseGenerator {
";\n";
} else {
std::string index = "this.bb_pos + offset";
- if (field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type)) {
index += ", optionalEncoding";
}
code += offset_prefix +
@@ -1569,7 +1568,7 @@ class JsTsGenerator : public BaseGenerator {
if (!parser_.opts.generate_all) {
imported_files.insert(vectortype.struct_def->file);
}
- } else if (vectortype.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(vectortype)) {
code += prefix + "):string\n";
code += prefix + ",optionalEncoding:flatbuffers.Encoding" +
"):" + vectortypename + "\n";
@@ -1588,7 +1587,7 @@ class JsTsGenerator : public BaseGenerator {
code += " = function(index";
if (vectortype.base_type == BASE_TYPE_STRUCT || is_union) {
code += ", obj";
- } else if (vectortype.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(vectortype)) {
code += ", optionalEncoding";
}
code += ") {\n";
@@ -1605,7 +1604,7 @@ class JsTsGenerator : public BaseGenerator {
} else {
if (is_union) {
index = "obj, " + index;
- } else if (vectortype.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(vectortype)) {
index += ", optionalEncoding";
}
code += offset_prefix + GenGetter(vectortype, "(" + index + ")");
@@ -1732,7 +1731,7 @@ class JsTsGenerator : public BaseGenerator {
}
// Emit vector helpers
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
// Emit a length helper
GenDocComment(code_ptr,
GenTypeAnnotation(kReturns, "number", "", false));
@@ -1884,7 +1883,7 @@ class JsTsGenerator : public BaseGenerator {
}
code += ");\n};\n\n";
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
auto vector_type = field.value.type.VectorType();
auto alignment = InlineAlignment(vector_type);
auto elem_size = InlineSize(vector_type);
diff --git a/src/idl_gen_json_schema.cpp b/src/idl_gen_json_schema.cpp
index 1ffd4a68..a321b892 100644
--- a/src/idl_gen_json_schema.cpp
+++ b/src/idl_gen_json_schema.cpp
@@ -87,7 +87,7 @@ std::string GenType(BaseType type) {
std::string GenBaseType(const Type &type) {
if (type.struct_def != nullptr) { return GenTypeRef(type.struct_def); }
if (type.enum_def != nullptr) { return GenTypeRef(type.enum_def); }
- if (type.base_type == BASE_TYPE_ARRAY || type.base_type == BASE_TYPE_VECTOR) {
+ if (IsArray(type) || IsVector(type)) {
return "\"type\" : \"array\", \"items\" : {" + GenType(type.element) + "}";
}
return GenType(type.base_type);
@@ -206,7 +206,7 @@ class JsonSchemaGenerator : public BaseGenerator {
std::string arrayInfo = "";
if (IsArray(property->value.type)) {
arrayInfo = "," + NewLine() + Indent(8) + "\"minItems\": " +
- NumToString(property->value.type.fixed_length) +
+ NumToString(property->value.type.fixed_length) +
"," + NewLine() + Indent(8) + "\"maxItems\": " +
NumToString(property->value.type.fixed_length);
}
diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp
index 80416d5e..ed85b2cb 100644
--- a/src/idl_gen_kotlin.cpp
+++ b/src/idl_gen_kotlin.cpp
@@ -513,7 +513,7 @@ class KotlinGenerator : public BaseGenerator {
if (field.key) key_field = &field;
GenerateAddField(NumToString(field_pos), field, writer, options);
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
auto vector_type = field.value.type.VectorType();
if (!IsStruct(vector_type)) {
GenerateCreateVectorField(field, writer, options);
@@ -571,7 +571,7 @@ class KotlinGenerator : public BaseGenerator {
writer +=
"val tableOffset = __indirect(vector"
"Location + 4 * (start + middle), bb)";
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
writer += "val comp = compareStrings(\\";
writer += GenOffsetGetter(key_field) + "\\";
writer += ", byteKey, bb)";
@@ -1192,7 +1192,7 @@ class KotlinGenerator : public BaseGenerator {
GenerateOverrideFun(
writer, "keysCompare", "o1: Int, o2: Int, _bb: ByteBuffer", "Int",
[&]() {
- if (key_field->value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field->value.type)) {
writer.SetValue("offset", NumToString(key_field->value.offset));
writer +=
" return compareStrings(__offset({{offset}}, o1, "
diff --git a/src/idl_gen_lobster.cpp b/src/idl_gen_lobster.cpp
index 809b2eef..4ebf8cad 100644
--- a/src/idl_gen_lobster.cpp
+++ b/src/idl_gen_lobster.cpp
@@ -62,7 +62,7 @@ class LobsterGenerator : public BaseGenerator {
auto bits = NumToString(SizeOf(type.base_type) * 8);
if (IsInteger(type.base_type)) return "int" + bits;
if (IsFloat(type.base_type)) return "float" + bits;
- if (type.base_type == BASE_TYPE_STRING) return "string";
+ if (IsString(type)) return "string";
if (type.base_type == BASE_TYPE_STRUCT) return "table";
return "none";
}
@@ -152,7 +152,7 @@ class LobsterGenerator : public BaseGenerator {
code += NamespacedName(*field.value.type.struct_def) + " { buf_, " +
start + " }\n";
} else {
- if (vectortype.base_type == BASE_TYPE_STRING)
+ if (IsString(vectortype))
code += "buf_.flatbuffers_string";
else
code += "buf_.read_" + GenTypeName(vectortype) + "_le";
@@ -176,7 +176,7 @@ class LobsterGenerator : public BaseGenerator {
}
default: FLATBUFFERS_ASSERT(0);
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code += def +
"_length():\n return "
"buf_.flatbuffers_field_vector_len(pos_, " +
@@ -210,7 +210,7 @@ class LobsterGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
code += "def " + NormalizedName(struct_def) + "Start" +
MakeCamel(NormalizedName(field)) +
"Vector(b_:flatbuffers_builder, n_:int):\n b_.StartVector(";
diff --git a/src/idl_gen_lua.cpp b/src/idl_gen_lua.cpp
index 3d9ad9bc..9788485e 100644
--- a/src/idl_gen_lua.cpp
+++ b/src/idl_gen_lua.cpp
@@ -327,7 +327,7 @@ class LuaGenerator : public BaseGenerator {
code += "a + ((j-1) * ";
code += NumToString(InlineSize(vectortype)) + "))\n";
code += std::string(Indent) + End;
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code += std::string(Indent) + "return ''\n";
} else {
code += std::string(Indent) + "return 0\n";
@@ -502,7 +502,7 @@ class LuaGenerator : public BaseGenerator {
default: FLATBUFFERS_ASSERT(0);
}
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
GetVectorLen(struct_def, field, code_ptr);
}
}
@@ -518,7 +518,7 @@ class LuaGenerator : public BaseGenerator {
auto offset = it - struct_def.fields.vec.begin();
BuildFieldOfTable(struct_def, field, offset, code_ptr);
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
BuildVectorOfTable(struct_def, field, code_ptr);
}
}
diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp
index f346c7ce..b986fe35 100644
--- a/src/idl_gen_php.cpp
+++ b/src/idl_gen_php.cpp
@@ -401,7 +401,7 @@ class PhpGenerator : public BaseGenerator {
code += Indent + Indent + "$o = $this->__offset(" +
NumToString(field.value.offset) + ");\n";
- if (field.value.type.VectorType().base_type == BASE_TYPE_STRING) {
+ if (IsString(field.value.type.VectorType())) {
code += Indent + Indent;
code += "return $o != 0 ? $this->__string($this->__vector($o) + $j * ";
code += NumToString(InlineSize(vectortype)) + ") : ";
@@ -705,7 +705,7 @@ class PhpGenerator : public BaseGenerator {
default: FLATBUFFERS_ASSERT(0);
}
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
GetVectorLen(field, code_ptr);
if (field.value.type.element == BASE_TYPE_UCHAR) {
GetUByte(field, code_ptr);
@@ -735,7 +735,7 @@ class PhpGenerator : public BaseGenerator {
} else {
BuildFieldOfTable(field, offset, code_ptr);
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
BuildVectorOfTable(field, code_ptr);
}
}
diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp
index 95b4490a..1260673f 100644
--- a/src/idl_gen_python.cpp
+++ b/src/idl_gen_python.cpp
@@ -370,7 +370,7 @@ class PythonGenerator : public BaseGenerator {
code += "return " + GenGetter(field.value.type);
code += "a + flatbuffers.number_types.UOffsetTFlags.py_type(j * ";
code += NumToString(InlineSize(vectortype)) + "))\n";
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code += Indent + Indent + "return \"\"\n";
} else {
code += Indent + Indent + "return 0\n";
@@ -400,7 +400,7 @@ class PythonGenerator : public BaseGenerator {
code += MakeCamel(GenTypeGet(field.value.type));
code += "Flags, o)\n";
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code += Indent + Indent + "return \"\"\n";
} else {
code += Indent + Indent + "return 0\n";
@@ -614,8 +614,7 @@ class PythonGenerator : public BaseGenerator {
default: FLATBUFFERS_ASSERT(0);
}
}
- if (field.value.type.base_type == BASE_TYPE_VECTOR ||
- field.value.type.base_type == BASE_TYPE_ARRAY) {
+ if (IsVector(field.value.type) || IsArray(field.value.type)) {
GetVectorLen(struct_def, field, code_ptr);
GetVectorIsNone(struct_def, field, code_ptr);
}
@@ -642,7 +641,7 @@ class PythonGenerator : public BaseGenerator {
auto offset = it - struct_def.fields.vec.begin();
BuildFieldOfTable(struct_def, field, offset, code_ptr);
- if (field.value.type.base_type == BASE_TYPE_VECTOR) {
+ if (IsVector(field.value.type)) {
BuildVectorOfTable(struct_def, field, code_ptr);
}
}
@@ -1265,7 +1264,7 @@ class PythonGenerator : public BaseGenerator {
// each string element. And this generated code, needs to be
// placed ahead of code_prefix.
auto vectortype = field.value.type.VectorType();
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code_prefix += GenIndents(3) + MakeLowerCamel(field) + "list = []";
code_prefix += GenIndents(3) + "for i in range(len(self." +
field_instance_name + ")):";
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index ad46bac6..51600142 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -87,7 +87,7 @@ enum FullType {
FullType GetFullType(const Type &type) {
// N.B. The order of these conditionals matters for some types.
- if (type.base_type == BASE_TYPE_STRING) {
+ if (IsString(type)) {
return ftString;
} else if (type.base_type == BASE_TYPE_STRUCT) {
if (type.struct_def->fixed) {
@@ -95,7 +95,7 @@ FullType GetFullType(const Type &type) {
} else {
return ftTable;
}
- } else if (type.base_type == BASE_TYPE_VECTOR) {
+ } else if (IsVector(type)) {
switch (GetFullType(type.VectorType())) {
case ftInteger: {
return ftVectorOfInteger;
diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp
index 6116d459..c377e9f6 100644
--- a/src/idl_gen_swift.cpp
+++ b/src/idl_gen_swift.cpp
@@ -508,8 +508,8 @@ class SwiftGenerator : public BaseGenerator {
code_.SetValue("OFFSET", name);
code_.SetValue("CONSTANT", field.value.constant);
std::string check_if_vector =
- (field.value.type.base_type == BASE_TYPE_VECTOR ||
- field.value.type.base_type == BASE_TYPE_ARRAY)
+ (IsVector(field.value.type) ||
+ IsArray(field.value.type))
? "VectorOf("
: "(";
auto body = "add" + check_if_vector + name + ": ";
@@ -565,12 +565,12 @@ class SwiftGenerator : public BaseGenerator {
return;
}
- auto offset_type = field.value.type.base_type == BASE_TYPE_STRING
+ auto offset_type = IsString(field.value.type)
? "Offset<String>"
: "Offset<UOffset>";
auto camel_case_name =
- (field.value.type.base_type == BASE_TYPE_VECTOR ||
- field.value.type.base_type == BASE_TYPE_ARRAY
+ (IsVector(field.value.type) ||
+ IsArray(field.value.type)
? "vectorOf"
: "offsetOf") +
MakeCamel(name, true);
@@ -586,8 +586,8 @@ class SwiftGenerator : public BaseGenerator {
if ((vectortype.base_type == BASE_TYPE_STRUCT &&
field.value.type.struct_def->fixed) &&
- (field.value.type.base_type == BASE_TYPE_VECTOR ||
- field.value.type.base_type == BASE_TYPE_ARRAY)) {
+ (IsVector(field.value.type) ||
+ IsArray(field.value.type))) {
auto field_name = NameWrappedInNameSpace(*vectortype.struct_def);
code_ += "public static func startVectorOf" + MakeCamel(name, true) +
"(_ size: Int, in builder: inout "
@@ -738,7 +738,7 @@ class SwiftGenerator : public BaseGenerator {
return;
}
- if (vectortype.base_type == BASE_TYPE_STRING) {
+ if (IsString(vectortype)) {
code_ +=
"{{ACCESS}}.directString(at: {{ACCESS}}.vector(at: o) + "
"index * {{SIZE}}) }";
@@ -915,8 +915,8 @@ class SwiftGenerator : public BaseGenerator {
auto name = Name(field);
auto type = GenType(field.value.type);
std::string check_if_vector =
- (field.value.type.base_type == BASE_TYPE_VECTOR ||
- field.value.type.base_type == BASE_TYPE_ARRAY)
+ (IsVector(field.value.type) ||
+ IsArray(field.value.type))
? "VectorOf("
: "(";
std::string body = "add" + check_if_vector + name + ": ";
@@ -1192,7 +1192,7 @@ class SwiftGenerator : public BaseGenerator {
}
case BASE_TYPE_UTYPE: break;
default: {
- code_.SetValue("VALUETYPE", (vectortype.base_type == BASE_TYPE_STRING
+ code_.SetValue("VALUETYPE", (IsString(vectortype)
? "String?"
: GenType(vectortype)));
code_ += "{{ACCESS_TYPE}} var {{VALUENAME}}: [{{VALUETYPE}}]";
@@ -1224,7 +1224,7 @@ class SwiftGenerator : public BaseGenerator {
auto type = GenType(field.union_type);
if (field.union_type.base_type == BASE_TYPE_NONE ||
- field.union_type.base_type == BASE_TYPE_STRING) {
+ IsString(field.union_type)) {
continue;
}
code_ += "case ." + ev_name + ":";
@@ -1254,7 +1254,7 @@ class SwiftGenerator : public BaseGenerator {
auto field = **it;
auto ev_name = Name(field);
if (field.union_type.base_type == BASE_TYPE_NONE ||
- field.union_type.base_type == BASE_TYPE_STRING) {
+ IsString(field.union_type)) {
continue;
}
buffer_constructor.push_back(indentation + "case ." + ev_name + ":");
@@ -1291,7 +1291,7 @@ class SwiftGenerator : public BaseGenerator {
"fbb: "
"ByteBuffer) -> {{VALUENAME}}? {";
Indent();
- if (key_field.value.type.base_type == BASE_TYPE_STRING)
+ if (IsString(key_field.value.type))
code_ += "let key = key.utf8.map { $0 }";
code_ += "var span = fbb.read(def: Int32.self, position: Int(vector - 4))";
code_ += "var start: Int32 = 0";
@@ -1300,7 +1300,7 @@ class SwiftGenerator : public BaseGenerator {
code_ += "var middle = span / 2";
code_ +=
"let tableOffset = Table.indirect(vector + 4 * (start + middle), fbb)";
- if (key_field.value.type.base_type == BASE_TYPE_STRING) {
+ if (IsString(key_field.value.type)) {
code_ += "let comp = Table.compare(" + offset_reader + ", key, fbb: fbb)";
} else {
code_ += "let comp = fbb.read(def: {{TYPE}}.self, position: Int(" +
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp
index b9086840..4db32b7b 100644
--- a/src/idl_parser.cpp
+++ b/src/idl_parser.cpp
@@ -716,7 +716,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
// with a special suffix.
ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
type.enum_def->underlying_type, &typefield));
- } else if (type.base_type == BASE_TYPE_VECTOR &&
+ } else if (IsVector(type) &&
type.element == BASE_TYPE_UNION) {
// Only cpp, js and ts supports the union vector feature so far.
if (!SupportsAdvancedUnionFeatures()) {
@@ -793,9 +793,8 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
// Default value of union and vector in NONE, NULL translated to "0".
FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
(type.base_type == BASE_TYPE_UNION) ||
- (type.base_type == BASE_TYPE_VECTOR) ||
- (type.base_type == BASE_TYPE_ARRAY));
- if (type.base_type == BASE_TYPE_VECTOR) {
+ IsVector(type) || IsArray(type));
+ if (IsVector(type)) {
// Vector can't use initialization list.
FLATBUFFERS_ASSERT(field->value.constant == "0");
} else {
@@ -815,7 +814,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
auto hash_name = field->attributes.Lookup("hash");
if (hash_name) {
- switch ((type.base_type == BASE_TYPE_VECTOR) ? type.element
+ switch ((IsVector(type)) ? type.element
: type.base_type) {
case BASE_TYPE_SHORT:
case BASE_TYPE_USHORT: {
@@ -969,7 +968,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
auto &type = elem->second->value.type;
if (type.enum_def == val.type.enum_def) {
if (inside_vector) {
- if (type.base_type == BASE_TYPE_VECTOR &&
+ if (IsVector(type) &&
type.element == BASE_TYPE_UTYPE) {
// Vector of union type field.
uoffset_t offset;
@@ -1037,7 +1036,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
builder_.ClearOffsets();
val.constant = NumToString(builder_.GetSize());
}
- } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
+ } else if (IsString(enum_val->union_type)) {
ECHECK(ParseString(val, field->shared));
} else {
FLATBUFFERS_ASSERT(false);
@@ -2960,7 +2959,7 @@ CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
if (field.value.type.struct_def == &struct_def) {
field.value.type.struct_def = nullptr;
field.value.type.enum_def = enum_def;
- auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
+ auto &bt = IsVector(field.value.type)
? field.value.type.element
: field.value.type.base_type;
FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);