From b243d324ddc86ad5a1bc28d2286ee49671949208 Mon Sep 17 00:00:00 2001 From: Vilsol Date: Thu, 20 Jan 2022 14:24:56 +0200 Subject: [PATCH 1/3] feat: add usePrototypeForDefaults option --- src/main.ts | 14 ++++++++++++-- src/options.ts | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index f13a90267..be90bd2df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -758,6 +758,11 @@ function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorP const { options, utils, typeMap } = ctx; const chunks: Code[] = []; + let createBase = code`createBase${fullName}()`; + if (options.usePrototypeForDefaults) { + createBase = code`Object.create(${createBase}) as ${fullName}`; + } + // create the basic function declaration chunks.push(code` decode( @@ -766,7 +771,7 @@ function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorP ): ${fullName} { const reader = input instanceof ${Reader} ? input : new ${Reader}(input); let end = length === undefined ? reader.len : reader.pos + length; - const message = createBase${fullName}(); + const message = ${createBase}; `); if (options.unknownFields) { @@ -1400,7 +1405,12 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri `); } - chunks.push(code`const message = createBase${fullName}();`); + let createBase = code`createBase${fullName}()`; + if (options.usePrototypeForDefaults) { + createBase = code`Object.create(${createBase}) as ${fullName}`; + } + + chunks.push(code`const message = ${createBase};`); // add a check for each incoming field messageDesc.field.forEach((field) => { diff --git a/src/options.ts b/src/options.ts index 0cc465a41..f1529486d 100644 --- a/src/options.ts +++ b/src/options.ts @@ -61,6 +61,7 @@ export type Options = { emitImportedFiles: boolean; useExactTypes: boolean; unknownFields: boolean; + usePrototypeForDefaults: boolean; }; export function defaultOptions(): Options { @@ -96,6 +97,7 @@ export function defaultOptions(): Options { emitImportedFiles: true, useExactTypes: true, unknownFields: false, + usePrototypeForDefaults: false, }; } From 8ca195c88d9600c9d69b6849bd773e1b40a77016 Mon Sep 17 00:00:00 2001 From: Vilsol Date: Thu, 20 Jan 2022 14:33:43 +0200 Subject: [PATCH 2/3] chore: add prototype test chore: update options test --- .../google/protobuf/timestamp.ts | 218 ++ .../google/protobuf/wrappers.bin | Bin 0 -> 4316 bytes .../google/protobuf/wrappers.proto | 118 + .../google/protobuf/wrappers.ts | 604 ++++ .../google/type/date.bin | Bin 0 -> 2086 bytes .../google/type/date.proto | 50 + .../google/type/date.ts | 131 + .../import_dir/thing.bin | Bin 0 -> 6652 bytes .../import_dir/thing.proto | 7 + .../import_dir/thing.ts | 109 + .../simple-prototype-defaults/parameters.txt | 1 + .../simple-prototype-defaults/simple-test.ts | 22 + .../simple-prototype-defaults/simple.bin | Bin 0 -> 21978 bytes .../simple-prototype-defaults/simple.proto | 152 + .../simple-prototype-defaults/simple.ts | 2437 +++++++++++++++++ tests/options-test.ts | 1 + 16 files changed, 3850 insertions(+) create mode 100644 integration/simple-prototype-defaults/google/protobuf/timestamp.ts create mode 100644 integration/simple-prototype-defaults/google/protobuf/wrappers.bin create mode 100644 integration/simple-prototype-defaults/google/protobuf/wrappers.proto create mode 100644 integration/simple-prototype-defaults/google/protobuf/wrappers.ts create mode 100644 integration/simple-prototype-defaults/google/type/date.bin create mode 100644 integration/simple-prototype-defaults/google/type/date.proto create mode 100644 integration/simple-prototype-defaults/google/type/date.ts create mode 100644 integration/simple-prototype-defaults/import_dir/thing.bin create mode 100644 integration/simple-prototype-defaults/import_dir/thing.proto create mode 100644 integration/simple-prototype-defaults/import_dir/thing.ts create mode 100644 integration/simple-prototype-defaults/parameters.txt create mode 100644 integration/simple-prototype-defaults/simple-test.ts create mode 100644 integration/simple-prototype-defaults/simple.bin create mode 100644 integration/simple-prototype-defaults/simple.proto create mode 100644 integration/simple-prototype-defaults/simple.ts diff --git a/integration/simple-prototype-defaults/google/protobuf/timestamp.ts b/integration/simple-prototype-defaults/google/protobuf/timestamp.ts new file mode 100644 index 000000000..fb67b68d3 --- /dev/null +++ b/integration/simple-prototype-defaults/google/protobuf/timestamp.ts @@ -0,0 +1,218 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; + +export const protobufPackage = 'google.protobuf'; + +/** + * A Timestamp represents a point in time independent of any time zone or local + * calendar, encoded as a count of seconds and fractions of seconds at + * nanosecond resolution. The count is relative to an epoch at UTC midnight on + * January 1, 1970, in the proleptic Gregorian calendar which extends the + * Gregorian calendar backwards to year one. + * + * All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap + * second table is needed for interpretation, using a [24-hour linear + * smear](https://developers.google.com/time/smear). + * + * The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By + * restricting to that range, we ensure that we can convert to and from [RFC + * 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. + * + * # Examples + * + * Example 1: Compute Timestamp from POSIX `time()`. + * + * Timestamp timestamp; + * timestamp.set_seconds(time(NULL)); + * timestamp.set_nanos(0); + * + * Example 2: Compute Timestamp from POSIX `gettimeofday()`. + * + * struct timeval tv; + * gettimeofday(&tv, NULL); + * + * Timestamp timestamp; + * timestamp.set_seconds(tv.tv_sec); + * timestamp.set_nanos(tv.tv_usec * 1000); + * + * Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. + * + * FILETIME ft; + * GetSystemTimeAsFileTime(&ft); + * UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + * + * // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z + * // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. + * Timestamp timestamp; + * timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); + * timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); + * + * Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. + * + * long millis = System.currentTimeMillis(); + * + * Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) + * .setNanos((int) ((millis % 1000) * 1000000)).build(); + * + * + * Example 5: Compute Timestamp from Java `Instant.now()`. + * + * Instant now = Instant.now(); + * + * Timestamp timestamp = + * Timestamp.newBuilder().setSeconds(now.getEpochSecond()) + * .setNanos(now.getNano()).build(); + * + * + * Example 6: Compute Timestamp from current time in Python. + * + * timestamp = Timestamp() + * timestamp.GetCurrentTime() + * + * # JSON Mapping + * + * In JSON format, the Timestamp type is encoded as a string in the + * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the + * format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" + * where {year} is always expressed using four digits while {month}, {day}, + * {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional + * seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), + * are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone + * is required. A proto3 JSON serializer should always use UTC (as indicated by + * "Z") when printing the Timestamp type and a proto3 JSON parser should be + * able to accept both UTC and other timezones (as indicated by an offset). + * + * For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past + * 01:30 UTC on January 15, 2017. + * + * In JavaScript, one can convert a Date object to this format using the + * standard + * [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) + * method. In Python, a standard `datetime.datetime` object can be converted + * to this format using + * [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with + * the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use + * the Joda Time's [`ISODateTimeFormat.dateTime()`]( + * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D + * ) to obtain a formatter capable of generating timestamps in this format. + */ +export interface Timestamp { + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + */ + seconds: number; + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + */ + nanos: number; +} + +function createBaseTimestamp(): Timestamp { + return { seconds: 0, nanos: 0 }; +} + +export const Timestamp = { + encode(message: Timestamp, writer: Writer = Writer.create()): Writer { + if (message.seconds !== 0) { + writer.uint32(8).int64(message.seconds); + } + if (message.nanos !== 0) { + writer.uint32(16).int32(message.nanos); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Timestamp { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseTimestamp()) as Timestamp; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.seconds = longToNumber(reader.int64() as Long); + break; + case 2: + message.nanos = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Timestamp { + return { + seconds: isSet(object.seconds) ? Number(object.seconds) : 0, + nanos: isSet(object.nanos) ? Number(object.nanos) : 0, + }; + }, + + toJSON(message: Timestamp): unknown { + const obj: any = {}; + message.seconds !== undefined && (obj.seconds = Math.round(message.seconds)); + message.nanos !== undefined && (obj.nanos = Math.round(message.nanos)); + return obj; + }, + + fromPartial, I>>(object: I): Timestamp { + const message = Object.create(createBaseTimestamp()) as Timestamp; + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; + return message; + }, +}; + +declare var self: any | undefined; +declare var window: any | undefined; +declare var global: any | undefined; +var globalThis: any = (() => { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + throw 'Unable to locate global object'; +})(); + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + +function longToNumber(long: Long): number { + if (long.gt(Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); + } + return long.toNumber(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/integration/simple-prototype-defaults/google/protobuf/wrappers.bin b/integration/simple-prototype-defaults/google/protobuf/wrappers.bin new file mode 100644 index 0000000000000000000000000000000000000000..7f34d6ce0cd3a5ebf8b18b31567e0fd536c12f1b GIT binary patch literal 4316 zcmbtXOK;oQ6{a4J6^=6#(_I7xnw)Vum?Tx?fs+o3OuC>X+GZ+CDoNSnObVD1uVfA= z(%{2}i~gD}yJ&wy(ZAG1m;KHq^-AqHz-VFL$9a6`JLjHzsXm@W(IgPN7jcwE!)&~J z6}uM~B2F4|WxH0ZJluLz+EM=U&#%=xgtf17W4S7f?L1Y#Xh+#F5XWwi3GG+v+9eMj zm8kTrWaX2c9aY^AA~(H3ro1F$hUxdOZ;+|XWZ-OYn^1Ko1ZR+WwReM|wV6y|=tiLj zGoi25`^_i{Zm?6EiNKDX#(p@tLFoQW2!NW`sYq^+*(hY1e^s|e(X{aqyUhnDMXvgs zyngBZi;16}XTt`BcPCNchLc5JFVgD^k<8Qk-%{ys<;p?7`SrJ(~IQI z?ye^;MG(o_ESTUpWtjrROCVl45u?EMry>TEH-mt)B`#(l5TloYsyPmI0pu6qP&p55)r&^iRD`Ldm{YO~ z{v+(fgh~;y?*_?&YMCHxs7>UdyYyS^T>1{=k)5*?f>ghZ=a3~t91x7deiKf&Z%^`Z#rFew%@Z<(y6ao zj@cS?bc^}}tKYK?;>g;j-RkJ(k$N4>wqYHcEyMnSIz6U;p9Z!8AZ^{zrEAy#UD!te z&4F!7;>@mNSk|ELn7!_EWcdWlAaotNZOL%2%h7VJ7(MHh%`y)%19dt%G_a0DcXPIM zCd5W@w47xzI7F==(Tg~#YaDdU1EbqA7*CJQoS3%p90@al?SO4E%_lmXqEO(ND-yVn zA|5Z5E=Wc`$=oNseGDV*nKUZEHj8qBl09f07MwTKz5kSGc7<^t=97XBi<3pq!YF(> z6-nYw#B-8YSA(2$TnhO_He{UCaTcgjRZNG%^SA-ejU=;1b5>jzIKJ|Y)6m1~GigU& z1k!WhjszOQ6?zDcFnWl{y8t&ESk3(89L*uU5;(%#N8Di|LbOM;8F%3ly#QAx`hqV4 zPZ~#?LAMyYS&-5tzkP7(Yig;iRlZRk@u;R%zEyru@2ln7H~BDMe5O2+i%+x)5A((6 ziY^zw&?-F47hfoKxmeOFJj@q=t2E>y%=0i`tSdWmQPnCu%okrOc*?;0Z?p;zCu+H> zXw^OCccty0X6eB#CQ~7)KJ(<0?o9SCQ1Q-2g~sB7uk2YjXHL(So_=S97AXb844J}Z zgkPnq0P|jLOC_~hRmzH1d0pPx-r_{#cHnp8oU;vejV0Dtq^1@#XurPy<6pc=Z-HL|fHyTweFFF;hX1y#ZEF&~bg_aS0zm(^ zye{!#2IaT42eTQ_zkRqV(U-MqQ!#Euk5|?AukS{`cnUAj^P?!76y%oypjn_VGy3)% zy>zjH9s)q$F3@8J<#vG{GoWt+Jr_?!tG-i?ZY`edGvO~i4Zl|O%y{?CEW^nV)v8X&wc=wD;}A6Dr9C;&(x zfDZ-zm_hqPK|f{)-~;H7P(S$A7%HN)J-Yeq4)M`-H~aHXvjzEQX`G2O*_6-5cv=d! z!cFKNpbTr9>SLVZdmM;Y-rU}lro&J znckX*{3PBT$kGSoB9xV5ybcII7kOA`_`y66x!g(skU#)Ikq6A69Ta)M3;_hiF{o+P zSjlcZ21EX6y&M1H+x8atH2{cHHW-7c1x?0Q6~b3^0RoS{wt+fIfY=IlLz? H`d9x0-c*59 literal 0 HcmV?d00001 diff --git a/integration/simple-prototype-defaults/google/protobuf/wrappers.proto b/integration/simple-prototype-defaults/google/protobuf/wrappers.proto new file mode 100644 index 000000000..01947639a --- /dev/null +++ b/integration/simple-prototype-defaults/google/protobuf/wrappers.proto @@ -0,0 +1,118 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Wrappers for primitive (non-message) types. These types are useful +// for embedding primitives in the `google.protobuf.Any` type and for places +// where we need to distinguish between the absence of a primitive +// typed field and its default value. + +syntax = "proto3"; + +package google.protobuf; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option cc_enable_arenas = true; +option go_package = "github.com/golang/protobuf/ptypes/wrappers"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "WrappersProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// Wrapper message for `double`. +// +// The JSON representation for `DoubleValue` is JSON number. +message DoubleValue { + // The double value. + double value = 1; +} + +// Wrapper message for `float`. +// +// The JSON representation for `FloatValue` is JSON number. +message FloatValue { + // The float value. + float value = 1; +} + +// Wrapper message for `int64`. +// +// The JSON representation for `Int64Value` is JSON string. +message Int64Value { + // The int64 value. + int64 value = 1; +} + +// Wrapper message for `uint64`. +// +// The JSON representation for `UInt64Value` is JSON string. +message UInt64Value { + // The uint64 value. + uint64 value = 1; +} + +// Wrapper message for `int32`. +// +// The JSON representation for `Int32Value` is JSON number. +message Int32Value { + // The int32 value. + int32 value = 1; +} + +// Wrapper message for `uint32`. +// +// The JSON representation for `UInt32Value` is JSON number. +message UInt32Value { + // The uint32 value. + uint32 value = 1; +} + +// Wrapper message for `bool`. +// +// The JSON representation for `BoolValue` is JSON `true` and `false`. +message BoolValue { + // The bool value. + bool value = 1; +} + +// Wrapper message for `string`. +// +// The JSON representation for `StringValue` is JSON string. +message StringValue { + // The string value. + string value = 1; +} + +// Wrapper message for `bytes`. +// +// The JSON representation for `BytesValue` is JSON string. +message BytesValue { + // The bytes value. + bytes value = 1; +} diff --git a/integration/simple-prototype-defaults/google/protobuf/wrappers.ts b/integration/simple-prototype-defaults/google/protobuf/wrappers.ts new file mode 100644 index 000000000..a607a0237 --- /dev/null +++ b/integration/simple-prototype-defaults/google/protobuf/wrappers.ts @@ -0,0 +1,604 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; + +export const protobufPackage = 'google.protobuf'; + +/** + * Wrapper message for `double`. + * + * The JSON representation for `DoubleValue` is JSON number. + */ +export interface DoubleValue { + /** The double value. */ + value: number; +} + +/** + * Wrapper message for `float`. + * + * The JSON representation for `FloatValue` is JSON number. + */ +export interface FloatValue { + /** The float value. */ + value: number; +} + +/** + * Wrapper message for `int64`. + * + * The JSON representation for `Int64Value` is JSON string. + */ +export interface Int64Value { + /** The int64 value. */ + value: number; +} + +/** + * Wrapper message for `uint64`. + * + * The JSON representation for `UInt64Value` is JSON string. + */ +export interface UInt64Value { + /** The uint64 value. */ + value: number; +} + +/** + * Wrapper message for `int32`. + * + * The JSON representation for `Int32Value` is JSON number. + */ +export interface Int32Value { + /** The int32 value. */ + value: number; +} + +/** + * Wrapper message for `uint32`. + * + * The JSON representation for `UInt32Value` is JSON number. + */ +export interface UInt32Value { + /** The uint32 value. */ + value: number; +} + +/** + * Wrapper message for `bool`. + * + * The JSON representation for `BoolValue` is JSON `true` and `false`. + */ +export interface BoolValue { + /** The bool value. */ + value: boolean; +} + +/** + * Wrapper message for `string`. + * + * The JSON representation for `StringValue` is JSON string. + */ +export interface StringValue { + /** The string value. */ + value: string; +} + +/** + * Wrapper message for `bytes`. + * + * The JSON representation for `BytesValue` is JSON string. + */ +export interface BytesValue { + /** The bytes value. */ + value: Uint8Array; +} + +function createBaseDoubleValue(): DoubleValue { + return { value: 0 }; +} + +export const DoubleValue = { + encode(message: DoubleValue, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(9).double(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): DoubleValue { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseDoubleValue()) as DoubleValue; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.double(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): DoubleValue { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: DoubleValue): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>(object: I): DoubleValue { + const message = Object.create(createBaseDoubleValue()) as DoubleValue; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseFloatValue(): FloatValue { + return { value: 0 }; +} + +export const FloatValue = { + encode(message: FloatValue, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(13).float(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): FloatValue { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseFloatValue()) as FloatValue; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.float(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): FloatValue { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: FloatValue): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>(object: I): FloatValue { + const message = Object.create(createBaseFloatValue()) as FloatValue; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseInt64Value(): Int64Value { + return { value: 0 }; +} + +export const Int64Value = { + encode(message: Int64Value, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(8).int64(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Int64Value { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseInt64Value()) as Int64Value; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = longToNumber(reader.int64() as Long); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Int64Value { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: Int64Value): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>(object: I): Int64Value { + const message = Object.create(createBaseInt64Value()) as Int64Value; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseUInt64Value(): UInt64Value { + return { value: 0 }; +} + +export const UInt64Value = { + encode(message: UInt64Value, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(8).uint64(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): UInt64Value { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseUInt64Value()) as UInt64Value; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = longToNumber(reader.uint64() as Long); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): UInt64Value { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: UInt64Value): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>(object: I): UInt64Value { + const message = Object.create(createBaseUInt64Value()) as UInt64Value; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseInt32Value(): Int32Value { + return { value: 0 }; +} + +export const Int32Value = { + encode(message: Int32Value, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(8).int32(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Int32Value { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseInt32Value()) as Int32Value; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Int32Value { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: Int32Value): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>(object: I): Int32Value { + const message = Object.create(createBaseInt32Value()) as Int32Value; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseUInt32Value(): UInt32Value { + return { value: 0 }; +} + +export const UInt32Value = { + encode(message: UInt32Value, writer: Writer = Writer.create()): Writer { + if (message.value !== 0) { + writer.uint32(8).uint32(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): UInt32Value { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseUInt32Value()) as UInt32Value; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.uint32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): UInt32Value { + return { + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: UInt32Value): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>(object: I): UInt32Value { + const message = Object.create(createBaseUInt32Value()) as UInt32Value; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseBoolValue(): BoolValue { + return { value: false }; +} + +export const BoolValue = { + encode(message: BoolValue, writer: Writer = Writer.create()): Writer { + if (message.value === true) { + writer.uint32(8).bool(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): BoolValue { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseBoolValue()) as BoolValue; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.bool(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): BoolValue { + return { + value: isSet(object.value) ? Boolean(object.value) : false, + }; + }, + + toJSON(message: BoolValue): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>(object: I): BoolValue { + const message = Object.create(createBaseBoolValue()) as BoolValue; + message.value = object.value ?? false; + return message; + }, +}; + +function createBaseStringValue(): StringValue { + return { value: '' }; +} + +export const StringValue = { + encode(message: StringValue, writer: Writer = Writer.create()): Writer { + if (message.value !== '') { + writer.uint32(10).string(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): StringValue { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseStringValue()) as StringValue; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): StringValue { + return { + value: isSet(object.value) ? String(object.value) : '', + }; + }, + + toJSON(message: StringValue): unknown { + const obj: any = {}; + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>(object: I): StringValue { + const message = Object.create(createBaseStringValue()) as StringValue; + message.value = object.value ?? ''; + return message; + }, +}; + +function createBaseBytesValue(): BytesValue { + return { value: new Uint8Array() }; +} + +export const BytesValue = { + encode(message: BytesValue, writer: Writer = Writer.create()): Writer { + if (message.value.length !== 0) { + writer.uint32(10).bytes(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): BytesValue { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseBytesValue()) as BytesValue; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.value = reader.bytes(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): BytesValue { + return { + value: isSet(object.value) ? bytesFromBase64(object.value) : new Uint8Array(), + }; + }, + + toJSON(message: BytesValue): unknown { + const obj: any = {}; + message.value !== undefined && + (obj.value = base64FromBytes(message.value !== undefined ? message.value : new Uint8Array())); + return obj; + }, + + fromPartial, I>>(object: I): BytesValue { + const message = Object.create(createBaseBytesValue()) as BytesValue; + message.value = object.value ?? new Uint8Array(); + return message; + }, +}; + +declare var self: any | undefined; +declare var window: any | undefined; +declare var global: any | undefined; +var globalThis: any = (() => { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + throw 'Unable to locate global object'; +})(); + +const atob: (b64: string) => string = + globalThis.atob || ((b64) => globalThis.Buffer.from(b64, 'base64').toString('binary')); +function bytesFromBase64(b64: string): Uint8Array { + const bin = atob(b64); + const arr = new Uint8Array(bin.length); + for (let i = 0; i < bin.length; ++i) { + arr[i] = bin.charCodeAt(i); + } + return arr; +} + +const btoa: (bin: string) => string = + globalThis.btoa || ((bin) => globalThis.Buffer.from(bin, 'binary').toString('base64')); +function base64FromBytes(arr: Uint8Array): string { + const bin: string[] = []; + for (const byte of arr) { + bin.push(String.fromCharCode(byte)); + } + return btoa(bin.join('')); +} + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + +function longToNumber(long: Long): number { + if (long.gt(Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); + } + return long.toNumber(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/integration/simple-prototype-defaults/google/type/date.bin b/integration/simple-prototype-defaults/google/type/date.bin new file mode 100644 index 0000000000000000000000000000000000000000..680ba7e6dd305c29b45f37db9716864b397b1a72 GIT binary patch literal 2086 zcmZ`)%~Bgj5Z)i*$OAEAO&p4gJ1SS17?QCX8 z2yx4+_yJO>%00)tKwc)tbk8mVQox1Tp6TxI`}#-hQKVE9i@KSmqCVh8gsE0Wt(VJw z<>8uJbuNCZusa?Fi@9@1&ea`O+C%>!V5OPhdd&rQW1(%&!75uAtHg}fJZsE9f(r8o ze75FW8y$B(vgN)Shu6aH+^3)?gz3cn@Lh2eshB5GsB~11L}C%w^DCap?2625V)U=; ze)jx>?#Yk8KVbKQ(z3JVvcF2qT?+hVC)ob81iLDoX&H?SY&W)E!hzKU4i9%j#^6x) zMUsgDOp<}nU`7I(DevRI;-&^a37tumz;@Vx4SG;5uBzX@W-wC|81oq<%D^NO7(r$* zl(7JD-WRC>nE=vGW66`gfT=Vio27^mGB_2G9A`l?=^ zPNyNaK&?=5P9v)yws!ZAI{VKNEIPbPVv%K_#jg{okz{WMJVhvd-a~*fpMuiBBQ5aG zD1tiG(#RyLL8gXg%C%rHkeSi4H!(Lg(=j)G=Ekp` z4Qq4*pN_e4&H2U}p9DS~|0yxALKk&1a*RGhd@^GsMsB!Ji2yA2rSyfGSa1<%Vv5zUiRcCO zc_yQT#O8^~qf$aft)NAWx}ISkWy^xI9peyST@JK}l*S=|-2x`Lq6;;8|M}X+47*srYgr{R z*%Diz1Phemi_0%e{uGlJcRqeCS6F1B;MJxVtbc&F^qWGhO6uN*Wo!EmB(@yXHi!f` zyV)&sNG?L!oI`RcZ~4mtl8cZwD=X{|*J8q5(C{kjpIt!V36ibzZJ1tcS^cpC>> 3) { + case 1: + message.year = reader.int32(); + break; + case 2: + message.month = reader.int32(); + break; + case 3: + message.day = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): DateMessage { + return { + year: isSet(object.year) ? Number(object.year) : 0, + month: isSet(object.month) ? Number(object.month) : 0, + day: isSet(object.day) ? Number(object.day) : 0, + }; + }, + + toJSON(message: DateMessage): unknown { + const obj: any = {}; + message.year !== undefined && (obj.year = Math.round(message.year)); + message.month !== undefined && (obj.month = Math.round(message.month)); + message.day !== undefined && (obj.day = Math.round(message.day)); + return obj; + }, + + fromPartial, I>>(object: I): DateMessage { + const message = Object.create(createBaseDateMessage()) as DateMessage; + message.year = object.year ?? 0; + message.month = object.month ?? 0; + message.day = object.day ?? 0; + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/integration/simple-prototype-defaults/import_dir/thing.bin b/integration/simple-prototype-defaults/import_dir/thing.bin new file mode 100644 index 0000000000000000000000000000000000000000..18377ed61039b9e0223782d6b1fd66d34cd8c98e GIT binary patch literal 6652 zcmbtZOLN=S6(&X5Q4ps|n6%?@o8(l9Y%--lQSzgf(*;FA5?qQ@3DAn{$ygw9DIo>{ z763)du9uxGGVM&e@21QCi!M6dbf&v5yX$}GvfsHE5Gg;Wt%j0-`#A4=&UX*BA9%x2 z7$px~FRCWTUT~z3qA&@sEG!h>|L}5tG56%JHSN=*Fg)@_RZjKBgKFXpMVvUpky>4R zPc2YySg?3Ud)v;|6fbLU#iAbuZhSdU`BI^iy%s;xE(A^x#+T z{W+o=-#a6@$1?8a@2{pyjzZso=wWm;Bjf2v#MMVZI0>fmj(Y#j=l?iY*xuXt)7&T9 zQY~FlgVg%I@cp0j2AlWZ{YzDQt9ap)-0$;w?QfT~{H0>ylU#B2&zDH<=!ZUSj0Xb| z#k5R{*0q>iCvnIN5)t)}A(Ws&7!937qeeJ7jl84dgjQEpewuC|GwAC?b>AmB7gHo+ z5uFHE*XTG&M)7*J>WUNLha)h`Yy$HrO(DcGL}wwYnnoSrdT|nYy>a4&0Xcz7<5)l( zioW8kN;W$p9dE!7)rt0*<69I~; z#WV`S#OsR+CcT(^=*SdiIY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^)xA11O_14PqV;QHex{E_Sfd_3Ys=Q;35Zt&|j)3W)zQ6`|B2vc`aE9L{J2sr8Y{ zY~0ISt@>Us!o@^3F!keP=zk{JW_uA~=(%toMum4~NAAGTwWWjQrQ!0DYcK7rFYT@` zS^Co8{xw`Iyhmc<#ptFXk+OlNUP3%8YIj4|ku?t;%?F3DKO4!0xCkNisHEcjih}cX zNp&jGsAQb|4R%UT@WV2TTXjaBbO`Ijqf(u!@_F?|+EP!pEHhYI-NZ+uG8;1N$qu2D z>WPaDVrwK12(E`mA`p?Ijv97KOvA@=HpCF2n)Z%Xn9t>l^WWwdKG6P<*XHMQbGhRD zclo)GuKYGno#_oXO;voG)Chv?dz%1J3o+e`9b=M;8W%r`{kh$;U zES&H~@m=i#i+4fF_g3}_>A%Y_yqCQe__g?<_AjLjW`BnU!2xE<7PzSk zI{eQUW9cB+Re#Q4vKrjJUBLyBf4O=3yixKn3iQm3l9y^Pyqg*&52O4p|NaNPiz>f9 z`OTd6imyA07h`-6;)^S9`le&?uJ*1ezcCT6&0m)kZ);jV5(rlJp_9n(H{MyjlC|k2 zWBz7=BU#2{=k0WLJ-K_LT`bP$au@U5hQXH$xm=OkFoj&Pkk6HQ5Iv%~+>> 3) { + case 1: + message.createdAt = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): ImportedThing { + return { + createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, + }; + }, + + toJSON(message: ImportedThing): unknown { + const obj: any = {}; + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); + return obj; + }, + + fromPartial, I>>(object: I): ImportedThing { + const message = Object.create(createBaseImportedThing()) as ImportedThing; + message.createdAt = object.createdAt ?? undefined; + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + +function toTimestamp(date: Date): Timestamp { + const seconds = date.getTime() / 1_000; + const nanos = (date.getTime() % 1_000) * 1_000_000; + return { seconds, nanos }; +} + +function fromTimestamp(t: Timestamp): Date { + let millis = t.seconds * 1_000; + millis += t.nanos / 1_000_000; + return new Date(millis); +} + +function fromJsonTimestamp(o: any): Date { + if (o instanceof Date) { + return o; + } else if (typeof o === 'string') { + return new Date(o); + } else { + return fromTimestamp(Timestamp.fromJSON(o)); + } +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/integration/simple-prototype-defaults/parameters.txt b/integration/simple-prototype-defaults/parameters.txt new file mode 100644 index 000000000..f1bb4e16d --- /dev/null +++ b/integration/simple-prototype-defaults/parameters.txt @@ -0,0 +1 @@ +usePrototypeForDefaults=true diff --git a/integration/simple-prototype-defaults/simple-test.ts b/integration/simple-prototype-defaults/simple-test.ts new file mode 100644 index 000000000..f6ddbd35a --- /dev/null +++ b/integration/simple-prototype-defaults/simple-test.ts @@ -0,0 +1,22 @@ +import { Child_Type, Simple, StateEnum } from './simple'; + +describe('simple', () => { + it('generates types correctly', () => { + const simple: Simple = { + birthday: undefined, + blob: Buffer.from([]), + blobs: [], + name: 'asdf', + age: 1, + child: { name: 'child', type: Child_Type.UNKNOWN }, + state: StateEnum.ON, + grandChildren: [{ name: 'grand1', type: Child_Type.UNKNOWN }, { name: 'grand2', type: Child_Type.UNKNOWN }], + coins: [2, 4, 6], + snacks: ['a', 'b'], + oldStates: [StateEnum.ON, StateEnum.OFF], + createdAt: new Date('1970-01-01T00:00:00.000Z'), + thing: undefined + }; + expect(simple.name).toEqual('asdf'); + }); +}); diff --git a/integration/simple-prototype-defaults/simple.bin b/integration/simple-prototype-defaults/simple.bin new file mode 100644 index 0000000000000000000000000000000000000000..321bb33c3cc2a8864d634fb214a2b1c6de0b688b GIT binary patch literal 21978 zcmeHv&2w8xk{{j!2!aKQA5WA-vGq|6i4rK00KcSZsiocJ2P8tGNDctDM(Wm(An-_` z1>(`bqbRnzXJ$Xfdc5P9lP`PpVaE{0I2pqkoxI z^&TLRVvp^cTcMdozRJwX%*v|Dtjdz;ZPj-7>TYJQ>G|GBA`u-pJ#3{SU;XKTIJ@I{ zJ9RhfAMUx?YT4IFPEvi((D&55h%KVYaYXFUEjNcPX{Eag{M#84UAtbxe>rUPM+G4UMnbxf5v$|!+tCt%)8LzpM-EkW%aaJ{z_iC+B z&QD3$|7BT!V@DsB)*k(BpNKoL{>YRi{#{I1y-u`0;!OYRm|XDo4x6=|m%f~yoVp_) zvbu74c_AZ&T&`8zM$4_r{YKSoO8=!R=l04K{71Kp%g@|qtL8Q2bY@bf2_dDMQe&SA zdFbuS-SVMqc)r|kx!_Q1$?aO*mF}C0yXVVVLqhGndac~3xbmRpzvNytkBpGdGzV|X zF9W#@^u0srZMUPzvJYYD&r9FmyPM4(92{iIEGQeQuGDB{m-7p`m11rJ!h&GEQFmJ{ z*>wNGeys^5w+>}_5294cTM)2bK9F8hmUo&i+I){hJ!sbaT4QHiw!CfspxktYtkzn7 zv$nPGcUVcwFQIKa0_;?7$kbd>=8GvgKUd5b$Ax^FFDp&(9sX6;BLL;9$+-M zBf=m*Z`F1hWbAUoS6rz>L9Wnr>#*x<^yBfcU~bnj2{0}Xo9>R+#3YalT1+AZ3Alhv z>vb1Sdqd92?frUPtHCp-;WZ|{a+{u{)n{Dtdc)#F5Ba)W-*;Pz@Q9XiHR;N-je)Wa z`Dz2wHe2N;%t0L!e+eh9!NlG{L(?+l5v+BmOhm?1C@fEBuMjgBkP2pE&4=DinD@K_n0EDD-#+Rn&x`Gocq za#gbKQL;=;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)nVZh4T@^K!pa-bUxSXl==XaiYU$8T}^CuX+nIn9vSG3bR&~jhAwF`3iK);r+A$4=x#gmQVk46a@&bZ%#Pp1?Px6l zfZdK3UOj=^IitE#PvCao_vAKs0{3O4uGmpb_hmwbg8WYHm1YM3>|WXDSRVBz?bWbi zr{1K!QtaYbiyDzVW)~-KXsDGa&O3=`XNds;X!7FhcLZUOy(DjZ1VLHv;|Z3ohaTwp zO1d70QdT&z*O51h9mR;Rv4+x62x51j ztpxz=4m3MJKs@MC3mzb_JHUdM1iPfu^;P5-mf}aj<5#%zCw@ud_g3=*0Df;ZKR`fy ztN8%}zqibf$k6n)4k`eE>}x$$0a2UIT>ydXYaLW1X_9;skdZfj6Oa+hZvrx!7vBVA z0DcmXMM3tHfQ%-|PXaQ^f`1Z_VUqkb&<*PjAoW~5$*bB=1Kntn{1m#;HkE`}v7e!a z1AAW=K?=lei2@NS|I7lV2C>`yj7laHDHfwY4M~v_5oky;RR9L0m@5DSQcRaW3rUd( z5oky;e*gxgNQD3fq?j6iZXxJWA|*fyu*ZNP{c}s;0*~cK0t8UU6iFL?`xh3bi)!ov zDL_LCM+O*>_JH&+tRZrLG826C=azk5lOnGnP!*b>5u`u2k`!Q(5s}#dJWHt@ktUtk zKMYBeAO&bh;hz8lQe-y(L3$sK-0Q^u(u({uJSWNCkPi?cqAHIob@(!G231DtUwcrM zH>j`KMok6E{WFvuMPo>SVt;8R&WjO<(F@;?eQP2A>pWZ_iP4520gXy1fROQ93z?~I z1*2~*oMCDZGJb0z20qPv(fBK7Q z3HuYkKxoW8fJk3ex0rkYVFeoGUJ?RTHJ5cG1zi9QCwf`_YYR!bBE_Bs@XUw-!Rgo5 z#Y entitiesById = 1; + map nameLookup = 2; + map intLookup = 3; + map mapOfTimestamps = 4; + map mapOfBytes = 5; + map mapOfStringValues = 6; + map longLookup = 7; +} + +message SimpleWithSnakeCaseMap { + map entities_by_id = 1; +} + +message SimpleWithMapOfEnums { + map enums_by_id = 1; +} + +service PingService { + rpc ping(PingRequest) returns (PingResponse); +} + +message PingRequest { + string input = 1; +} + +message PingResponse { + string output = 1; +} + +message Numbers { + double double = 1; + float float = 2; + int32 int32 = 3; + int64 int64 = 4; + uint32 uint32 = 5; + uint64 uint64 = 6; + sint32 sint32 = 7; + sint64 sint64 = 8; + fixed32 fixed32 = 9; + fixed64 fixed64 = 10; + sfixed32 sfixed32 = 11; + sfixed64 sfixed64 = 12; +} + +/** For testing proto3's field presence feature. */ +message SimpleButOptional { + // Name field + optional string name = 1; + /* Age */ + optional int32 age = 2; + optional google.protobuf.Timestamp created_at = 9; // This comment will also attach + optional Child child = 3; + optional StateEnum state = 4; + // A thing (imported from thing) + optional ImportedThing thing = 10; + optional google.type.Date birthday = 12; +} + +message Empty {} diff --git a/integration/simple-prototype-defaults/simple.ts b/integration/simple-prototype-defaults/simple.ts new file mode 100644 index 000000000..f0e68fb5a --- /dev/null +++ b/integration/simple-prototype-defaults/simple.ts @@ -0,0 +1,2437 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; +import { Timestamp } from './google/protobuf/timestamp'; +import { ImportedThing } from './import_dir/thing'; +import { DateMessage } from './google/type/date'; +import { StringValue, Int32Value, BoolValue, BytesValue } from './google/protobuf/wrappers'; + +export const protobufPackage = 'simple'; + +/** + * Adding a comment to the syntax will become the first + * comment in the output source file. + */ + +export enum StateEnum { + UNKNOWN = 0, + ON = 2, + OFF = 3, + UNRECOGNIZED = -1, +} + +export function stateEnumFromJSON(object: any): StateEnum { + switch (object) { + case 0: + case 'UNKNOWN': + return StateEnum.UNKNOWN; + case 2: + case 'ON': + return StateEnum.ON; + case 3: + case 'OFF': + return StateEnum.OFF; + case -1: + case 'UNRECOGNIZED': + default: + return StateEnum.UNRECOGNIZED; + } +} + +export function stateEnumToJSON(object: StateEnum): string { + switch (object) { + case StateEnum.UNKNOWN: + return 'UNKNOWN'; + case StateEnum.ON: + return 'ON'; + case StateEnum.OFF: + return 'OFF'; + default: + return 'UNKNOWN'; + } +} + +/** Example comment on the Simple message */ +export interface Simple { + /** Name field */ + name: string; + /** Age */ + age: number; + /** This comment will also attach */ + createdAt: Date | undefined; + child: Child | undefined; + state: StateEnum; + grandChildren: Child[]; + coins: number[]; + snacks: string[]; + oldStates: StateEnum[]; + /** A thing (imported from thing) */ + thing: ImportedThing | undefined; + blobs: Uint8Array[]; + birthday: DateMessage | undefined; + blob: Uint8Array; +} + +export interface Child { + name: string; + type: Child_Type; +} + +export enum Child_Type { + UNKNOWN = 0, + GOOD = 1, + BAD = 2, + UNRECOGNIZED = -1, +} + +export function child_TypeFromJSON(object: any): Child_Type { + switch (object) { + case 0: + case 'UNKNOWN': + return Child_Type.UNKNOWN; + case 1: + case 'GOOD': + return Child_Type.GOOD; + case 2: + case 'BAD': + return Child_Type.BAD; + case -1: + case 'UNRECOGNIZED': + default: + return Child_Type.UNRECOGNIZED; + } +} + +export function child_TypeToJSON(object: Child_Type): string { + switch (object) { + case Child_Type.UNKNOWN: + return 'UNKNOWN'; + case Child_Type.GOOD: + return 'GOOD'; + case Child_Type.BAD: + return 'BAD'; + default: + return 'UNKNOWN'; + } +} + +export interface Nested { + name: string; + message: Nested_InnerMessage | undefined; + state: Nested_InnerEnum; +} + +export enum Nested_InnerEnum { + UNKNOWN_INNER = 0, + GOOD = 100, + BAD = 1000, + UNRECOGNIZED = -1, +} + +export function nested_InnerEnumFromJSON(object: any): Nested_InnerEnum { + switch (object) { + case 0: + case 'UNKNOWN_INNER': + return Nested_InnerEnum.UNKNOWN_INNER; + case 100: + case 'GOOD': + return Nested_InnerEnum.GOOD; + case 1000: + case 'BAD': + return Nested_InnerEnum.BAD; + case -1: + case 'UNRECOGNIZED': + default: + return Nested_InnerEnum.UNRECOGNIZED; + } +} + +export function nested_InnerEnumToJSON(object: Nested_InnerEnum): string { + switch (object) { + case Nested_InnerEnum.UNKNOWN_INNER: + return 'UNKNOWN_INNER'; + case Nested_InnerEnum.GOOD: + return 'GOOD'; + case Nested_InnerEnum.BAD: + return 'BAD'; + default: + return 'UNKNOWN'; + } +} + +/** Comment for a nested message * / */ +export interface Nested_InnerMessage { + name: string; + deep: Nested_InnerMessage_DeepMessage | undefined; +} + +export interface Nested_InnerMessage_DeepMessage { + name: string; +} + +export interface OneOfMessage { + first: string | undefined; + last: string | undefined; +} + +export interface SimpleWithWrappers { + name: string | undefined; + age: number | undefined; + enabled: boolean | undefined; + coins: number[]; + snacks: string[]; + id: Uint8Array | undefined; +} + +export interface Entity { + id: number; +} + +export interface SimpleWithMap { + entitiesById: { [key: number]: Entity }; + nameLookup: { [key: string]: string }; + intLookup: { [key: number]: number }; + mapOfTimestamps: { [key: string]: Date }; + mapOfBytes: { [key: string]: Uint8Array }; + mapOfStringValues: { [key: string]: string | undefined }; + longLookup: { [key: number]: number }; +} + +export interface SimpleWithMap_EntitiesByIdEntry { + key: number; + value: Entity | undefined; +} + +export interface SimpleWithMap_NameLookupEntry { + key: string; + value: string; +} + +export interface SimpleWithMap_IntLookupEntry { + key: number; + value: number; +} + +export interface SimpleWithMap_MapOfTimestampsEntry { + key: string; + value: Date | undefined; +} + +export interface SimpleWithMap_MapOfBytesEntry { + key: string; + value: Uint8Array; +} + +export interface SimpleWithMap_MapOfStringValuesEntry { + key: string; + value: string | undefined; +} + +export interface SimpleWithMap_LongLookupEntry { + key: number; + value: number; +} + +export interface SimpleWithSnakeCaseMap { + entitiesById: { [key: number]: Entity }; +} + +export interface SimpleWithSnakeCaseMap_EntitiesByIdEntry { + key: number; + value: Entity | undefined; +} + +export interface SimpleWithMapOfEnums { + enumsById: { [key: number]: StateEnum }; +} + +export interface SimpleWithMapOfEnums_EnumsByIdEntry { + key: number; + value: StateEnum; +} + +export interface PingRequest { + input: string; +} + +export interface PingResponse { + output: string; +} + +export interface Numbers { + double: number; + float: number; + int32: number; + int64: number; + uint32: number; + uint64: number; + sint32: number; + sint64: number; + fixed32: number; + fixed64: number; + sfixed32: number; + sfixed64: number; +} + +/** For testing proto3's field presence feature. */ +export interface SimpleButOptional { + /** Name field */ + name?: string | undefined; + /** Age */ + age?: number | undefined; + /** This comment will also attach */ + createdAt?: Date | undefined; + child?: Child | undefined; + state?: StateEnum | undefined; + /** A thing (imported from thing) */ + thing?: ImportedThing | undefined; + birthday?: DateMessage | undefined; +} + +export interface Empty {} + +function createBaseSimple(): Simple { + return { + name: '', + age: 0, + createdAt: undefined, + child: undefined, + state: 0, + grandChildren: [], + coins: [], + snacks: [], + oldStates: [], + thing: undefined, + blobs: [], + birthday: undefined, + blob: new Uint8Array(), + }; +} + +export const Simple = { + encode(message: Simple, writer: Writer = Writer.create()): Writer { + if (message.name !== '') { + writer.uint32(10).string(message.name); + } + if (message.age !== 0) { + writer.uint32(16).int32(message.age); + } + if (message.createdAt !== undefined) { + Timestamp.encode(toTimestamp(message.createdAt), writer.uint32(74).fork()).ldelim(); + } + if (message.child !== undefined) { + Child.encode(message.child, writer.uint32(26).fork()).ldelim(); + } + if (message.state !== 0) { + writer.uint32(32).int32(message.state); + } + for (const v of message.grandChildren) { + Child.encode(v!, writer.uint32(42).fork()).ldelim(); + } + writer.uint32(50).fork(); + for (const v of message.coins) { + writer.int32(v); + } + writer.ldelim(); + for (const v of message.snacks) { + writer.uint32(58).string(v!); + } + writer.uint32(66).fork(); + for (const v of message.oldStates) { + writer.int32(v); + } + writer.ldelim(); + if (message.thing !== undefined) { + ImportedThing.encode(message.thing, writer.uint32(82).fork()).ldelim(); + } + for (const v of message.blobs) { + writer.uint32(90).bytes(v!); + } + if (message.birthday !== undefined) { + DateMessage.encode(message.birthday, writer.uint32(98).fork()).ldelim(); + } + if (message.blob.length !== 0) { + writer.uint32(106).bytes(message.blob); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Simple { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimple()) as Simple; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.age = reader.int32(); + break; + case 9: + message.createdAt = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 3: + message.child = Child.decode(reader, reader.uint32()); + break; + case 4: + message.state = reader.int32() as any; + break; + case 5: + message.grandChildren.push(Child.decode(reader, reader.uint32())); + break; + case 6: + if ((tag & 7) === 2) { + const end2 = reader.uint32() + reader.pos; + while (reader.pos < end2) { + message.coins.push(reader.int32()); + } + } else { + message.coins.push(reader.int32()); + } + break; + case 7: + message.snacks.push(reader.string()); + break; + case 8: + if ((tag & 7) === 2) { + const end2 = reader.uint32() + reader.pos; + while (reader.pos < end2) { + message.oldStates.push(reader.int32() as any); + } + } else { + message.oldStates.push(reader.int32() as any); + } + break; + case 10: + message.thing = ImportedThing.decode(reader, reader.uint32()); + break; + case 11: + message.blobs.push(reader.bytes()); + break; + case 12: + message.birthday = DateMessage.decode(reader, reader.uint32()); + break; + case 13: + message.blob = reader.bytes(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Simple { + return { + name: isSet(object.name) ? String(object.name) : '', + age: isSet(object.age) ? Number(object.age) : 0, + createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, + child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, + state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, + grandChildren: Array.isArray(object?.grandChildren) + ? object.grandChildren.map((e: any) => Child.fromJSON(e)) + : [], + coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], + thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, + blobs: Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], + birthday: isSet(object.birthday) ? DateMessage.fromJSON(object.birthday) : undefined, + blob: isSet(object.blob) ? bytesFromBase64(object.blob) : new Uint8Array(), + }; + }, + + toJSON(message: Simple): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.age !== undefined && (obj.age = Math.round(message.age)); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); + message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); + message.state !== undefined && (obj.state = stateEnumToJSON(message.state)); + if (message.grandChildren) { + obj.grandChildren = message.grandChildren.map((e) => (e ? Child.toJSON(e) : undefined)); + } else { + obj.grandChildren = []; + } + if (message.coins) { + obj.coins = message.coins.map((e) => Math.round(e)); + } else { + obj.coins = []; + } + if (message.snacks) { + obj.snacks = message.snacks.map((e) => e); + } else { + obj.snacks = []; + } + if (message.oldStates) { + obj.oldStates = message.oldStates.map((e) => stateEnumToJSON(e)); + } else { + obj.oldStates = []; + } + message.thing !== undefined && (obj.thing = message.thing ? ImportedThing.toJSON(message.thing) : undefined); + if (message.blobs) { + obj.blobs = message.blobs.map((e) => base64FromBytes(e !== undefined ? e : new Uint8Array())); + } else { + obj.blobs = []; + } + message.birthday !== undefined && + (obj.birthday = message.birthday ? DateMessage.toJSON(message.birthday) : undefined); + message.blob !== undefined && + (obj.blob = base64FromBytes(message.blob !== undefined ? message.blob : new Uint8Array())); + return obj; + }, + + fromPartial, I>>(object: I): Simple { + const message = Object.create(createBaseSimple()) as Simple; + message.name = object.name ?? ''; + message.age = object.age ?? 0; + message.createdAt = object.createdAt ?? undefined; + message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; + message.state = object.state ?? 0; + message.grandChildren = object.grandChildren?.map((e) => Child.fromPartial(e)) || []; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.oldStates = object.oldStates?.map((e) => e) || []; + message.thing = + object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; + message.blobs = object.blobs?.map((e) => e) || []; + message.birthday = + object.birthday !== undefined && object.birthday !== null ? DateMessage.fromPartial(object.birthday) : undefined; + message.blob = object.blob ?? new Uint8Array(); + return message; + }, +}; + +function createBaseChild(): Child { + return { name: '', type: 0 }; +} + +export const Child = { + encode(message: Child, writer: Writer = Writer.create()): Writer { + if (message.name !== '') { + writer.uint32(10).string(message.name); + } + if (message.type !== 0) { + writer.uint32(16).int32(message.type); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Child { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseChild()) as Child; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.type = reader.int32() as any; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Child { + return { + name: isSet(object.name) ? String(object.name) : '', + type: isSet(object.type) ? child_TypeFromJSON(object.type) : 0, + }; + }, + + toJSON(message: Child): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.type !== undefined && (obj.type = child_TypeToJSON(message.type)); + return obj; + }, + + fromPartial, I>>(object: I): Child { + const message = Object.create(createBaseChild()) as Child; + message.name = object.name ?? ''; + message.type = object.type ?? 0; + return message; + }, +}; + +function createBaseNested(): Nested { + return { name: '', message: undefined, state: 0 }; +} + +export const Nested = { + encode(message: Nested, writer: Writer = Writer.create()): Writer { + if (message.name !== '') { + writer.uint32(10).string(message.name); + } + if (message.message !== undefined) { + Nested_InnerMessage.encode(message.message, writer.uint32(18).fork()).ldelim(); + } + if (message.state !== 0) { + writer.uint32(24).int32(message.state); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Nested { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseNested()) as Nested; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.message = Nested_InnerMessage.decode(reader, reader.uint32()); + break; + case 3: + message.state = reader.int32() as any; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Nested { + return { + name: isSet(object.name) ? String(object.name) : '', + message: isSet(object.message) ? Nested_InnerMessage.fromJSON(object.message) : undefined, + state: isSet(object.state) ? nested_InnerEnumFromJSON(object.state) : 0, + }; + }, + + toJSON(message: Nested): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.message !== undefined && + (obj.message = message.message ? Nested_InnerMessage.toJSON(message.message) : undefined); + message.state !== undefined && (obj.state = nested_InnerEnumToJSON(message.state)); + return obj; + }, + + fromPartial, I>>(object: I): Nested { + const message = Object.create(createBaseNested()) as Nested; + message.name = object.name ?? ''; + message.message = + object.message !== undefined && object.message !== null + ? Nested_InnerMessage.fromPartial(object.message) + : undefined; + message.state = object.state ?? 0; + return message; + }, +}; + +function createBaseNested_InnerMessage(): Nested_InnerMessage { + return { name: '', deep: undefined }; +} + +export const Nested_InnerMessage = { + encode(message: Nested_InnerMessage, writer: Writer = Writer.create()): Writer { + if (message.name !== '') { + writer.uint32(10).string(message.name); + } + if (message.deep !== undefined) { + Nested_InnerMessage_DeepMessage.encode(message.deep, writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Nested_InnerMessage { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseNested_InnerMessage()) as Nested_InnerMessage; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.deep = Nested_InnerMessage_DeepMessage.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Nested_InnerMessage { + return { + name: isSet(object.name) ? String(object.name) : '', + deep: isSet(object.deep) ? Nested_InnerMessage_DeepMessage.fromJSON(object.deep) : undefined, + }; + }, + + toJSON(message: Nested_InnerMessage): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.deep !== undefined && + (obj.deep = message.deep ? Nested_InnerMessage_DeepMessage.toJSON(message.deep) : undefined); + return obj; + }, + + fromPartial, I>>(object: I): Nested_InnerMessage { + const message = Object.create(createBaseNested_InnerMessage()) as Nested_InnerMessage; + message.name = object.name ?? ''; + message.deep = + object.deep !== undefined && object.deep !== null + ? Nested_InnerMessage_DeepMessage.fromPartial(object.deep) + : undefined; + return message; + }, +}; + +function createBaseNested_InnerMessage_DeepMessage(): Nested_InnerMessage_DeepMessage { + return { name: '' }; +} + +export const Nested_InnerMessage_DeepMessage = { + encode(message: Nested_InnerMessage_DeepMessage, writer: Writer = Writer.create()): Writer { + if (message.name !== '') { + writer.uint32(10).string(message.name); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Nested_InnerMessage_DeepMessage { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseNested_InnerMessage_DeepMessage()) as Nested_InnerMessage_DeepMessage; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Nested_InnerMessage_DeepMessage { + return { + name: isSet(object.name) ? String(object.name) : '', + }; + }, + + toJSON(message: Nested_InnerMessage_DeepMessage): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + return obj; + }, + + fromPartial, I>>( + object: I + ): Nested_InnerMessage_DeepMessage { + const message = Object.create(createBaseNested_InnerMessage_DeepMessage()) as Nested_InnerMessage_DeepMessage; + message.name = object.name ?? ''; + return message; + }, +}; + +function createBaseOneOfMessage(): OneOfMessage { + return { first: undefined, last: undefined }; +} + +export const OneOfMessage = { + encode(message: OneOfMessage, writer: Writer = Writer.create()): Writer { + if (message.first !== undefined) { + writer.uint32(10).string(message.first); + } + if (message.last !== undefined) { + writer.uint32(18).string(message.last); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): OneOfMessage { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseOneOfMessage()) as OneOfMessage; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.first = reader.string(); + break; + case 2: + message.last = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): OneOfMessage { + return { + first: isSet(object.first) ? String(object.first) : undefined, + last: isSet(object.last) ? String(object.last) : undefined, + }; + }, + + toJSON(message: OneOfMessage): unknown { + const obj: any = {}; + message.first !== undefined && (obj.first = message.first); + message.last !== undefined && (obj.last = message.last); + return obj; + }, + + fromPartial, I>>(object: I): OneOfMessage { + const message = Object.create(createBaseOneOfMessage()) as OneOfMessage; + message.first = object.first ?? undefined; + message.last = object.last ?? undefined; + return message; + }, +}; + +function createBaseSimpleWithWrappers(): SimpleWithWrappers { + return { name: undefined, age: undefined, enabled: undefined, coins: [], snacks: [], id: undefined }; +} + +export const SimpleWithWrappers = { + encode(message: SimpleWithWrappers, writer: Writer = Writer.create()): Writer { + if (message.name !== undefined) { + StringValue.encode({ value: message.name! }, writer.uint32(10).fork()).ldelim(); + } + if (message.age !== undefined) { + Int32Value.encode({ value: message.age! }, writer.uint32(18).fork()).ldelim(); + } + if (message.enabled !== undefined) { + BoolValue.encode({ value: message.enabled! }, writer.uint32(26).fork()).ldelim(); + } + for (const v of message.coins) { + Int32Value.encode({ value: v!! }, writer.uint32(50).fork()).ldelim(); + } + for (const v of message.snacks) { + StringValue.encode({ value: v!! }, writer.uint32(58).fork()).ldelim(); + } + if (message.id !== undefined) { + BytesValue.encode({ value: message.id! }, writer.uint32(66).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithWrappers { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithWrappers()) as SimpleWithWrappers; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = StringValue.decode(reader, reader.uint32()).value; + break; + case 2: + message.age = Int32Value.decode(reader, reader.uint32()).value; + break; + case 3: + message.enabled = BoolValue.decode(reader, reader.uint32()).value; + break; + case 6: + message.coins.push(Int32Value.decode(reader, reader.uint32()).value); + break; + case 7: + message.snacks.push(StringValue.decode(reader, reader.uint32()).value); + break; + case 8: + message.id = BytesValue.decode(reader, reader.uint32()).value; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithWrappers { + return { + name: isSet(object.name) ? String(object.name) : undefined, + age: isSet(object.age) ? Number(object.age) : undefined, + enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, + coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + id: isSet(object.id) ? new Uint8Array(object.id) : undefined, + }; + }, + + toJSON(message: SimpleWithWrappers): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.age !== undefined && (obj.age = message.age); + message.enabled !== undefined && (obj.enabled = message.enabled); + if (message.coins) { + obj.coins = message.coins.map((e) => e); + } else { + obj.coins = []; + } + if (message.snacks) { + obj.snacks = message.snacks.map((e) => e); + } else { + obj.snacks = []; + } + message.id !== undefined && (obj.id = message.id); + return obj; + }, + + fromPartial, I>>(object: I): SimpleWithWrappers { + const message = Object.create(createBaseSimpleWithWrappers()) as SimpleWithWrappers; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.id = object.id ?? undefined; + return message; + }, +}; + +function createBaseEntity(): Entity { + return { id: 0 }; +} + +export const Entity = { + encode(message: Entity, writer: Writer = Writer.create()): Writer { + if (message.id !== 0) { + writer.uint32(8).int32(message.id); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Entity { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseEntity()) as Entity; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.id = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Entity { + return { + id: isSet(object.id) ? Number(object.id) : 0, + }; + }, + + toJSON(message: Entity): unknown { + const obj: any = {}; + message.id !== undefined && (obj.id = Math.round(message.id)); + return obj; + }, + + fromPartial, I>>(object: I): Entity { + const message = Object.create(createBaseEntity()) as Entity; + message.id = object.id ?? 0; + return message; + }, +}; + +function createBaseSimpleWithMap(): SimpleWithMap { + return { + entitiesById: {}, + nameLookup: {}, + intLookup: {}, + mapOfTimestamps: {}, + mapOfBytes: {}, + mapOfStringValues: {}, + longLookup: {}, + }; +} + +export const SimpleWithMap = { + encode(message: SimpleWithMap, writer: Writer = Writer.create()): Writer { + Object.entries(message.entitiesById).forEach(([key, value]) => { + SimpleWithMap_EntitiesByIdEntry.encode({ key: key as any, value }, writer.uint32(10).fork()).ldelim(); + }); + Object.entries(message.nameLookup).forEach(([key, value]) => { + SimpleWithMap_NameLookupEntry.encode({ key: key as any, value }, writer.uint32(18).fork()).ldelim(); + }); + Object.entries(message.intLookup).forEach(([key, value]) => { + SimpleWithMap_IntLookupEntry.encode({ key: key as any, value }, writer.uint32(26).fork()).ldelim(); + }); + Object.entries(message.mapOfTimestamps).forEach(([key, value]) => { + SimpleWithMap_MapOfTimestampsEntry.encode({ key: key as any, value }, writer.uint32(34).fork()).ldelim(); + }); + Object.entries(message.mapOfBytes).forEach(([key, value]) => { + SimpleWithMap_MapOfBytesEntry.encode({ key: key as any, value }, writer.uint32(42).fork()).ldelim(); + }); + Object.entries(message.mapOfStringValues).forEach(([key, value]) => { + if (value !== undefined) { + SimpleWithMap_MapOfStringValuesEntry.encode({ key: key as any, value }, writer.uint32(50).fork()).ldelim(); + } + }); + Object.entries(message.longLookup).forEach(([key, value]) => { + SimpleWithMap_LongLookupEntry.encode({ key: key as any, value }, writer.uint32(58).fork()).ldelim(); + }); + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap()) as SimpleWithMap; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + const entry1 = SimpleWithMap_EntitiesByIdEntry.decode(reader, reader.uint32()); + if (entry1.value !== undefined) { + message.entitiesById[entry1.key] = entry1.value; + } + break; + case 2: + const entry2 = SimpleWithMap_NameLookupEntry.decode(reader, reader.uint32()); + if (entry2.value !== undefined) { + message.nameLookup[entry2.key] = entry2.value; + } + break; + case 3: + const entry3 = SimpleWithMap_IntLookupEntry.decode(reader, reader.uint32()); + if (entry3.value !== undefined) { + message.intLookup[entry3.key] = entry3.value; + } + break; + case 4: + const entry4 = SimpleWithMap_MapOfTimestampsEntry.decode(reader, reader.uint32()); + if (entry4.value !== undefined) { + message.mapOfTimestamps[entry4.key] = entry4.value; + } + break; + case 5: + const entry5 = SimpleWithMap_MapOfBytesEntry.decode(reader, reader.uint32()); + if (entry5.value !== undefined) { + message.mapOfBytes[entry5.key] = entry5.value; + } + break; + case 6: + const entry6 = SimpleWithMap_MapOfStringValuesEntry.decode(reader, reader.uint32()); + if (entry6.value !== undefined) { + message.mapOfStringValues[entry6.key] = entry6.value; + } + break; + case 7: + const entry7 = SimpleWithMap_LongLookupEntry.decode(reader, reader.uint32()); + if (entry7.value !== undefined) { + message.longLookup[entry7.key] = entry7.value; + } + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap { + return { + entitiesById: isObject(object.entitiesById) + ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, {}) + : {}, + nameLookup: isObject(object.nameLookup) + ? Object.entries(object.nameLookup).reduce<{ [key: string]: string }>((acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, {}) + : {}, + intLookup: isObject(object.intLookup) + ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, {}) + : {}, + mapOfTimestamps: isObject(object.mapOfTimestamps) + ? Object.entries(object.mapOfTimestamps).reduce<{ [key: string]: Date }>((acc, [key, value]) => { + acc[key] = fromJsonTimestamp(value); + return acc; + }, {}) + : {}, + mapOfBytes: isObject(object.mapOfBytes) + ? Object.entries(object.mapOfBytes).reduce<{ [key: string]: Uint8Array }>((acc, [key, value]) => { + acc[key] = bytesFromBase64(value as string); + return acc; + }, {}) + : {}, + mapOfStringValues: isObject(object.mapOfStringValues) + ? Object.entries(object.mapOfStringValues).reduce<{ [key: string]: string | undefined }>( + (acc, [key, value]) => { + acc[key] = value as string | undefined; + return acc; + }, + {} + ) + : {}, + longLookup: isObject(object.longLookup) + ? Object.entries(object.longLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, {}) + : {}, + }; + }, + + toJSON(message: SimpleWithMap): unknown { + const obj: any = {}; + obj.entitiesById = {}; + if (message.entitiesById) { + Object.entries(message.entitiesById).forEach(([k, v]) => { + obj.entitiesById[k] = Entity.toJSON(v); + }); + } + obj.nameLookup = {}; + if (message.nameLookup) { + Object.entries(message.nameLookup).forEach(([k, v]) => { + obj.nameLookup[k] = v; + }); + } + obj.intLookup = {}; + if (message.intLookup) { + Object.entries(message.intLookup).forEach(([k, v]) => { + obj.intLookup[k] = Math.round(v); + }); + } + obj.mapOfTimestamps = {}; + if (message.mapOfTimestamps) { + Object.entries(message.mapOfTimestamps).forEach(([k, v]) => { + obj.mapOfTimestamps[k] = v.toISOString(); + }); + } + obj.mapOfBytes = {}; + if (message.mapOfBytes) { + Object.entries(message.mapOfBytes).forEach(([k, v]) => { + obj.mapOfBytes[k] = base64FromBytes(v); + }); + } + obj.mapOfStringValues = {}; + if (message.mapOfStringValues) { + Object.entries(message.mapOfStringValues).forEach(([k, v]) => { + obj.mapOfStringValues[k] = v; + }); + } + obj.longLookup = {}; + if (message.longLookup) { + Object.entries(message.longLookup).forEach(([k, v]) => { + obj.longLookup[k] = Math.round(v); + }); + } + return obj; + }, + + fromPartial, I>>(object: I): SimpleWithMap { + const message = Object.create(createBaseSimpleWithMap()) as SimpleWithMap; + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[Number(key)] = Entity.fromPartial(value); + } + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[key] = String(value); + } + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[Number(key)] = Number(value); + } + return acc; + }, + {} + ); + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[key] = value; + } + return acc; + }, + {} + ); + message.mapOfBytes = Object.entries(object.mapOfBytes ?? {}).reduce<{ [key: string]: Uint8Array }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[key] = value; + } + return acc; + }, + {} + ); + message.mapOfStringValues = Object.entries(object.mapOfStringValues ?? {}).reduce<{ + [key: string]: string | undefined; + }>((acc, [key, value]) => { + if (value !== undefined) { + acc[key] = value; + } + return acc; + }, {}); + message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[Number(key)] = Number(value); + } + return acc; + }, + {} + ); + return message; + }, +}; + +function createBaseSimpleWithMap_EntitiesByIdEntry(): SimpleWithMap_EntitiesByIdEntry { + return { key: 0, value: undefined }; +} + +export const SimpleWithMap_EntitiesByIdEntry = { + encode(message: SimpleWithMap_EntitiesByIdEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== 0) { + writer.uint32(8).int32(message.key); + } + if (message.value !== undefined) { + Entity.encode(message.value, writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_EntitiesByIdEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_EntitiesByIdEntry()) as SimpleWithMap_EntitiesByIdEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.int32(); + break; + case 2: + message.value = Entity.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_EntitiesByIdEntry { + return { + key: isSet(object.key) ? Number(object.key) : 0, + value: isSet(object.value) ? Entity.fromJSON(object.value) : undefined, + }; + }, + + toJSON(message: SimpleWithMap_EntitiesByIdEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = Math.round(message.key)); + message.value !== undefined && (obj.value = message.value ? Entity.toJSON(message.value) : undefined); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_EntitiesByIdEntry { + const message = Object.create(createBaseSimpleWithMap_EntitiesByIdEntry()) as SimpleWithMap_EntitiesByIdEntry; + message.key = object.key ?? 0; + message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; + return message; + }, +}; + +function createBaseSimpleWithMap_NameLookupEntry(): SimpleWithMap_NameLookupEntry { + return { key: '', value: '' }; +} + +export const SimpleWithMap_NameLookupEntry = { + encode(message: SimpleWithMap_NameLookupEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value !== '') { + writer.uint32(18).string(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_NameLookupEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_NameLookupEntry()) as SimpleWithMap_NameLookupEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_NameLookupEntry { + return { + key: isSet(object.key) ? String(object.key) : '', + value: isSet(object.value) ? String(object.value) : '', + }; + }, + + toJSON(message: SimpleWithMap_NameLookupEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { + const message = Object.create(createBaseSimpleWithMap_NameLookupEntry()) as SimpleWithMap_NameLookupEntry; + message.key = object.key ?? ''; + message.value = object.value ?? ''; + return message; + }, +}; + +function createBaseSimpleWithMap_IntLookupEntry(): SimpleWithMap_IntLookupEntry { + return { key: 0, value: 0 }; +} + +export const SimpleWithMap_IntLookupEntry = { + encode(message: SimpleWithMap_IntLookupEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== 0) { + writer.uint32(8).int32(message.key); + } + if (message.value !== 0) { + writer.uint32(16).int32(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_IntLookupEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_IntLookupEntry()) as SimpleWithMap_IntLookupEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.int32(); + break; + case 2: + message.value = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_IntLookupEntry { + return { + key: isSet(object.key) ? Number(object.key) : 0, + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: SimpleWithMap_IntLookupEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = Math.round(message.key)); + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { + const message = Object.create(createBaseSimpleWithMap_IntLookupEntry()) as SimpleWithMap_IntLookupEntry; + message.key = object.key ?? 0; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseSimpleWithMap_MapOfTimestampsEntry(): SimpleWithMap_MapOfTimestampsEntry { + return { key: '', value: undefined }; +} + +export const SimpleWithMap_MapOfTimestampsEntry = { + encode(message: SimpleWithMap_MapOfTimestampsEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value !== undefined) { + Timestamp.encode(toTimestamp(message.value), writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_MapOfTimestampsEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_MapOfTimestampsEntry()) as SimpleWithMap_MapOfTimestampsEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_MapOfTimestampsEntry { + return { + key: isSet(object.key) ? String(object.key) : '', + value: isSet(object.value) ? fromJsonTimestamp(object.value) : undefined, + }; + }, + + toJSON(message: SimpleWithMap_MapOfTimestampsEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && (obj.value = message.value.toISOString()); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfTimestampsEntry { + const message = Object.create(createBaseSimpleWithMap_MapOfTimestampsEntry()) as SimpleWithMap_MapOfTimestampsEntry; + message.key = object.key ?? ''; + message.value = object.value ?? undefined; + return message; + }, +}; + +function createBaseSimpleWithMap_MapOfBytesEntry(): SimpleWithMap_MapOfBytesEntry { + return { key: '', value: new Uint8Array() }; +} + +export const SimpleWithMap_MapOfBytesEntry = { + encode(message: SimpleWithMap_MapOfBytesEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value.length !== 0) { + writer.uint32(18).bytes(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_MapOfBytesEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_MapOfBytesEntry()) as SimpleWithMap_MapOfBytesEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = reader.bytes(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_MapOfBytesEntry { + return { + key: isSet(object.key) ? String(object.key) : '', + value: isSet(object.value) ? bytesFromBase64(object.value) : new Uint8Array(), + }; + }, + + toJSON(message: SimpleWithMap_MapOfBytesEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && + (obj.value = base64FromBytes(message.value !== undefined ? message.value : new Uint8Array())); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfBytesEntry { + const message = Object.create(createBaseSimpleWithMap_MapOfBytesEntry()) as SimpleWithMap_MapOfBytesEntry; + message.key = object.key ?? ''; + message.value = object.value ?? new Uint8Array(); + return message; + }, +}; + +function createBaseSimpleWithMap_MapOfStringValuesEntry(): SimpleWithMap_MapOfStringValuesEntry { + return { key: '', value: undefined }; +} + +export const SimpleWithMap_MapOfStringValuesEntry = { + encode(message: SimpleWithMap_MapOfStringValuesEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value !== undefined) { + StringValue.encode({ value: message.value! }, writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_MapOfStringValuesEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create( + createBaseSimpleWithMap_MapOfStringValuesEntry() + ) as SimpleWithMap_MapOfStringValuesEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = StringValue.decode(reader, reader.uint32()).value; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_MapOfStringValuesEntry { + return { + key: isSet(object.key) ? String(object.key) : '', + value: isSet(object.value) ? String(object.value) : undefined, + }; + }, + + toJSON(message: SimpleWithMap_MapOfStringValuesEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfStringValuesEntry { + const message = Object.create( + createBaseSimpleWithMap_MapOfStringValuesEntry() + ) as SimpleWithMap_MapOfStringValuesEntry; + message.key = object.key ?? ''; + message.value = object.value ?? undefined; + return message; + }, +}; + +function createBaseSimpleWithMap_LongLookupEntry(): SimpleWithMap_LongLookupEntry { + return { key: 0, value: 0 }; +} + +export const SimpleWithMap_LongLookupEntry = { + encode(message: SimpleWithMap_LongLookupEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== 0) { + writer.uint32(8).int64(message.key); + } + if (message.value !== 0) { + writer.uint32(16).int64(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_LongLookupEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMap_LongLookupEntry()) as SimpleWithMap_LongLookupEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = longToNumber(reader.int64() as Long); + break; + case 2: + message.value = longToNumber(reader.int64() as Long); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMap_LongLookupEntry { + return { + key: isSet(object.key) ? Number(object.key) : 0, + value: isSet(object.value) ? Number(object.value) : 0, + }; + }, + + toJSON(message: SimpleWithMap_LongLookupEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = Math.round(message.key)); + message.value !== undefined && (obj.value = Math.round(message.value)); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMap_LongLookupEntry { + const message = Object.create(createBaseSimpleWithMap_LongLookupEntry()) as SimpleWithMap_LongLookupEntry; + message.key = object.key ?? 0; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBaseSimpleWithSnakeCaseMap(): SimpleWithSnakeCaseMap { + return { entitiesById: {} }; +} + +export const SimpleWithSnakeCaseMap = { + encode(message: SimpleWithSnakeCaseMap, writer: Writer = Writer.create()): Writer { + Object.entries(message.entitiesById).forEach(([key, value]) => { + SimpleWithSnakeCaseMap_EntitiesByIdEntry.encode({ key: key as any, value }, writer.uint32(10).fork()).ldelim(); + }); + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithSnakeCaseMap { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithSnakeCaseMap()) as SimpleWithSnakeCaseMap; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + const entry1 = SimpleWithSnakeCaseMap_EntitiesByIdEntry.decode(reader, reader.uint32()); + if (entry1.value !== undefined) { + message.entitiesById[entry1.key] = entry1.value; + } + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithSnakeCaseMap { + return { + entitiesById: isObject(object.entitiesById) + ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, {}) + : {}, + }; + }, + + toJSON(message: SimpleWithSnakeCaseMap): unknown { + const obj: any = {}; + obj.entitiesById = {}; + if (message.entitiesById) { + Object.entries(message.entitiesById).forEach(([k, v]) => { + obj.entitiesById[k] = Entity.toJSON(v); + }); + } + return obj; + }, + + fromPartial, I>>(object: I): SimpleWithSnakeCaseMap { + const message = Object.create(createBaseSimpleWithSnakeCaseMap()) as SimpleWithSnakeCaseMap; + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[Number(key)] = Entity.fromPartial(value); + } + return acc; + }, + {} + ); + return message; + }, +}; + +function createBaseSimpleWithSnakeCaseMap_EntitiesByIdEntry(): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + return { key: 0, value: undefined }; +} + +export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { + encode(message: SimpleWithSnakeCaseMap_EntitiesByIdEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== 0) { + writer.uint32(8).int32(message.key); + } + if (message.value !== undefined) { + Entity.encode(message.value, writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create( + createBaseSimpleWithSnakeCaseMap_EntitiesByIdEntry() + ) as SimpleWithSnakeCaseMap_EntitiesByIdEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.int32(); + break; + case 2: + message.value = Entity.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + return { + key: isSet(object.key) ? Number(object.key) : 0, + value: isSet(object.value) ? Entity.fromJSON(object.value) : undefined, + }; + }, + + toJSON(message: SimpleWithSnakeCaseMap_EntitiesByIdEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = Math.round(message.key)); + message.value !== undefined && (obj.value = message.value ? Entity.toJSON(message.value) : undefined); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + const message = Object.create( + createBaseSimpleWithSnakeCaseMap_EntitiesByIdEntry() + ) as SimpleWithSnakeCaseMap_EntitiesByIdEntry; + message.key = object.key ?? 0; + message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; + return message; + }, +}; + +function createBaseSimpleWithMapOfEnums(): SimpleWithMapOfEnums { + return { enumsById: {} }; +} + +export const SimpleWithMapOfEnums = { + encode(message: SimpleWithMapOfEnums, writer: Writer = Writer.create()): Writer { + Object.entries(message.enumsById).forEach(([key, value]) => { + SimpleWithMapOfEnums_EnumsByIdEntry.encode({ key: key as any, value }, writer.uint32(10).fork()).ldelim(); + }); + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMapOfEnums { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleWithMapOfEnums()) as SimpleWithMapOfEnums; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + const entry1 = SimpleWithMapOfEnums_EnumsByIdEntry.decode(reader, reader.uint32()); + if (entry1.value !== undefined) { + message.enumsById[entry1.key] = entry1.value; + } + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMapOfEnums { + return { + enumsById: isObject(object.enumsById) + ? Object.entries(object.enumsById).reduce<{ [key: number]: StateEnum }>((acc, [key, value]) => { + acc[Number(key)] = value as number; + return acc; + }, {}) + : {}, + }; + }, + + toJSON(message: SimpleWithMapOfEnums): unknown { + const obj: any = {}; + obj.enumsById = {}; + if (message.enumsById) { + Object.entries(message.enumsById).forEach(([k, v]) => { + obj.enumsById[k] = stateEnumToJSON(v); + }); + } + return obj; + }, + + fromPartial, I>>(object: I): SimpleWithMapOfEnums { + const message = Object.create(createBaseSimpleWithMapOfEnums()) as SimpleWithMapOfEnums; + message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[Number(key)] = value as number; + } + return acc; + }, + {} + ); + return message; + }, +}; + +function createBaseSimpleWithMapOfEnums_EnumsByIdEntry(): SimpleWithMapOfEnums_EnumsByIdEntry { + return { key: 0, value: 0 }; +} + +export const SimpleWithMapOfEnums_EnumsByIdEntry = { + encode(message: SimpleWithMapOfEnums_EnumsByIdEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== 0) { + writer.uint32(8).int32(message.key); + } + if (message.value !== 0) { + writer.uint32(16).int32(message.value); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleWithMapOfEnums_EnumsByIdEntry { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create( + createBaseSimpleWithMapOfEnums_EnumsByIdEntry() + ) as SimpleWithMapOfEnums_EnumsByIdEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.int32(); + break; + case 2: + message.value = reader.int32() as any; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleWithMapOfEnums_EnumsByIdEntry { + return { + key: isSet(object.key) ? Number(object.key) : 0, + value: isSet(object.value) ? stateEnumFromJSON(object.value) : 0, + }; + }, + + toJSON(message: SimpleWithMapOfEnums_EnumsByIdEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = Math.round(message.key)); + message.value !== undefined && (obj.value = stateEnumToJSON(message.value)); + return obj; + }, + + fromPartial, I>>( + object: I + ): SimpleWithMapOfEnums_EnumsByIdEntry { + const message = Object.create( + createBaseSimpleWithMapOfEnums_EnumsByIdEntry() + ) as SimpleWithMapOfEnums_EnumsByIdEntry; + message.key = object.key ?? 0; + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePingRequest(): PingRequest { + return { input: '' }; +} + +export const PingRequest = { + encode(message: PingRequest, writer: Writer = Writer.create()): Writer { + if (message.input !== '') { + writer.uint32(10).string(message.input); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): PingRequest { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBasePingRequest()) as PingRequest; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.input = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): PingRequest { + return { + input: isSet(object.input) ? String(object.input) : '', + }; + }, + + toJSON(message: PingRequest): unknown { + const obj: any = {}; + message.input !== undefined && (obj.input = message.input); + return obj; + }, + + fromPartial, I>>(object: I): PingRequest { + const message = Object.create(createBasePingRequest()) as PingRequest; + message.input = object.input ?? ''; + return message; + }, +}; + +function createBasePingResponse(): PingResponse { + return { output: '' }; +} + +export const PingResponse = { + encode(message: PingResponse, writer: Writer = Writer.create()): Writer { + if (message.output !== '') { + writer.uint32(10).string(message.output); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): PingResponse { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBasePingResponse()) as PingResponse; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.output = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): PingResponse { + return { + output: isSet(object.output) ? String(object.output) : '', + }; + }, + + toJSON(message: PingResponse): unknown { + const obj: any = {}; + message.output !== undefined && (obj.output = message.output); + return obj; + }, + + fromPartial, I>>(object: I): PingResponse { + const message = Object.create(createBasePingResponse()) as PingResponse; + message.output = object.output ?? ''; + return message; + }, +}; + +function createBaseNumbers(): Numbers { + return { + double: 0, + float: 0, + int32: 0, + int64: 0, + uint32: 0, + uint64: 0, + sint32: 0, + sint64: 0, + fixed32: 0, + fixed64: 0, + sfixed32: 0, + sfixed64: 0, + }; +} + +export const Numbers = { + encode(message: Numbers, writer: Writer = Writer.create()): Writer { + if (message.double !== 0) { + writer.uint32(9).double(message.double); + } + if (message.float !== 0) { + writer.uint32(21).float(message.float); + } + if (message.int32 !== 0) { + writer.uint32(24).int32(message.int32); + } + if (message.int64 !== 0) { + writer.uint32(32).int64(message.int64); + } + if (message.uint32 !== 0) { + writer.uint32(40).uint32(message.uint32); + } + if (message.uint64 !== 0) { + writer.uint32(48).uint64(message.uint64); + } + if (message.sint32 !== 0) { + writer.uint32(56).sint32(message.sint32); + } + if (message.sint64 !== 0) { + writer.uint32(64).sint64(message.sint64); + } + if (message.fixed32 !== 0) { + writer.uint32(77).fixed32(message.fixed32); + } + if (message.fixed64 !== 0) { + writer.uint32(81).fixed64(message.fixed64); + } + if (message.sfixed32 !== 0) { + writer.uint32(93).sfixed32(message.sfixed32); + } + if (message.sfixed64 !== 0) { + writer.uint32(97).sfixed64(message.sfixed64); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Numbers { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseNumbers()) as Numbers; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.double = reader.double(); + break; + case 2: + message.float = reader.float(); + break; + case 3: + message.int32 = reader.int32(); + break; + case 4: + message.int64 = longToNumber(reader.int64() as Long); + break; + case 5: + message.uint32 = reader.uint32(); + break; + case 6: + message.uint64 = longToNumber(reader.uint64() as Long); + break; + case 7: + message.sint32 = reader.sint32(); + break; + case 8: + message.sint64 = longToNumber(reader.sint64() as Long); + break; + case 9: + message.fixed32 = reader.fixed32(); + break; + case 10: + message.fixed64 = longToNumber(reader.fixed64() as Long); + break; + case 11: + message.sfixed32 = reader.sfixed32(); + break; + case 12: + message.sfixed64 = longToNumber(reader.sfixed64() as Long); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Numbers { + return { + double: isSet(object.double) ? Number(object.double) : 0, + float: isSet(object.float) ? Number(object.float) : 0, + int32: isSet(object.int32) ? Number(object.int32) : 0, + int64: isSet(object.int64) ? Number(object.int64) : 0, + uint32: isSet(object.uint32) ? Number(object.uint32) : 0, + uint64: isSet(object.uint64) ? Number(object.uint64) : 0, + sint32: isSet(object.sint32) ? Number(object.sint32) : 0, + sint64: isSet(object.sint64) ? Number(object.sint64) : 0, + fixed32: isSet(object.fixed32) ? Number(object.fixed32) : 0, + fixed64: isSet(object.fixed64) ? Number(object.fixed64) : 0, + sfixed32: isSet(object.sfixed32) ? Number(object.sfixed32) : 0, + sfixed64: isSet(object.sfixed64) ? Number(object.sfixed64) : 0, + }; + }, + + toJSON(message: Numbers): unknown { + const obj: any = {}; + message.double !== undefined && (obj.double = message.double); + message.float !== undefined && (obj.float = message.float); + message.int32 !== undefined && (obj.int32 = Math.round(message.int32)); + message.int64 !== undefined && (obj.int64 = Math.round(message.int64)); + message.uint32 !== undefined && (obj.uint32 = Math.round(message.uint32)); + message.uint64 !== undefined && (obj.uint64 = Math.round(message.uint64)); + message.sint32 !== undefined && (obj.sint32 = Math.round(message.sint32)); + message.sint64 !== undefined && (obj.sint64 = Math.round(message.sint64)); + message.fixed32 !== undefined && (obj.fixed32 = Math.round(message.fixed32)); + message.fixed64 !== undefined && (obj.fixed64 = Math.round(message.fixed64)); + message.sfixed32 !== undefined && (obj.sfixed32 = Math.round(message.sfixed32)); + message.sfixed64 !== undefined && (obj.sfixed64 = Math.round(message.sfixed64)); + return obj; + }, + + fromPartial, I>>(object: I): Numbers { + const message = Object.create(createBaseNumbers()) as Numbers; + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; + return message; + }, +}; + +function createBaseSimpleButOptional(): SimpleButOptional { + return { + name: undefined, + age: undefined, + createdAt: undefined, + child: undefined, + state: undefined, + thing: undefined, + birthday: undefined, + }; +} + +export const SimpleButOptional = { + encode(message: SimpleButOptional, writer: Writer = Writer.create()): Writer { + if (message.name !== undefined) { + writer.uint32(10).string(message.name); + } + if (message.age !== undefined) { + writer.uint32(16).int32(message.age); + } + if (message.createdAt !== undefined) { + Timestamp.encode(toTimestamp(message.createdAt), writer.uint32(74).fork()).ldelim(); + } + if (message.child !== undefined) { + Child.encode(message.child, writer.uint32(26).fork()).ldelim(); + } + if (message.state !== undefined) { + writer.uint32(32).int32(message.state); + } + if (message.thing !== undefined) { + ImportedThing.encode(message.thing, writer.uint32(82).fork()).ldelim(); + } + if (message.birthday !== undefined) { + DateMessage.encode(message.birthday, writer.uint32(98).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): SimpleButOptional { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseSimpleButOptional()) as SimpleButOptional; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.age = reader.int32(); + break; + case 9: + message.createdAt = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 3: + message.child = Child.decode(reader, reader.uint32()); + break; + case 4: + message.state = reader.int32() as any; + break; + case 10: + message.thing = ImportedThing.decode(reader, reader.uint32()); + break; + case 12: + message.birthday = DateMessage.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): SimpleButOptional { + return { + name: isSet(object.name) ? String(object.name) : undefined, + age: isSet(object.age) ? Number(object.age) : undefined, + createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, + child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, + state: isSet(object.state) ? stateEnumFromJSON(object.state) : undefined, + thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, + birthday: isSet(object.birthday) ? DateMessage.fromJSON(object.birthday) : undefined, + }; + }, + + toJSON(message: SimpleButOptional): unknown { + const obj: any = {}; + message.name !== undefined && (obj.name = message.name); + message.age !== undefined && (obj.age = Math.round(message.age)); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); + message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); + message.state !== undefined && + (obj.state = message.state !== undefined ? stateEnumToJSON(message.state) : undefined); + message.thing !== undefined && (obj.thing = message.thing ? ImportedThing.toJSON(message.thing) : undefined); + message.birthday !== undefined && + (obj.birthday = message.birthday ? DateMessage.toJSON(message.birthday) : undefined); + return obj; + }, + + fromPartial, I>>(object: I): SimpleButOptional { + const message = Object.create(createBaseSimpleButOptional()) as SimpleButOptional; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.createdAt = object.createdAt ?? undefined; + message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; + message.state = object.state ?? undefined; + message.thing = + object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; + message.birthday = + object.birthday !== undefined && object.birthday !== null ? DateMessage.fromPartial(object.birthday) : undefined; + return message; + }, +}; + +function createBaseEmpty(): Empty { + return {}; +} + +export const Empty = { + encode(_: Empty, writer: Writer = Writer.create()): Writer { + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Empty { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseEmpty()) as Empty; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(_: any): Empty { + return {}; + }, + + toJSON(_: Empty): unknown { + const obj: any = {}; + return obj; + }, + + fromPartial, I>>(_: I): Empty { + const message = Object.create(createBaseEmpty()) as Empty; + return message; + }, +}; + +export interface PingService { + ping(request: PingRequest): Promise; +} + +export class PingServiceClientImpl implements PingService { + private readonly rpc: Rpc; + constructor(rpc: Rpc) { + this.rpc = rpc; + this.ping = this.ping.bind(this); + } + ping(request: PingRequest): Promise { + const data = PingRequest.encode(request).finish(); + const promise = this.rpc.request('simple.PingService', 'ping', data); + return promise.then((data) => PingResponse.decode(new Reader(data))); + } +} + +interface Rpc { + request(service: string, method: string, data: Uint8Array): Promise; +} + +declare var self: any | undefined; +declare var window: any | undefined; +declare var global: any | undefined; +var globalThis: any = (() => { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + throw 'Unable to locate global object'; +})(); + +const atob: (b64: string) => string = + globalThis.atob || ((b64) => globalThis.Buffer.from(b64, 'base64').toString('binary')); +function bytesFromBase64(b64: string): Uint8Array { + const bin = atob(b64); + const arr = new Uint8Array(bin.length); + for (let i = 0; i < bin.length; ++i) { + arr[i] = bin.charCodeAt(i); + } + return arr; +} + +const btoa: (bin: string) => string = + globalThis.btoa || ((bin) => globalThis.Buffer.from(bin, 'binary').toString('base64')); +function base64FromBytes(arr: Uint8Array): string { + const bin: string[] = []; + for (const byte of arr) { + bin.push(String.fromCharCode(byte)); + } + return btoa(bin.join('')); +} + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + +function toTimestamp(date: Date): Timestamp { + const seconds = date.getTime() / 1_000; + const nanos = (date.getTime() % 1_000) * 1_000_000; + return { seconds, nanos }; +} + +function fromTimestamp(t: Timestamp): Date { + let millis = t.seconds * 1_000; + millis += t.nanos / 1_000_000; + return new Date(millis); +} + +function fromJsonTimestamp(o: any): Date { + if (o instanceof Date) { + return o; + } else if (typeof o === 'string') { + return new Date(o); + } else { + return fromTimestamp(Timestamp.fromJSON(o)); + } +} + +function longToNumber(long: Long): number { + if (long.gt(Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); + } + return long.toNumber(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} + +function isObject(value: any): boolean { + return typeof value === 'object' && value !== null; +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/tests/options-test.ts b/tests/options-test.ts index 27cc3f65f..9b8a89b5a 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -38,6 +38,7 @@ describe('options', () => { "useExactTypes": true, "useMongoObjectId": false, "useOptionals": "none", + "usePrototypeForDefaults": false, } `); }); From d125183a986bbb30a0151ea136c7a7193bfdee5e Mon Sep 17 00:00:00 2001 From: Vilsol Date: Thu, 20 Jan 2022 15:55:52 +0200 Subject: [PATCH 3/3] chore: more appropriate tests for prototype-defaults --- .../simple-prototype-defaults-test.ts | 24 +++++++++++++++++++ .../simple-prototype-defaults/simple-test.ts | 22 ----------------- 2 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 integration/simple-prototype-defaults/simple-prototype-defaults-test.ts delete mode 100644 integration/simple-prototype-defaults/simple-test.ts diff --git a/integration/simple-prototype-defaults/simple-prototype-defaults-test.ts b/integration/simple-prototype-defaults/simple-prototype-defaults-test.ts new file mode 100644 index 000000000..4c8fa5518 --- /dev/null +++ b/integration/simple-prototype-defaults/simple-prototype-defaults-test.ts @@ -0,0 +1,24 @@ +import { Simple } from './simple'; + +describe('simple', () => { + it('generates types correctly', () => { + const decodedUndefined = Simple.decode(Simple.encode(Simple.fromPartial({})).finish()); + + expect(Object.keys(decodedUndefined).includes('age')).toBeFalsy(); + expect(decodedUndefined.hasOwnProperty('age')).toBeFalsy(); + + const decodedWithZero = Simple.decode(Simple.encode(Simple.fromPartial({ + age: 0 + })).finish()); + + expect(Object.keys(decodedWithZero).includes('age')).toBeFalsy(); + expect(decodedWithZero.hasOwnProperty('age')).toBeFalsy(); + + const decodedWithOne = Simple.decode(Simple.encode(Simple.fromPartial({ + age: 1 + })).finish()); + + expect(Object.keys(decodedWithOne).includes('age')).toBeTruthy(); + expect(decodedWithOne.hasOwnProperty('age')).toBeTruthy(); + }); +}); diff --git a/integration/simple-prototype-defaults/simple-test.ts b/integration/simple-prototype-defaults/simple-test.ts deleted file mode 100644 index f6ddbd35a..000000000 --- a/integration/simple-prototype-defaults/simple-test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Child_Type, Simple, StateEnum } from './simple'; - -describe('simple', () => { - it('generates types correctly', () => { - const simple: Simple = { - birthday: undefined, - blob: Buffer.from([]), - blobs: [], - name: 'asdf', - age: 1, - child: { name: 'child', type: Child_Type.UNKNOWN }, - state: StateEnum.ON, - grandChildren: [{ name: 'grand1', type: Child_Type.UNKNOWN }, { name: 'grand2', type: Child_Type.UNKNOWN }], - coins: [2, 4, 6], - snacks: ['a', 'b'], - oldStates: [StateEnum.ON, StateEnum.OFF], - createdAt: new Date('1970-01-01T00:00:00.000Z'), - thing: undefined - }; - expect(simple.name).toEqual('asdf'); - }); -});