[jira] [Comment Edited] (THRIFT-4611) NodeJS thrift enums not mappable back to their string values
[ https://issues.apache.org/jira/browse/THRIFT-4611?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16570469#comment-16570469 ] Can Celasun edited comment on THRIFT-4611 at 8/6/18 4:44 PM: - First, I think there is a misunderstanding here. If you have access to this generated code: {code:java} var ttypes = module.exports = {}; ttypes.EventType = { 'OUTBOUND' : 0, 'INBOUND' : 1, } {code} you can simply reverse the EventType object and lookup the string. Why does that not work for you? Now, on to your suggestion: {code:java} if (this.type !== null && this.type !== undefined) { output.writeFieldBegin('type', Common_ttypes.EventType, 3); output.writeMap(this.type); // key and value here would be string and number output.writeFieldEnd(); } {code} This will never happen as it would change the thrift wire format. Enums are sent as integers, in all supported languages. The best thing to do here is to add a string function to the generated object, so you'll be able to do something like: {code:java} EventType.stringValue(1) // returns "INBOUND" {code} was (Author: calcifer): First, I think there is a misunderstanding here. If you have access to this generated code: {code:java} var ttypes = module.exports = {}; ttypes.EventType = { 'OUTBOUND' : 0, 'INBOUND' : 1, } {code} you can simply reverse the EventType object and lookup the string. Why does that not work for you? Now, on to your suggestion: {code:java} if (this.type !== null && this.type !== undefined) { output.writeFieldBegin('type', Common_ttypes.EventType, 3); output.writeMap(this.type); // key and value here would be string and number output.writeFieldEnd(); } {code} This will never happen as it would change the thrift wire format. Enums are sent as integers, in all supported languages. The best thing to do here is to add a string function to the generated object, so you'll be able to do something like: {code:java} EventType.stringValue(1) // returns "INBOUND" {code} > NodeJS thrift enums not mappable back to their string values > > > Key: THRIFT-4611 > URL: https://issues.apache.org/jira/browse/THRIFT-4611 > Project: Thrift > Issue Type: Wish > Components: JavaScript - Library, Node.js - Library >Affects Versions: 0.11.0 >Reporter: Anthony Alayo >Priority: Minor > > While attempting to build a javascript-based web tool to read encoded thrift > objects, I hit a wall. While I was able to decode from a base64 thrift string > to an object, all enum fields are only in their numeral representations with > no way to reverse them. > > Here is one example, where there is a Common.thrift file containing enums, > and an Event.thrift using one of the enums from Common.thrift. > > Here is the generated Event_types.js: > {code:java} > var Common_ttypes = require('./Common_types'); > var ttypes = module.exports = {}; > var Event = module.exports.Event = function(args) { > this.id = null; > this.type = null; > ... > } > {code} > Here is the generated Common_types.js > {code:java} > var ttypes = module.exports = {}; > ttypes.EventType = { > 'OUTBOUND' : 0, > 'INBOUND' : 1, > }{code} > While Common_types is being required in Event_types, there is no use of it > anywhere. The field type is solely being treated as a number: > {code:java} > if (this.type !== null && this.type !== undefined) { > output.writeFieldBegin('type', Thrift.Type.I32, 3); > output.writeI32(this.type); > output.writeFieldEnd(); > } > {code} > > It seems to be a bug, since I assume from the import that we would expect to > use enums instead of numbers. Perhaps this is intended, but if that's the > case, could this be made into a feature? The ability to know and display the > enum string values instead of a number is a game changer. -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Comment Edited] (THRIFT-4611) NodeJS thrift enums not mappable back to their string values
[ https://issues.apache.org/jira/browse/THRIFT-4611?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16570241#comment-16570241 ] Brian Forbis edited comment on THRIFT-4611 at 8/6/18 2:15 PM: -- I don't believe there is a built in way to do this with just the generated JS code. For enums, it creates a key value object where key is the enum name and value is the enum code. If you reverse the object, you could use it in the other direction to get the enum name. Lodash has an invert method that can be used for this: {code:java} const _ = require('lodash'); const enumCode = 3; const enumString = _.invert(enumObject)[enumCode] {code} If however you are trying to build a generic tool that can stringify a thrift object for display to humans, I don't think there is a way to automatically convert the enum code into a string without knowing that you need to do it first. For now, if you really wanted to build a generic tool to do this dynamically, you may need to manually parse the thrift IDL yourself to get that info. Otherwise this would need a redesign of the generated thrift code to support this feature. was (Author: bforbis): I don't believe there is a built in way to do this with just the generated JS code. For enums, it creates a key value object where key is the enum name and value is the enum code. If you reverse the object, you could use it in the other direction to get the enum name. Lodash has an invert method that can be used for this: {code:java} const _ = require('lodash'); const enumCode = 3; const enumString = _.invert(enumObject)[enumCode] {code} > NodeJS thrift enums not mappable back to their string values > > > Key: THRIFT-4611 > URL: https://issues.apache.org/jira/browse/THRIFT-4611 > Project: Thrift > Issue Type: Wish > Components: JavaScript - Library, Node.js - Library >Affects Versions: 0.11.0 >Reporter: Anthony Alayo >Priority: Minor > > While attempting to build a javascript-based web tool to read encoded thrift > objects, I hit a wall. While I was able to decode from a base64 thrift string > to an object, all enum fields are only in their numeral representations with > no way to reverse them. > > Here is one example, where there is a Common.thrift file containing enums, > and an Event.thrift using one of the enums from Common.thrift. > > Here is the generated Event_types.js: > {code:java} > var Common_ttypes = require('./Common_types'); > var ttypes = module.exports = {}; > var Event = module.exports.Event = function(args) { > this.id = null; > this.type = null; > ... > } > {code} > Here is the generated Common_types.js > {code:java} > var ttypes = module.exports = {}; > ttypes.EventType = { > 'OUTBOUND' : 0, > 'INBOUND' : 1, > }{code} > While Common_types is being required in Event_types, there is no use of it > anywhere. The field type is solely being treated as a number: > {code:java} > if (this.type !== null && this.type !== undefined) { > output.writeFieldBegin('type', Thrift.Type.I32, 3); > output.writeI32(this.type); > output.writeFieldEnd(); > } > {code} > > It seems to be a bug, since I assume from the import that we would expect to > use enums instead of numbers. Perhaps this is intended, but if that's the > case, could this be made into a feature? The ability to know and display the > enum string values instead of a number is a game changer. -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Comment Edited] (THRIFT-4611) NodeJS thrift enums not mappable back to their string values
[ https://issues.apache.org/jira/browse/THRIFT-4611?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16570241#comment-16570241 ] Brian Forbis edited comment on THRIFT-4611 at 8/6/18 2:07 PM: -- I don't believe there is a built in way to do this with just the generated JS code. For enums, it creates a key value object where key is the enum name and value is the enum code. If you reverse the object, you could use it in the other direction to get the enum name. Lodash has an invert method that can be used for this: {code:java} const _ = require('lodash'); const enumCode = 3; const enumString = _.invert(enumObject)[enumCode] {code} was (Author: bforbis): I don't believe there is a built in way to do this with just the generated JS code. For enums, it creates a key value object where key is the enum name and value is the enum code. If you reverse the object, you could use it in the other direction to get the enum name. Lodash has an invert method that can be used for this: ``` const _ = require('lodash'); const enumCode = 3; const enumString = _.invert(enumObject)[enumCode] ``` > NodeJS thrift enums not mappable back to their string values > > > Key: THRIFT-4611 > URL: https://issues.apache.org/jira/browse/THRIFT-4611 > Project: Thrift > Issue Type: Bug > Components: Node.js - Compiler >Affects Versions: 0.11.0 >Reporter: Anthony Alayo >Priority: Major > > While attempting to build a javascript-based web tool to read encoded thrift > objects, I hit a wall. While I was able to decode from a base64 thrift string > to an object, all enum fields are only in their numeral representations with > no way to reverse them. > > Here is one example, where there is a Common.thrift file containing enums, > and an Event.thrift using one of the enums from Common.thrift. > > Here is the generated Event_types.js: > {code:java} > var Common_ttypes = require('./Common_types'); > var ttypes = module.exports = {}; > var Event = module.exports.Event = function(args) { > this.id = null; > this.type = null; > ... > } > {code} > Here is the generated Common_types.js > {code:java} > var ttypes = module.exports = {}; > ttypes.EventType = { > 'OUTBOUND' : 0, > 'INBOUND' : 1, > }{code} > While Common_types is being required in Event_types, there is no use of it > anywhere. The field type is solely being treated as a number: > {code:java} > if (this.type !== null && this.type !== undefined) { > output.writeFieldBegin('type', Thrift.Type.I32, 3); > output.writeI32(this.type); > output.writeFieldEnd(); > } > {code} > > It seems to be a bug, since I assume from the import that we would expect to > use enums instead of numbers. Perhaps this is intended, but if that's the > case, could this be made into a feature? The ability to know and display the > enum string values instead of a number is a game changer. -- This message was sent by Atlassian JIRA (v7.6.3#76005)