mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 20:12:02 +00:00
has_method support for primitive fields in java runtime. Changed: idl.h, FlatBufferBuilder.java , idl_gen_general.cpp, idl_parser.cpp, flatc.cpp (#5468)
* has_method support for primitive fields in java runtime * adding the new flag to flatc * addressing the review comments
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
acc9990abd
commit
a20e71ac96
@@ -528,6 +528,7 @@ struct IDLOptions {
|
|||||||
bool size_prefixed;
|
bool size_prefixed;
|
||||||
std::string root_type;
|
std::string root_type;
|
||||||
bool force_defaults;
|
bool force_defaults;
|
||||||
|
bool java_primitive_has_method;
|
||||||
std::vector<std::string> cpp_includes;
|
std::vector<std::string> cpp_includes;
|
||||||
|
|
||||||
// Possible options for the more general generator below.
|
// Possible options for the more general generator below.
|
||||||
@@ -602,6 +603,7 @@ struct IDLOptions {
|
|||||||
protobuf_ascii_alike(false),
|
protobuf_ascii_alike(false),
|
||||||
size_prefixed(false),
|
size_prefixed(false),
|
||||||
force_defaults(false),
|
force_defaults(false),
|
||||||
|
java_primitive_has_method(false),
|
||||||
lang(IDLOptions::kJava),
|
lang(IDLOptions::kJava),
|
||||||
mini_reflect(IDLOptions::kNone),
|
mini_reflect(IDLOptions::kNone),
|
||||||
lang_to_generate(0),
|
lang_to_generate(0),
|
||||||
@@ -927,6 +929,8 @@ class Parser : public ParserState {
|
|||||||
|
|
||||||
extern std::string MakeCamel(const std::string &in, bool first = true);
|
extern std::string MakeCamel(const std::string &in, bool first = true);
|
||||||
|
|
||||||
|
extern std::string MakeScreamingCamel(const std::string &in);
|
||||||
|
|
||||||
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
||||||
// object that has been populated with the corresponding schema.
|
// object that has been populated with the corresponding schema.
|
||||||
// If ident_step is 0, no indentation will be generated. Additionally,
|
// If ident_step is 0, no indentation will be generated. Additionally,
|
||||||
|
|||||||
@@ -199,6 +199,17 @@ public class FlatBufferBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to test if a field is present in the table
|
||||||
|
*
|
||||||
|
* @param table Flatbuffer table
|
||||||
|
* @param offset virtual table offset
|
||||||
|
* @return true if the filed is present
|
||||||
|
*/
|
||||||
|
public static boolean isFieldPresent(Table table, int offset) {
|
||||||
|
return table.__offset(offset) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the FlatBufferBuilder by purging all data that it holds.
|
* Reset the FlatBufferBuilder by purging all data that it holds.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -319,6 +319,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
|||||||
opts.force_defaults = true;
|
opts.force_defaults = true;
|
||||||
} else if (arg == "--force-empty") {
|
} else if (arg == "--force-empty") {
|
||||||
opts.set_empty_to_null = false;
|
opts.set_empty_to_null = false;
|
||||||
|
} else if (arg == "--java-primitive-has-method") {
|
||||||
|
opts.java_primitive_has_method = true;
|
||||||
} else {
|
} else {
|
||||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||||
if (arg == params_.generators[i].generator_opt_long ||
|
if (arg == params_.generators[i].generator_opt_long ||
|
||||||
|
|||||||
@@ -1323,6 +1323,15 @@ class GeneralGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (parser_.opts.java_primitive_has_method &&
|
||||||
|
IsScalar(field.value.type.base_type) && !struct_def.fixed) {
|
||||||
|
auto vt_offset_constant = " public static final int VT_" +
|
||||||
|
MakeScreamingCamel(field.name) + " = " +
|
||||||
|
NumToString(field.value.offset) + ";";
|
||||||
|
|
||||||
|
code += vt_offset_constant;
|
||||||
|
code += "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
code += "\n";
|
code += "\n";
|
||||||
flatbuffers::FieldDef *key_field = nullptr;
|
flatbuffers::FieldDef *key_field = nullptr;
|
||||||
|
|||||||
@@ -102,6 +102,18 @@ std::string MakeCamel(const std::string &in, bool first) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert an underscore_based_identifier in to screaming snake case.
|
||||||
|
std::string MakeScreamingCamel(const std::string &in) {
|
||||||
|
std::string s;
|
||||||
|
for (size_t i = 0; i < in.length(); i++) {
|
||||||
|
if (in[i] != '_')
|
||||||
|
s += static_cast<char>(toupper(in[i]));
|
||||||
|
else
|
||||||
|
s += in[i];
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
void DeserializeDoc( std::vector<std::string> &doc,
|
void DeserializeDoc( std::vector<std::string> &doc,
|
||||||
const Vector<Offset<String>> *documentation) {
|
const Vector<Offset<String>> *documentation) {
|
||||||
if (documentation == nullptr) return;
|
if (documentation == nullptr) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user