Skip to content

Commit

Permalink
THRIFT-4513: Fixing java thrift compiler to generate constants in sta…
Browse files Browse the repository at this point in the history
  • Loading branch information
romanoid committed Mar 9, 2018
1 parent b4f22ff commit 32e4c53
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions compiler/cpp/src/thrift/generate/t_java_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,61 @@ void t_java_generator::generate_consts(std::vector<t_const*> consts) {
f_consts.close();
}

/**
* A functor that determines which of two constant map keys of the same type is Greater.
*/
class IsKeyLess {
public:
IsKeyLess(t_type* type) {
type_ = type;
};

bool operator()(t_const_value* left_key, t_const_value* right_key) const {
t_type* type = t_java_generator::get_true_type(type_);

if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
case t_base_type::TYPE_STRING:
return left_key->get_string() < right_key->get_string();
case t_base_type::TYPE_BOOL:
case t_base_type::TYPE_I8:
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
case t_base_type::TYPE_I64:
return left_key->get_integer() < right_key->get_integer();
case t_base_type::TYPE_DOUBLE:
if (left_key->get_type() == t_const_value::CV_INTEGER
&& right_key->get_type() == t_const_value::CV_INTEGER) {
return left_key->get_integer() < right_key->get_integer();
}
double left;
double right;
if (left_key->get_type() == t_const_value::CV_INTEGER) {
left = left_key->get_integer();
} else {
left = left_key->get_double();
}
if (right_key->get_type() == t_const_value::CV_INTEGER) {
right = right_key->get_integer();
} else {
right = right_key->get_double();
}
return left < right;
default:
throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
return left_key->get_integer() < right_key->get_integer();
} else {
throw "compiler error: unexpected constant map key type " + type->get_name();
}
}

private:
t_type* type_;
};

/**
* Prints the value of a constant with the given type. Note that type checking
* is NOT performed in this function as it is always run beforehand using the
Expand All @@ -619,7 +674,9 @@ void t_java_generator::print_const_value(std::ofstream& out,
} else if (type->is_enum()) {
out << name << " = " << render_const_value(out, type, value) << ";" << endl << endl;
} else if (type->is_struct() || type->is_xception()) {
const vector<t_field*>& fields = ((t_struct*)type)->get_members();
const vector<t_field*>& unsorted_fields = ((t_struct*)type)->get_members();
vector<t_field*> fields = unsorted_fields;
std::sort(fields.begin(), fields.end());
vector<t_field*>::const_iterator f_iter;
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
Expand Down Expand Up @@ -660,7 +717,9 @@ void t_java_generator::print_const_value(std::ofstream& out,
}
t_type* ktype = ((t_map*)type)->get_key_type();
t_type* vtype = ((t_map*)type)->get_val_type();
const map<t_const_value*, t_const_value*>& val = value->get_map();
const map<t_const_value*, t_const_value*>& unsorted_val = value->get_map();
map<t_const_value*, t_const_value*, IsKeyLess> val = map<t_const_value*, t_const_value*, IsKeyLess>(
unsorted_val.begin(), unsorted_val.end(), IsKeyLess(ktype));
map<t_const_value*, t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string key = render_const_value(out, ktype, v_iter->first);
Expand Down

0 comments on commit 32e4c53

Please sign in to comment.