THRIFT-2576 Implement Thrift.Protocol.prototype.skip method in JavaScript library
Client: JavaScript
Patch: Hyungsul Kim
This closes #141
diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js
index 8fc7cd2..6cab4fd 100644
--- a/lib/js/src/thrift.js
+++ b/lib/js/src/thrift.js
@@ -1299,11 +1299,77 @@
},
/**
- * Method to arbitrarily skip over data (not implemented).
- * @throws {string} this method is not implemented and always throws.
- */
+ * Method to arbitrarily skip over data */
skip: function(type) {
- throw 'skip not supported yet';
+ var ret, i;
+ switch (type) {
+ case Thrift.Type.STOP:
+ return null;
+
+ case Thrift.Type.BOOL:
+ return this.readBool();
+
+ case Thrift.Type.BYTE:
+ return this.readByte();
+
+ case Thrift.Type.I16:
+ return this.readI16();
+
+ case Thrift.Type.I32:
+ return this.readI32();
+
+ case Thrift.Type.I64:
+ return this.readI64();
+
+ case Thrift.Type.DOUBLE:
+ return this.readDouble();
+
+ case Thrift.Type.STRING:
+ return this.readString();
+
+ case Thrift.Type.STRUCT:
+ this.readStructBegin();
+ while (true) {
+ ret = this.readFieldBegin();
+ if (ret.ftype == Thrift.Type.STOP) {
+ break;
+ }
+ this.skip(ret.ftype);
+ this.readFieldEnd();
+ }
+ this.readStructEnd();
+ return null;
+
+ case Thrift.Type.MAP:
+ ret = this.readMapBegin();
+ for (i = 0; i < ret.size; i++) {
+ if (i > 0) {
+ if (this.rstack.length > this.rpos[this.rpos.length - 1] + 1) {
+ this.rstack.pop();
+ }
+ }
+ this.skip(ret.ktype);
+ this.skip(ret.vtype);
+ }
+ this.readMapEnd();
+ return null;
+
+ case Thrift.Type.SET:
+ ret = this.readSetBegin();
+ for (i = 0; i < ret.size; i++) {
+ this.skip(ret.etype);
+ }
+ this.readSetEnd();
+ return null;
+
+ case Thrift.Type.LIST:
+ ret = this.readListBegin();
+ for (i = 0; i < ret.size; i++) {
+ this.skip(ret.etype);
+ }
+ this.readListEnd();
+ return null;
+ }
}
};
diff --git a/lib/js/test/test.js b/lib/js/test/test.js
index 7351fd9..1504f62 100755
--- a/lib/js/test/test.js
+++ b/lib/js/test/test.js
@@ -191,6 +191,45 @@
equal(client.testTypedef(69), 69);
});
+ test("Skip", function() {
+ var structTestInput = new ThriftTest.Xtruct();
+ var modifiedClient = new ThriftTest.ThriftTestClient(protocol);
+
+ modifiedClient.recv_testStruct = function() {
+ var input = modifiedClient.input;
+ var xtruct3 = new ThriftTest.Xtruct3();
+
+ input.readMessageBegin();
+ input.readStructBegin();
+
+ // read Xtruct data with Xtruct3
+ input.readFieldBegin();
+ xtruct3.read(input);
+ input.readFieldEnd();
+ // read Thrift.Type.STOP message
+ input.readFieldBegin();
+ input.readFieldEnd();
+
+ input.readStructEnd();
+ input.readMessageEnd();
+
+ return xtruct3;
+ };
+
+ structTestInput.string_thing = 'worked';
+ structTestInput.byte_thing = 0x01;
+ structTestInput.i32_thing = Math.pow(2,30);
+ structTestInput.i64_thing = Math.pow(2,52);
+
+ var structTestOutput = modifiedClient.testStruct(structTestInput);
+
+ equal(structTestOutput instanceof ThriftTest.Xtruct3, true);
+ equal(structTestOutput.string_thing, structTestInput.string_thing);
+ equal(structTestOutput.changed, null);
+ equal(structTestOutput.i32_thing, structTestInput.i32_thing);
+ equal(structTestOutput.i64_thing, structTestInput.i64_thing);
+ });
+
module("deeper!");