You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Larregoity (Commented) (JIRA)" <ji...@apache.org> on 2012/03/28 19:11:27 UTC

[jira] [Commented] (THRIFT-1297) Compiled javascript code does not write falsy values

    [ https://issues.apache.org/jira/browse/THRIFT-1297?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13240547#comment-13240547 ] 

Larregoity commented on THRIFT-1297:
------------------------------------

Thanks for the work around.
Unfortunately, for booleans, wrapping using new Boolean(bool) this is not enough:

 var bool = false;
 new Boolean(bool)  is not falsy, so it will be written.

But as in lib.js, the writeBool method is:
 writeBool: function(value) {
        this.tstack.push(value ? 1 : 0);
    },
The value pushed on the wire will always be 1.

I work around this issue using:

	var oldWriteBool = Thrift.Protocol.prototype.writeBool;
	var newWriteBool = function (value) {
		var fixedValue = value;
		if (value instanceof Boolean) {
			fixedValue = value.valueOf();
		}
		oldWriteBool.call(this, fixedValue);
	}
	Thrift.Protocol.prototype.writeBool = newWriteBool;

and then I can pass false booleans by wrapping them into new Boolean.

                
> Compiled javascript code does not write falsy values
> ----------------------------------------------------
>
>                 Key: THRIFT-1297
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1297
>             Project: Thrift
>          Issue Type: Bug
>          Components: JavaScript - Compiler, Node.js - Compiler
>    Affects Versions: 0.7
>         Environment: Windows 0.7 thrift compiler (pre built)
> http://www.apache.org/dyn/closer.cgi?path=/thrift/0.7.0/thrift-0.7.0.exe
> Thrift 0.7 compiled on Ubuntu shows same symptoms
>            Reporter: Joris van der Wel
>            Priority: Critical
>              Labels: compiler, javascript, regression
>
> I compiled cassandra.thrift using "thrift --gen js:node cassandra.thrift".
> When I use this code this results in the following error from cassandra / thrift:
> {code}
> { name: 'TApplicationException',  type: 7,
>   message: 'Required field \'reversed\' was not found in serialized data! Struct : SliceRange(start:null, finish:null, reversed:false, count:100)' }
> {code}
> If I look in the generated code, I find 
> {code}
> SliceRange.prototype.write = function(output) {
>   output.writeStructBegin('SliceRange');
>   if (this.start) {
>     output.writeFieldBegin('start', Thrift.Type.STRING, 1);
>     output.writeString(this.start);
>     output.writeFieldEnd();
>   }
>   if (this.finish) {
>     output.writeFieldBegin('finish', Thrift.Type.STRING, 2);
>     output.writeString(this.finish);
>     output.writeFieldEnd();
>   }
>   if (this.reversed) {
>     output.writeFieldBegin('reversed', Thrift.Type.BOOL, 3);
>     output.writeBool(this.reversed);
>     output.writeFieldEnd();
>   }
>   if (this.count) {
>     output.writeFieldBegin('count', Thrift.Type.I32, 4);
>     output.writeI32(this.count);
>     output.writeFieldEnd();
>   }
>   output.writeFieldStop();
>   output.writeStructEnd();
>   return;
> };
> {code}
> this.reversed is false and subsequently does not get sent
> If I look at the compiled version that was generated by 0.6.1 I see:
> {code}
> SliceRange.prototype.write = function(output){ 
>   output.writeStructBegin('SliceRange')
>   if (null != this.start) {
>     output.writeFieldBegin('start', Thrift.Type.STRING, 1)
>     output.writeString(this.start)
>     output.writeFieldEnd()
>   }
>   if (null != this.finish) {
>     output.writeFieldBegin('finish', Thrift.Type.STRING, 2)
>     output.writeString(this.finish)
>     output.writeFieldEnd()
>   }
>   if (null != this.reversed) {
>     output.writeFieldBegin('reversed', Thrift.Type.BOOL, 3)
>     output.writeBool(this.reversed)
>     output.writeFieldEnd()
>   }
>   if (null != this.count) {
>     output.writeFieldBegin('count', Thrift.Type.I32, 4)
>     output.writeI32(this.count)
>     output.writeFieldEnd()
>   }
>   output.writeFieldStop()
>   output.writeStructEnd()
>   return
> }
> {code}
> null is also not be completely correct, the solution would be:
> {code}
> ...
> if (undefined !== this.reversed)
> ...
> {code}
> Unless this change was intentional and breaks things because cassandra is perhaps using an old thrift version?

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira