[ https://issues.apache.org/jira/browse/THRIFT-1976?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17351293#comment-17351293 ]
Axel commented on THRIFT-1976: ------------------------------ Yep, here is my explanation: Currently, the thrift compiler maps a thrift map to a javascript object and you are accessing its members using string only keys, which is the root cause of this issue here. Since Javascript ES6, there is the native data type Map in javascript that can utilize arbitrary types as keys. You are correct that I did not include a PR so far, as the work is not done yet. The reason why I put my comment here and basically link my issue with this one is for two reasons: # I plan to work on THRIFT-5409. So I searched the bugtracker for related tickets. But THRIFT-5409 not a duplicate as it is a bit broader and was created due to a different motivation. It will make this one superflous, very similar to my work on THRIFT-3508 vs. THRIFT-3227. # I wanted to document here the fact that ES6 provides a Map collection type that can very well support arbitrary types as key. So this is actually fixed in javascript and no longer an issue of the language. So in order to fix the issue here, I think the preferred way is to use that Map and "catch up" with the improvements in the JS language. I am well aware that working on this is a bit more complex than THRIFT-3508, as it is a breaking change. So it either needs to be opt-in or it needs to be decided that the gain is worth the breaking change. A proper TDD workflow is also mandatory in order to finish this work. > Javascript client unable to serialize/deserialize maps with struct keys > ----------------------------------------------------------------------- > > Key: THRIFT-1976 > URL: https://issues.apache.org/jira/browse/THRIFT-1976 > Project: Thrift > Issue Type: Bug > Components: JavaScript - Compiler, JavaScript - Library > Affects Versions: 0.9 > Environment: All > Reporter: Andrew Stanton > Assignee: Randy Abernethy > Priority: Major > > The Javascript compiler is unable to serialize maps where a struct is a key > due to particularities with how Javascript treats {} objects. > Using the following thrift definition: > {code} > struct Foo { > 1: required string name; > } > struct Bar { > 1: required string description; > } > struct Mapper { > 1: required map<Foo,Bar> fooToBar; > } > {code} > It produces the following javascript code: > {code} > Mapper.prototype.write = function(output) { > output.writeStructBegin('Mapper'); > if (this.fooToBar !== null && this.fooToBar !== undefined) { > output.writeFieldBegin('fooToBar', Thrift.Type.MAP, 1); > output.writeMapBegin(Thrift.Type.STRUCT, Thrift.Type.STRUCT, > Thrift.objectLength(this.fooToBar)); > // XXX > // This will always fail as kiter8 will always be a String > // XXX > for (var kiter8 in this.fooToBar) > { > if (this.fooToBar.hasOwnProperty(kiter8)) > { > var viter9 = this.fooToBar[kiter8]; > // kiter8 is a string, not an object. > kiter8.write(output); > viter9.write(output); > } > } > output.writeMapEnd(); > output.writeFieldEnd(); > } > output.writeFieldStop(); > output.writeStructEnd(); > return; > }; > {code} > This code always fails since enumeration of an object's keys will always > yield String values. I've annotated the relevant parts in the above code. > There isn't really a simple fix for this either: Using for-in, there is no > way to get a reference to the original key object. Practically, a Map type > would have to be added to the thrift library with basic put()/get()/delete() > operations to allow for struct maps. -- This message was sent by Atlassian Jira (v8.3.4#803005)