Skip to content

Commit

Permalink
THRIFT-4675: Fix int64 constants generation for map keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafa-cosar authored and jeking3 committed Jan 25, 2019
1 parent 67fbc1f commit 129bb3e
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 4 deletions.
8 changes: 6 additions & 2 deletions compiler/cpp/src/thrift/generate/t_js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,12 @@ string t_js_generator::render_const_value(t_type* type, t_const_value* value) {
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
if (v_iter != val.begin())
out << "," << endl;

out << indent() << render_const_value(ktype, v_iter->first);

if (ktype->is_base_type() && ((t_base_type*)get_true_type(ktype))->get_base() == t_base_type::TYPE_I64){
out << indent() << "\"" << v_iter->first->get_integer() << "\"";
} else {
out << indent() << render_const_value(ktype, v_iter->first);
}

out << " : ";
out << render_const_value(vtype, v_iter->second);
Expand Down
2 changes: 1 addition & 1 deletion lib/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"jsdoc": "^3.5.5",
"json-int64": "^0.0.3",
"json-int64": "^1.0.0",
"nopt": "^4.0.1"
}
}
1 change: 1 addition & 0 deletions lib/js/test/test-int64.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script src="build/js/thrift.js" type="text/javascript" charset="utf-8"></script>
<!-- browserified node-int64 -->
<script src="build/js/lib/Int64.js" type="text/javascript" charset="utf-8"></script>
<script src="build/js/lib/JSONInt64.js" type="text/javascript" charset="utf-8"></script>
<!-- int64 constants to check -->
<script src="gen-js/Int64Test_types.js" type="text/javascript" charset="utf-8"></script>

Expand Down
6 changes: 6 additions & 0 deletions lib/js/test/test-int64.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ QUnit.module('Int64');
for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
assert.ok(EXPECTED_INT64_LIST[i].equals(Int64Test.INT64_LIST[i]));
}

for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i){
let int64Object = EXPECTED_INT64_LIST[i];
assert.ok(Int64Test.INT64_2_INT64_MAP[JSONInt64.toDecimalString(int64Object)].equals(int64Object));
}

console.log('Int64 test -- ends');
});

11 changes: 11 additions & 0 deletions lib/nodejs/test/int64.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

const Int64 = require("node-int64");
const JSONInt64 = require("json-int64");
const i64types = require("./gen-nodejs-es6/Int64Test_types.js");
const test = require("tape");

Expand Down Expand Up @@ -72,6 +73,16 @@ const cases = {
for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
assert.ok(EXPECTED_INT64_LIST[i].equals(i64types.INT64_LIST[i]));
}

for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
const int64Object = EXPECTED_INT64_LIST[i];
assert.ok(
i64types.INT64_2_INT64_MAP[
JSONInt64.toDecimalString(int64Object)
].equals(int64Object)
);
}

assert.end();
}
};
Expand Down
7 changes: 7 additions & 0 deletions lib/nodets/test/int64.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import Int64 = require("node-int64");
import JSONInt64 = require('json-int64');
import i64types = require("./gen-nodejs/Int64Test_types");
import test = require("tape");

Expand Down Expand Up @@ -72,6 +73,12 @@ const cases = {
for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
assert.ok(EXPECTED_INT64_LIST[i].equals(i64types.INT64_LIST[i]));
}

for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i){
let int64Object = EXPECTED_INT64_LIST[i];
assert.ok(i64types.INT64_2_INT64_MAP[JSONInt64.toDecimalString(int64Object)].equals(int64Object));
}

assert.end();
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"bufferutil": "^4.0.1",
"jsdoc": "^3.5.5",
"json-int64": "^0.0.3",
"json-int64": "^1.0.0",
"nopt": "^4.0.1"
},
"types": "./thrift.d.ts"
Expand Down
7 changes: 7 additions & 0 deletions lib/ts/test/test-int64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* jshint -W100 */

var Int64 = require("node-int64");
var JSONInt64 = require("json-int64");
import { Int64Test } from "./gen-js/Int64Test_types";


Expand Down Expand Up @@ -89,6 +90,12 @@ QUnit.module('Int64');
for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
assert.ok(EXPECTED_INT64_LIST[i].equals(Int64Test.INT64_LIST[i]));
}

for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i){
let int64Object = EXPECTED_INT64_LIST[i];
assert.ok(Int64Test.INT64_2_INT64_MAP[JSONInt64.toDecimalString(int64Object)].equals(int64Object));
}

console.log('Int64 test -- ends');
});

9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"eslint-plugin-prettier": "^3.0.0",
"istanbul": "^0.4.5",
"jsdoc": "^3.5.5",
"json-int64": "^1.0.0",
"prettier": "^1.14.3",
"tape": "^4.9.0",
"utf-8-validate": "^4.0.0",
Expand Down
10 changes: 10 additions & 0 deletions test/Int64Test.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ const i64 MAX_SIGNED_INT64 = 9223372036854775807
const i64 MIN_SIGNED_INT64 = -9223372036854775808

const list<i64> INT64_LIST = [SMALL_INT64, MAX_JS_SAFE_INT64, MIN_JS_SAFE_INT64, MAX_JS_SAFE_PLUS_ONE_INT64, MIN_JS_SAFE_MINUS_ONE_INT64, MAX_SIGNED_INT64, MIN_SIGNED_INT64]

const map<i64, i64> INT64_2_INT64_MAP = {
SMALL_INT64: SMALL_INT64,
MAX_JS_SAFE_INT64: MAX_JS_SAFE_INT64,
MIN_JS_SAFE_INT64: MIN_JS_SAFE_INT64,
MAX_JS_SAFE_PLUS_ONE_INT64: MAX_JS_SAFE_PLUS_ONE_INT64,
MIN_JS_SAFE_MINUS_ONE_INT64: MIN_JS_SAFE_MINUS_ONE_INT64,
MAX_SIGNED_INT64: MAX_SIGNED_INT64,
MIN_SIGNED_INT64: MIN_SIGNED_INT64
}

0 comments on commit 129bb3e

Please sign in to comment.