Skip to content

Instantly share code, notes, and snippets.

@zxt5105515
Created February 23, 2023 08:48
Show Gist options
  • Save zxt5105515/826229a73d00e20793efd0d8a64ba3b1 to your computer and use it in GitHub Desktop.
Save zxt5105515/826229a73d00e20793efd0d8a64ba3b1 to your computer and use it in GitHub Desktop.
protobufjs enum to string ,string to enum
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