From cb65ef209920008a3553e4072cf17a491d4971e4 Mon Sep 17 00:00:00 2001 From: Tyson Jones Date: Tue, 22 Oct 2024 11:53:39 -0600 Subject: [PATCH] Added null propagation and guard --- .../affixes/google/protobuf/timestamp.ts | 226 ++++++ .../affixes/google/protobuf/wrappers.ts | 680 ++++++++++++++++++ src/schema.ts | 4 +- 3 files changed, 908 insertions(+), 2 deletions(-) create mode 100644 integration/affixes/google/protobuf/timestamp.ts create mode 100644 integration/affixes/google/protobuf/wrappers.ts diff --git a/integration/affixes/google/protobuf/timestamp.ts b/integration/affixes/google/protobuf/timestamp.ts new file mode 100644 index 000000000..6f98eef53 --- /dev/null +++ b/integration/affixes/google/protobuf/timestamp.ts @@ -0,0 +1,226 @@ +// Code generated by protoc-gen-ts_proto. DO NOT EDIT. +// source: google/protobuf/timestamp.proto + +/* eslint-disable */ +import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire"; + +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 PrefixTimestampSuffix { + /** + * 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 createBasePrefixTimestampSuffix(): PrefixTimestampSuffix { + return { seconds: 0, nanos: 0 }; +} + +export const PrefixTimestampSuffix: MessageFns = { + encode(message: PrefixTimestampSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.seconds !== 0) { + writer.uint32(8).int64(message.seconds); + } + if (message.nanos !== 0) { + writer.uint32(16).int32(message.nanos); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixTimestampSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixTimestampSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.seconds = longToNumber(reader.int64()); + continue; + } + case 2: { + if (tag !== 16) { + break; + } + + message.nanos = reader.int32(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixTimestampSuffix { + return { + seconds: isSet(object.seconds) ? globalThis.Number(object.seconds) : 0, + nanos: isSet(object.nanos) ? globalThis.Number(object.nanos) : 0, + }; + }, + + toJSON(message: PrefixTimestampSuffix): unknown { + const obj: any = {}; + if (message.seconds !== 0) { + obj.seconds = Math.round(message.seconds); + } + if (message.nanos !== 0) { + obj.nanos = Math.round(message.nanos); + } + return obj; + }, + + create, I>>(base?: I): PrefixTimestampSuffix { + return PrefixTimestampSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixTimestampSuffix { + const message = createBasePrefixTimestampSuffix(); + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin ? T + : T extends globalThis.Array ? globalThis.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 } & { [K in Exclude>]: never }; + +function longToNumber(int64: { toString(): string }): number { + const num = globalThis.Number(int64.toString()); + if (num > globalThis.Number.MAX_SAFE_INTEGER) { + throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + } + if (num < globalThis.Number.MIN_SAFE_INTEGER) { + throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER"); + } + return num; +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} + +export interface MessageFns { + encode(message: T, writer?: BinaryWriter): BinaryWriter; + decode(input: BinaryReader | Uint8Array, length?: number): T; + fromJSON(object: any): T; + toJSON(message: T): unknown; + create, I>>(base?: I): T; + fromPartial, I>>(object: I): T; +} diff --git a/integration/affixes/google/protobuf/wrappers.ts b/integration/affixes/google/protobuf/wrappers.ts new file mode 100644 index 000000000..10e4b9ade --- /dev/null +++ b/integration/affixes/google/protobuf/wrappers.ts @@ -0,0 +1,680 @@ +// Code generated by protoc-gen-ts_proto. DO NOT EDIT. +// source: google/protobuf/wrappers.proto + +/* eslint-disable */ +import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire"; + +export const protobufPackage = "google.protobuf"; + +/** + * Wrapper message for `double`. + * + * The JSON representation for `DoubleValue` is JSON number. + */ +export interface PrefixDoubleValueSuffix { + /** The double value. */ + value: number; +} + +/** + * Wrapper message for `float`. + * + * The JSON representation for `FloatValue` is JSON number. + */ +export interface PrefixFloatValueSuffix { + /** The float value. */ + value: number; +} + +/** + * Wrapper message for `int64`. + * + * The JSON representation for `Int64Value` is JSON string. + */ +export interface PrefixInt64ValueSuffix { + /** The int64 value. */ + value: number; +} + +/** + * Wrapper message for `uint64`. + * + * The JSON representation for `UInt64Value` is JSON string. + */ +export interface PrefixUInt64ValueSuffix { + /** The uint64 value. */ + value: number; +} + +/** + * Wrapper message for `int32`. + * + * The JSON representation for `Int32Value` is JSON number. + */ +export interface PrefixInt32ValueSuffix { + /** The int32 value. */ + value: number; +} + +/** + * Wrapper message for `uint32`. + * + * The JSON representation for `UInt32Value` is JSON number. + */ +export interface PrefixUInt32ValueSuffix { + /** The uint32 value. */ + value: number; +} + +/** + * Wrapper message for `bool`. + * + * The JSON representation for `BoolValue` is JSON `true` and `false`. + */ +export interface PrefixBoolValueSuffix { + /** The bool value. */ + value: boolean; +} + +/** + * Wrapper message for `string`. + * + * The JSON representation for `StringValue` is JSON string. + */ +export interface PrefixStringValueSuffix { + /** The string value. */ + value: string; +} + +/** + * Wrapper message for `bytes`. + * + * The JSON representation for `BytesValue` is JSON string. + */ +export interface PrefixBytesValueSuffix { + /** The bytes value. */ + value: Uint8Array; +} + +function createBasePrefixDoubleValueSuffix(): PrefixDoubleValueSuffix { + return { value: 0 }; +} + +export const PrefixDoubleValueSuffix: MessageFns = { + encode(message: PrefixDoubleValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(9).double(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixDoubleValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixDoubleValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 9) { + break; + } + + message.value = reader.double(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixDoubleValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixDoubleValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = message.value; + } + return obj; + }, + + create, I>>(base?: I): PrefixDoubleValueSuffix { + return PrefixDoubleValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixDoubleValueSuffix { + const message = createBasePrefixDoubleValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixFloatValueSuffix(): PrefixFloatValueSuffix { + return { value: 0 }; +} + +export const PrefixFloatValueSuffix: MessageFns = { + encode(message: PrefixFloatValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(13).float(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixFloatValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixFloatValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 13) { + break; + } + + message.value = reader.float(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixFloatValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixFloatValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = message.value; + } + return obj; + }, + + create, I>>(base?: I): PrefixFloatValueSuffix { + return PrefixFloatValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixFloatValueSuffix { + const message = createBasePrefixFloatValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixInt64ValueSuffix(): PrefixInt64ValueSuffix { + return { value: 0 }; +} + +export const PrefixInt64ValueSuffix: MessageFns = { + encode(message: PrefixInt64ValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(8).int64(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixInt64ValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixInt64ValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.value = longToNumber(reader.int64()); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixInt64ValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixInt64ValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = Math.round(message.value); + } + return obj; + }, + + create, I>>(base?: I): PrefixInt64ValueSuffix { + return PrefixInt64ValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixInt64ValueSuffix { + const message = createBasePrefixInt64ValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixUInt64ValueSuffix(): PrefixUInt64ValueSuffix { + return { value: 0 }; +} + +export const PrefixUInt64ValueSuffix: MessageFns = { + encode(message: PrefixUInt64ValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(8).uint64(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixUInt64ValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixUInt64ValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.value = longToNumber(reader.uint64()); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixUInt64ValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixUInt64ValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = Math.round(message.value); + } + return obj; + }, + + create, I>>(base?: I): PrefixUInt64ValueSuffix { + return PrefixUInt64ValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixUInt64ValueSuffix { + const message = createBasePrefixUInt64ValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixInt32ValueSuffix(): PrefixInt32ValueSuffix { + return { value: 0 }; +} + +export const PrefixInt32ValueSuffix: MessageFns = { + encode(message: PrefixInt32ValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(8).int32(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixInt32ValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixInt32ValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.value = reader.int32(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixInt32ValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixInt32ValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = Math.round(message.value); + } + return obj; + }, + + create, I>>(base?: I): PrefixInt32ValueSuffix { + return PrefixInt32ValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixInt32ValueSuffix { + const message = createBasePrefixInt32ValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixUInt32ValueSuffix(): PrefixUInt32ValueSuffix { + return { value: 0 }; +} + +export const PrefixUInt32ValueSuffix: MessageFns = { + encode(message: PrefixUInt32ValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== 0) { + writer.uint32(8).uint32(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixUInt32ValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixUInt32ValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.value = reader.uint32(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixUInt32ValueSuffix { + return { value: isSet(object.value) ? globalThis.Number(object.value) : 0 }; + }, + + toJSON(message: PrefixUInt32ValueSuffix): unknown { + const obj: any = {}; + if (message.value !== 0) { + obj.value = Math.round(message.value); + } + return obj; + }, + + create, I>>(base?: I): PrefixUInt32ValueSuffix { + return PrefixUInt32ValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixUInt32ValueSuffix { + const message = createBasePrefixUInt32ValueSuffix(); + message.value = object.value ?? 0; + return message; + }, +}; + +function createBasePrefixBoolValueSuffix(): PrefixBoolValueSuffix { + return { value: false }; +} + +export const PrefixBoolValueSuffix: MessageFns = { + encode(message: PrefixBoolValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== false) { + writer.uint32(8).bool(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixBoolValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixBoolValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 8) { + break; + } + + message.value = reader.bool(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixBoolValueSuffix { + return { value: isSet(object.value) ? globalThis.Boolean(object.value) : false }; + }, + + toJSON(message: PrefixBoolValueSuffix): unknown { + const obj: any = {}; + if (message.value !== false) { + obj.value = message.value; + } + return obj; + }, + + create, I>>(base?: I): PrefixBoolValueSuffix { + return PrefixBoolValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixBoolValueSuffix { + const message = createBasePrefixBoolValueSuffix(); + message.value = object.value ?? false; + return message; + }, +}; + +function createBasePrefixStringValueSuffix(): PrefixStringValueSuffix { + return { value: "" }; +} + +export const PrefixStringValueSuffix: MessageFns = { + encode(message: PrefixStringValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value !== "") { + writer.uint32(10).string(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixStringValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixStringValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break; + } + + message.value = reader.string(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixStringValueSuffix { + return { value: isSet(object.value) ? globalThis.String(object.value) : "" }; + }, + + toJSON(message: PrefixStringValueSuffix): unknown { + const obj: any = {}; + if (message.value !== "") { + obj.value = message.value; + } + return obj; + }, + + create, I>>(base?: I): PrefixStringValueSuffix { + return PrefixStringValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixStringValueSuffix { + const message = createBasePrefixStringValueSuffix(); + message.value = object.value ?? ""; + return message; + }, +}; + +function createBasePrefixBytesValueSuffix(): PrefixBytesValueSuffix { + return { value: new Uint8Array(0) }; +} + +export const PrefixBytesValueSuffix: MessageFns = { + encode(message: PrefixBytesValueSuffix, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.value.length !== 0) { + writer.uint32(10).bytes(message.value); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): PrefixBytesValueSuffix { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBasePrefixBytesValueSuffix(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break; + } + + message.value = reader.bytes(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): PrefixBytesValueSuffix { + return { value: isSet(object.value) ? bytesFromBase64(object.value) : new Uint8Array(0) }; + }, + + toJSON(message: PrefixBytesValueSuffix): unknown { + const obj: any = {}; + if (message.value.length !== 0) { + obj.value = base64FromBytes(message.value); + } + return obj; + }, + + create, I>>(base?: I): PrefixBytesValueSuffix { + return PrefixBytesValueSuffix.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): PrefixBytesValueSuffix { + const message = createBasePrefixBytesValueSuffix(); + message.value = object.value ?? new Uint8Array(0); + return message; + }, +}; + +function bytesFromBase64(b64: string): Uint8Array { + if ((globalThis as any).Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); + } else { + const bin = globalThis.atob(b64); + const arr = new Uint8Array(bin.length); + for (let i = 0; i < bin.length; ++i) { + arr[i] = bin.charCodeAt(i); + } + return arr; + } +} + +function base64FromBytes(arr: Uint8Array): string { + if ((globalThis as any).Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); + } else { + const bin: string[] = []; + arr.forEach((byte) => { + bin.push(globalThis.String.fromCharCode(byte)); + }); + return globalThis.btoa(bin.join("")); + } +} + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin ? T + : T extends globalThis.Array ? globalThis.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 } & { [K in Exclude>]: never }; + +function longToNumber(int64: { toString(): string }): number { + const num = globalThis.Number(int64.toString()); + if (num > globalThis.Number.MAX_SAFE_INTEGER) { + throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + } + if (num < globalThis.Number.MIN_SAFE_INTEGER) { + throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER"); + } + return num; +} + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} + +export interface MessageFns { + encode(message: T, writer?: BinaryWriter): BinaryWriter; + decode(input: BinaryReader | Uint8Array, length?: number): T; + fromJSON(object: any): T; + toJSON(message: T): unknown; + create, I>>(base?: I): T; + fromPartial, I>>(object: I): T; +} diff --git a/src/schema.ts b/src/schema.ts index 8f3735b21..8b5e46f0a 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -239,8 +239,8 @@ function encodedOptionsToOptions( const resultOptions: Code[] = []; for (const key in encodedOptions) { const value = encodedOptions[key]; - const extension = extensionCache[extendee][parseInt(key, 10) >>> 3]; - if (shouldAddOptionDefinition(ctx, extension)) { + const extension = extensionCache[extendee]?.[parseInt(key, 10) >>> 3]; + if (extension && shouldAddOptionDefinition(ctx, extension)) { // todo: we should be able to create an option definition ALWAYS, however, // we currently cannot do that because the if the extension is a sub-message // (and thus, not just a straightforward value), we don't have an JSON object