|
| 1 | +// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. |
| 2 | +// Source: src/foo/A.proto |
| 3 | + |
| 4 | +import type { ByteSource } from "twirpscript"; |
| 5 | +import { BinaryReader, BinaryWriter } from "twirpscript"; |
| 6 | + |
| 7 | +//========================================// |
| 8 | +// Types // |
| 9 | +//========================================// |
| 10 | + |
| 11 | +export interface Foo { |
| 12 | + name: string; |
| 13 | +} |
| 14 | + |
| 15 | +//========================================// |
| 16 | +// Protobuf Encode / Decode // |
| 17 | +//========================================// |
| 18 | + |
| 19 | +export const Foo = { |
| 20 | + /** |
| 21 | + * Serializes a Foo to protobuf. |
| 22 | + */ |
| 23 | + encode: function (foo: Partial<Foo>): Uint8Array { |
| 24 | + return Foo._writeMessage(foo, new BinaryWriter()).getResultBuffer(); |
| 25 | + }, |
| 26 | + |
| 27 | + /** |
| 28 | + * Deserializes a Foo from protobuf. |
| 29 | + */ |
| 30 | + decode: function (bytes: ByteSource): Foo { |
| 31 | + return Foo._readMessage(Foo.initialize(), new BinaryReader(bytes)); |
| 32 | + }, |
| 33 | + |
| 34 | + /** |
| 35 | + * Serializes a Foo to JSON. |
| 36 | + */ |
| 37 | + encodeJSON: function (foo: Partial<Foo>): string { |
| 38 | + return JSON.stringify(Foo._writeMessageJSON(foo)); |
| 39 | + }, |
| 40 | + |
| 41 | + /** |
| 42 | + * Deserializes a Foo from JSON. |
| 43 | + */ |
| 44 | + decodeJSON: function (json: string): Foo { |
| 45 | + return Foo._readMessageJSON(Foo.initialize(), JSON.parse(json)); |
| 46 | + }, |
| 47 | + |
| 48 | + /** |
| 49 | + * Initializes a Foo with all fields set to their default value. |
| 50 | + */ |
| 51 | + initialize: function (): Foo { |
| 52 | + return { |
| 53 | + name: "", |
| 54 | + }; |
| 55 | + }, |
| 56 | + |
| 57 | + /** |
| 58 | + * @private |
| 59 | + */ |
| 60 | + _writeMessage: function ( |
| 61 | + msg: Partial<Foo>, |
| 62 | + writer: BinaryWriter |
| 63 | + ): BinaryWriter { |
| 64 | + if (msg.name) { |
| 65 | + writer.writeString(1, msg.name); |
| 66 | + } |
| 67 | + return writer; |
| 68 | + }, |
| 69 | + |
| 70 | + /** |
| 71 | + * @private |
| 72 | + */ |
| 73 | + _writeMessageJSON: function (msg: Partial<Foo>): Record<string, unknown> { |
| 74 | + const json: Record<string, unknown> = {}; |
| 75 | + if (msg.name) { |
| 76 | + json.name = msg.name; |
| 77 | + } |
| 78 | + return json; |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * @private |
| 83 | + */ |
| 84 | + _readMessage: function (msg: Foo, reader: BinaryReader): Foo { |
| 85 | + while (reader.nextField()) { |
| 86 | + const field = reader.getFieldNumber(); |
| 87 | + switch (field) { |
| 88 | + case 1: { |
| 89 | + msg.name = reader.readString(); |
| 90 | + break; |
| 91 | + } |
| 92 | + default: { |
| 93 | + reader.skipField(); |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return msg; |
| 99 | + }, |
| 100 | + |
| 101 | + /** |
| 102 | + * @private |
| 103 | + */ |
| 104 | + _readMessageJSON: function (msg: Foo, json: any): Foo { |
| 105 | + const name = json.name ?? json.name; |
| 106 | + if (name) { |
| 107 | + msg.name = name; |
| 108 | + } |
| 109 | + return msg; |
| 110 | + }, |
| 111 | +}; |
0 commit comments