Repository: thrift
Updated Branches:
  refs/heads/master f49ea0691 -> 25023b16e


THRIFT-809 Javascript client: Please make required fields actually required.

Patch: noazark

Github Pull Request: This closes #232


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/25023b16
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/25023b16
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/25023b16

Branch: refs/heads/master
Commit: 25023b16eb4f26d7fcf986e817e8c437e2f08bf3
Parents: f49ea06
Author: Roger Meier <[email protected]>
Authored: Thu Oct 9 02:01:17 2014 +0200
Committer: Roger Meier <[email protected]>
Committed: Thu Oct 9 02:01:17 2014 +0200

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_js_generator.cc |  8 ++++--
 lib/nodejs/lib/thrift/protocol.js           | 36 +++++++-----------------
 lib/nodejs/lib/thrift/thrift.js             | 18 ++++++++++++
 3 files changed, 34 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/25023b16/compiler/cpp/src/generate/t_js_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_js_generator.cc 
b/compiler/cpp/src/generate/t_js_generator.cc
index 74ec15a..12b7daa 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -708,8 +708,12 @@ void 
t_js_generator::generate_js_struct_definition(ofstream& out,
 
     for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
         out << indent() << indent() << "if (args." << (*m_iter)->get_name() << 
" !== undefined) {" << endl
-            << indent() << indent() << indent() << "this." << 
(*m_iter)->get_name() << " = args." << (*m_iter)->get_name()  << ";" << endl
-            << indent() << indent() << "}" << endl;
+            << indent() << indent() << indent() << "this." << 
(*m_iter)->get_name() << " = args." << (*m_iter)->get_name()  << ";" << endl;
+        if (!(*m_iter)->get_req()) {
+          out << indent() << indent() << "} else {" << endl
+              << indent() << indent() << indent() << "throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.UNKNOWN, 'Required 
field " << (*m_iter)->get_name() << " is unset!');" << endl;
+        }
+        out << indent() << indent() << "}" << endl;
         if (gen_ts_) {
           f_types_ts_ << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": " 
<< ts_get_type((*m_iter)->get_type()) << "; ";
         }

http://git-wip-us.apache.org/repos/asf/thrift/blob/25023b16/lib/nodejs/lib/thrift/protocol.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/protocol.js 
b/lib/nodejs/lib/thrift/protocol.js
index 6c4d9e6..c8e8a4e 100644
--- a/lib/nodejs/lib/thrift/protocol.js
+++ b/lib/nodejs/lib/thrift/protocol.js
@@ -25,22 +25,6 @@ var binary = require('./binary'),
 
 var InputBufferUnderrunError = require('./transport').InputBufferUnderrunError;
 
-var UNKNOWN = 0,
-    INVALID_DATA = 1,
-    NEGATIVE_SIZE = 2,
-    SIZE_LIMIT = 3,
-    BAD_VERSION = 4,
-    NOT_IMPLEMENTED = 5,
-    DEPTH_LIMIT = 6;
-
-var TProtocolException = function(type, message) {
-  Error.call(this, message);
-  this.name = 'TProtocolException';
-  this.type = type;
-};
-util.inherits(TProtocolException, Error);
-
-
 //
 // BINARY PROTOCOL
 //
@@ -186,14 +170,14 @@ TBinaryProtocol.prototype.readMessageBegin = function() {
     var version = sz & VERSION_MASK;
     if (version != VERSION_1) {
       console.log("BAD: " + version);
-      throw new TProtocolException(BAD_VERSION, "Bad version in 
readMessageBegin: " + sz);
+      throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.BAD_VERSION, "Bad 
version in readMessageBegin: " + sz);
     }
     type = sz & TYPE_MASK;
     name = this.readString();
     seqid = this.readI32();
   } else {
     if (this.strictRead) {
-      throw new TProtocolException(BAD_VERSION, "No protocol version header");
+      throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.BAD_VERSION, "No 
protocol version header");
     }
     name = this.trans.read(sz);
     type = this.readByte();
@@ -550,7 +534,7 @@ TCompactProtocol.prototype.getTType = function(type) {
     case TCompactProtocol.Types.CT_STRUCT:
       return Type.STRUCT;
     default:
-      throw new TProtocolException(INVALID_DATA, "Unknown type: " + type);
+      throw new 
Thrift.TProtocolException(Thift.TProtocolExceptionType.INVALID_DATA, "Unknown 
type: " + type);
   }
   return Type.STOP;
 };
@@ -808,7 +792,7 @@ TCompactProtocol.prototype.i64ToZigzag = function(l) {
     l = new Int64(l);
   }
   if (! (l instanceof Int64)) {
-    throw new TProtocolException(INVALID_DATA, "Expected Int64 or Number, 
found: " + l);
+    throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.INVALID_DATA, "Expected 
Int64 or Number, found: " + l);
   }
   var hi = l.buffer.readUInt32BE(0, true);
   var lo = l.buffer.readUInt32BE(4, true);
@@ -907,8 +891,8 @@ TCompactProtocol.prototype.readFieldEnd = function() {
 TCompactProtocol.prototype.readMapBegin = function() {
   var msize = this.readVarint32();
   if (msize < 0) {
-    throw new TProtocolException(NEGATIVE_SIZE, "Negative map size");
-  } 
+    throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, 
"Negative map size");
+  }
 
   var kvType = 0;
   if (msize !== 0) {
@@ -932,8 +916,8 @@ TCompactProtocol.prototype.readListBegin = function() {
   }
 
   if (lsize < 0) {
-    throw TProtocolException(NEGATIVE_SIZE, "Negative list size");
-  } 
+    throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, 
"Negative list size");
+  }
 
   var elemType = this.getTType(size_and_type & 0x0000000f);
 
@@ -993,7 +977,7 @@ TCompactProtocol.prototype.readBinary = function() {
 
   // Catch error cases
   if (size < 0) {
-    throw new TProtocolException(NEGATIVE_SIZE, "Negative binary/string size");
+    throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, 
"Negative binary/string size");
   }
   var value = this.trans.readString(size);
 
@@ -1042,7 +1026,7 @@ TCompactProtocol.prototype.readVarint64 = function() {
       break;
     }
     if (rsize >= 10) {
-      throw new TProtocolException(INVALID_DATA, "Variable-length int over 10 
bytes.");
+      throw new 
Thrift.TProtocolException(Thrift.TProtocolExceptionType.INVALID_DATA, 
"Variable-length int over 10 bytes.");
     }
   }
   var i64 = new Int64(hi, lo);

http://git-wip-us.apache.org/repos/asf/thrift/blob/25023b16/lib/nodejs/lib/thrift/thrift.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/thrift.js b/lib/nodejs/lib/thrift/thrift.js
index aabe11f..e02bb7d 100644
--- a/lib/nodejs/lib/thrift/thrift.js
+++ b/lib/nodejs/lib/thrift/thrift.js
@@ -126,6 +126,24 @@ TApplicationException.prototype.write = function(output){
   output.writeStructEnd();
 };
 
+var TProtocolExceptionType = exports.TProtocolExceptionType = {
+  UNKNOWN: 0,
+  INVALID_DATA: 1,
+  NEGATIVE_SIZE: 2,
+  SIZE_LIMIT: 3,
+  BAD_VERSION: 4,
+  NOT_IMPLEMENTED: 5,
+  DEPTH_LIMIT: 6
+}
+
+
+var TProtocolException = exports.TProtocolException = function(type, message) {
+  Error.call(this, message);
+  this.name = 'TProtocolException';
+  this.type = type;
+};
+util.inherits(TProtocolException, Error);
+
 exports.objectLength = function(obj) {
   return Object.keys(obj).length;
 };

Reply via email to