Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

[FOUND-1522 / THRIFT-1976] Allow JS thrift client to use ES6 maps #2

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions compiler/cpp/src/thrift/generate/t_js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,13 @@ void t_js_generator::generate_deserialize_container(ostream& out, t_type* ttype,
t_field fsize(g_type_i32, size);

// Declare variables, read header
if (ttype->is_map()) {
if (ttype->is_map() && gen_es6_) {
out << indent() << prefix << " = new Map();" << endl;

out << indent() << js_const_type_ << rtmp3 << " = input.readMapBegin();" << endl;
out << indent() << js_const_type_ << size << " = " << rtmp3 << ".size || 0;" << endl;

} else if (ttype->is_map()) {
out << indent() << prefix << " = {};" << endl;

out << indent() << js_const_type_ << rtmp3 << " = input.readMapBegin();" << endl;
Expand Down Expand Up @@ -2365,7 +2371,11 @@ void t_js_generator::generate_deserialize_map_element(ostream& out, t_map* tmap,
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);

indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
if (gen_es6_) {
indent(out) << prefix << ".set(" << key << ", " << val << ");" << endl;
} else {
indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
}
}

void t_js_generator::generate_deserialize_set_element(ostream& out, t_set* tset, string prefix) {
Expand Down Expand Up @@ -2492,7 +2502,16 @@ void t_js_generator::generate_serialize_container(ostream& out, t_type* ttype, s
<< ", " << prefix << ".length);" << endl;
}

if (ttype->is_map()) {
if (ttype->is_map() && gen_es6_) {
string kiter = tmp("kiter");
string viter = tmp("viter");
indent(out) << "for (" << js_let_type_ << kiter << " of " << prefix << ".keys()) {" << endl;
indent_up();
indent(out) << js_let_type_ << viter << " = " << prefix << ".get(" << kiter << ");" << endl;
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
scope_down(out);

} else if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
indent(out) << "for (" << js_let_type_ << kiter << " in " << prefix << ") {" << endl;
Expand Down
18 changes: 18 additions & 0 deletions lib/nodejs/lib/thrift/thrift.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ function TProtocolException(type, message) {
util.inherits(TProtocolException, Error);

exports.objectLength = function(obj) {
if (obj instanceof Map) {
return obj.size;
}
return Object.keys(obj).length;
};

Expand Down Expand Up @@ -219,6 +222,21 @@ copyMap = function(obj, types){
}
var Type = type;

if (obj instanceof Map) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is happening on line 226? and 231?

i understand that you're following the example on lines 240-256; am wondering if want to use clearer JS syntax (and possibly es6?)

var result = new Map(), val;
for(var prop of obj.keys()) {
val = obj.get(prop);
if (type === null) {
result.set(prop, val);
} else if (type === copyMap || type === copyList) {
result.set(prop, type(val, types.slice(1)));
} else {
result.set(prop, new Type(val));
}
}
return result;
}

var result = {}, val;
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/apache/thrift.git"
},
"version": "0.16.0",
"version": "1.0.0",
"author": {
"name": "Apache Thrift Developers",
"email": "dev@thrift.apache.org",
Expand Down