Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
Cómo convertir objetos a datos protobuf en nodejs

Si tengo un mensaje proto3 definido como a continuación

 message Log { string object = 1; string key_result = 2; string review = 3; repeated string tag = 4; string begin_at = 5; string end_at = 6; string created_at = 7; string id = 8; }

el protoc con --js_out y --ts_out generará dts como

 export class Log extends jspb.Message { getObject(): string; setObject(value: string): void; getKeyResult(): string; setKeyResult(value: string): void; getReview(): string; setReview(value: string): void; clearTagList(): void; getTagList(): Array<string>; setTagList(value: Array<string>): void; addTag(value: string, index?: number): string; getBeginAt(): string; setBeginAt(value: string): void; getEndAt(): string; setEndAt(value: string): void; getCreatedAt(): string; setCreatedAt(value: string): void; getId(): string; setId(value: string): void; serializeBinary(): Uint8Array; toObject(includeInstance?: boolean): Log.AsObject; static toObject(includeInstance: boolean, msg: Log): Log.AsObject; static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>}; static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>}; static serializeBinaryToWriter(message: Log, writer: jspb.BinaryWriter): void; static deserializeBinary(bytes: Uint8Array): Log; static deserializeBinaryFromReader(message: Log, reader: jspb.BinaryReader): Log; }

Es simple convertir la clase de Log en objeto a través del método toObject . Pero para convertir el objeto a la clase, necesito configurar cada campo manualmente.

 export function toLog(log: Log.AsObject): Log { const t = new Log t.setObject(log.object) t.setKeyResult(log.keyResult) t.setReview(log.review) t.setTagList(log.tagList) t.setBeginAt(log.beginAt) t.setEndAt(log.endAt) t.setCreatedAt(log.createdAt) t.setId(log.id) return t }

¿Alguna forma más sencilla de realizar tal tarea?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!