Created
February 23, 2023 08:48
-
-
Save zxt5105515/826229a73d00e20793efd0d8a64ba3b1 to your computer and use it in GitHub Desktop.
protobufjs enum to string ,string to enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ProtoBuf = require('protobufjs'); | |
//create proto root | |
const myProtoRoot = new ProtoBuf.Root() | |
//load proto file | |
/* | |
one of the proto file | |
enum Test_Enum | |
{ | |
Red = 0; | |
Green = 1; | |
*/ | |
myProtoRoot.loadSync(myProtoFileList) | |
// | |
//find the enum proto type | |
let EnumProto = myProtoRoot.lookupEnum("Test_Enum"); | |
// int to string | |
let intEnum = 1 | |
let strEnum = EnumProto.valuesById[intEnum] | |
console.log(strEnum) //should print Red | |
// string to int | |
let strEnum2 = 'Green' | |
let intEnum2 = EnumProto.values[strEnum2] | |
console.log(intEnum2) //should print 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment