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 000000000..7f34d6ce0 Binary files /dev/null and b/integration/simple-prototype-defaults/google/protobuf/wrappers.bin differ 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 000000000..680ba7e6d Binary files /dev/null and b/integration/simple-prototype-defaults/google/type/date.bin differ diff --git a/integration/simple-prototype-defaults/google/type/date.proto b/integration/simple-prototype-defaults/google/type/date.proto new file mode 100644 index 000000000..b958feeba --- /dev/null +++ b/integration/simple-prototype-defaults/google/type/date.proto @@ -0,0 +1,50 @@ +// Copyright 2019 Google LLC. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +syntax = "proto3"; + +package google.type; + +option cc_enable_arenas = true; +option go_package = "google.golang.org/genproto/googleapis/type/date;date"; +option java_multiple_files = true; +option java_outer_classname = "DateProto"; +option java_package = "com.google.type"; +option objc_class_prefix = "GTP"; + +// Represents a whole or partial calendar date, e.g. a birthday. The time of day +// and time zone are either specified elsewhere or are not significant. The date +// is relative to the Proleptic Gregorian Calendar. This can represent: +// +// * A full date, with non-zero year, month and day values +// * A month and day value, with a zero year, e.g. an anniversary +// * A year on its own, with zero month and day values +// * A year and month value, with a zero day, e.g. a credit card expiration date +// +// Related types are [google.type.TimeOfDay][google.type.TimeOfDay] and `google.protobuf.Timestamp`. +message Date { + // Year of date. Must be from 1 to 9999, or 0 if specifying a date without + // a year. + int32 year = 1; + + // Month of year. Must be from 1 to 12, or 0 if specifying a year without a + // month and day. + int32 month = 2; + + // Day of month. Must be from 1 to 31 and valid for the year and month, or 0 + // if specifying a year by itself or a year and month where the day is not + // significant. + int32 day = 3; +} diff --git a/integration/simple-prototype-defaults/google/type/date.ts b/integration/simple-prototype-defaults/google/type/date.ts new file mode 100644 index 000000000..8183aa6fc --- /dev/null +++ b/integration/simple-prototype-defaults/google/type/date.ts @@ -0,0 +1,131 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; + +export const protobufPackage = 'google.type'; + +/** + * Represents a whole or partial calendar date, e.g. a birthday. The time of day + * and time zone are either specified elsewhere or are not significant. The date + * is relative to the Proleptic Gregorian Calendar. This can represent: + * + * * A full date, with non-zero year, month and day values + * * A month and day value, with a zero year, e.g. an anniversary + * * A year on its own, with zero month and day values + * * A year and month value, with a zero day, e.g. a credit card expiration date + * + * Related types are [google.type.TimeOfDay][google.type.TimeOfDay] and `google.protobuf.Timestamp`. + */ +export interface DateMessage { + /** + * Year of date. Must be from 1 to 9999, or 0 if specifying a date without + * a year. + */ + year: number; + /** + * Month of year. Must be from 1 to 12, or 0 if specifying a year without a + * month and day. + */ + month: number; + /** + * Day of month. Must be from 1 to 31 and valid for the year and month, or 0 + * if specifying a year by itself or a year and month where the day is not + * significant. + */ + day: number; +} + +function createBaseDateMessage(): DateMessage { + return { year: 0, month: 0, day: 0 }; +} + +export const DateMessage = { + encode(message: DateMessage, writer: Writer = Writer.create()): Writer { + if (message.year !== 0) { + writer.uint32(8).int32(message.year); + } + if (message.month !== 0) { + writer.uint32(16).int32(message.month); + } + if (message.day !== 0) { + writer.uint32(24).int32(message.day); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): DateMessage { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseDateMessage()) as DateMessage; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 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 000000000..18377ed61 Binary files /dev/null and b/integration/simple-prototype-defaults/import_dir/thing.bin differ diff --git a/integration/simple-prototype-defaults/import_dir/thing.proto b/integration/simple-prototype-defaults/import_dir/thing.proto new file mode 100644 index 000000000..4742517f8 --- /dev/null +++ b/integration/simple-prototype-defaults/import_dir/thing.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +import "google/protobuf/timestamp.proto"; +package simple; + +message ImportedThing { + google.protobuf.Timestamp created_at = 1; +} \ No newline at end of file diff --git a/integration/simple-prototype-defaults/import_dir/thing.ts b/integration/simple-prototype-defaults/import_dir/thing.ts new file mode 100644 index 000000000..23eb37a06 --- /dev/null +++ b/integration/simple-prototype-defaults/import_dir/thing.ts @@ -0,0 +1,109 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; +import { Timestamp } from '../google/protobuf/timestamp'; + +export const protobufPackage = 'simple'; + +export interface ImportedThing { + createdAt: Date | undefined; +} + +function createBaseImportedThing(): ImportedThing { + return { createdAt: undefined }; +} + +export const ImportedThing = { + encode(message: ImportedThing, writer: Writer = Writer.create()): Writer { + if (message.createdAt !== undefined) { + Timestamp.encode(toTimestamp(message.createdAt), writer.uint32(10).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): ImportedThing { + const reader = input instanceof Reader ? input : new Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = Object.create(createBaseImportedThing()) as ImportedThing; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 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-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.bin b/integration/simple-prototype-defaults/simple.bin new file mode 100644 index 000000000..321bb33c3 Binary files /dev/null and b/integration/simple-prototype-defaults/simple.bin differ diff --git a/integration/simple-prototype-defaults/simple.proto b/integration/simple-prototype-defaults/simple.proto new file mode 100644 index 000000000..98ad06038 --- /dev/null +++ b/integration/simple-prototype-defaults/simple.proto @@ -0,0 +1,152 @@ +// Adding a comment to the syntax will become the first +// comment in the output source file. +syntax = "proto3"; + +import "google/type/date.proto"; +import "google/protobuf/wrappers.proto"; +import "google/protobuf/timestamp.proto"; +import "import_dir/thing.proto"; + +package simple; + +// This comment is separated by a blank non-comment line, and will detach from +// the following comment on the message Simple. + +/** Example comment on the Simple message */ +message Simple { + // Name field + string name = 1; + /* Age */ + int32 age = 2; + google.protobuf.Timestamp created_at = 9; // This comment will also attach + Child child = 3; + StateEnum state = 4; + repeated Child grand_children = 5; + repeated int32 coins = 6; + repeated string snacks = 7; + repeated StateEnum old_states = 8; + // A thing (imported from thing) + ImportedThing thing = 10; + repeated bytes blobs = 11; + google.type.Date birthday = 12; + bytes blob = 13; +} + +message Child { + enum Type { + UNKNOWN = 0; + GOOD = 1; + BAD = 2; + } + string name = 1; + Type type = 2; +} + +enum StateEnum { + UNKNOWN = 0; + ON = 2; + OFF = 3; +} + +message Nested { + string name = 1; + InnerMessage message = 2; + InnerEnum state = 3; + + // Comment for a nested message */ + message InnerMessage { + string name = 1; + DeepMessage deep = 2; + + message DeepMessage { + string name = 1; + } + } + + enum InnerEnum { + UNKNOWN_INNER = 0; + GOOD = 100; + BAD = 1000; + } +} + +message OneOfMessage { + oneof name_fields { + string first = 1; + string last = 2; + } +} + +message SimpleWithWrappers { + google.protobuf.StringValue name = 1; + google.protobuf.Int32Value age = 2; + google.protobuf.BoolValue enabled = 3; + repeated google.protobuf.Int32Value coins = 6; + repeated google.protobuf.StringValue snacks = 7; + google.protobuf.BytesValue id = 8; +} + +message Entity { + int32 id = 1; +} + +message SimpleWithMap { + map 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/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, }; } 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, } `); });