Skip to content

Commit

Permalink
fix: Support using messages called String/Boolean/Number
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilzu committed Oct 2, 2023
1 parent 4fc7940 commit 6c867ee
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 6 deletions.
10 changes: 7 additions & 3 deletions integration/global-this/global-this-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Object, Error } from './global-this';
import { Object, Error, String, Boolean, Number, Array } from "./global-this";

describe('global-this', () => {
it('generates types correctly', () => {
describe("global-this", () => {
it("generates types correctly", () => {
Object.fromPartial({});
Error.fromPartial({});
String.fromPartial({});
Boolean.fromPartial({});
Number.fromPartial({});
Array.fromPartial({});
});
});
Binary file modified integration/global-this/global-this.bin
Binary file not shown.
16 changes: 16 additions & 0 deletions integration/global-this/global-this.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@ message Object {
message Error {
string name = 1;
}

message String {
string value = 1;
}

message Boolean {
bool value = 1;
}

message Number {
double value = 1;
}

message Array {
repeated String values = 1;
}
267 changes: 265 additions & 2 deletions integration/global-this/global-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ export interface Error {
name: string;
}

export interface String {
value: string;
}

export interface Boolean {
value: boolean;
}

export interface Number {
value: number;
}

export interface Array {
values: String[];
}

function createBaseObject(): Object {
return { name: "" };
}
Expand Down Expand Up @@ -47,7 +63,7 @@ export const Object = {
},

fromJSON(object: any): Object {
return { name: isSet(object.name) ? String(object.name) : "" };
return { name: isSet(object.name) ? gt.String(object.name) : "" };
},

toJSON(message: Object): unknown {
Expand Down Expand Up @@ -104,7 +120,7 @@ export const Error = {
},

fromJSON(object: any): Error {
return { name: isSet(object.name) ? String(object.name) : "" };
return { name: isSet(object.name) ? gt.String(object.name) : "" };
},

toJSON(message: Error): unknown {
Expand All @@ -125,6 +141,253 @@ export const Error = {
},
};

function createBaseString(): String {
return { value: "" };
}

export const String = {
encode(message: String, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== "") {
writer.uint32(10).string(message.value);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): String {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseString();
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.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): String {
return { value: isSet(object.value) ? gt.String(object.value) : "" };
},

toJSON(message: String): unknown {
const obj: any = {};
if (message.value !== "") {
obj.value = message.value;
}
return obj;
},

create<I extends Exact<DeepPartial<String>, I>>(base?: I): String {
return String.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<String>, I>>(object: I): String {
const message = createBaseString();
message.value = object.value ?? "";
return message;
},
};

function createBaseBoolean(): Boolean {
return { value: false };
}

export const Boolean = {
encode(message: Boolean, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
writer.uint32(8).bool(message.value);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Boolean {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseBoolean();
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.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): Boolean {
return { value: isSet(object.value) ? gt.Boolean(object.value) : false };
},

toJSON(message: Boolean): unknown {
const obj: any = {};
if (message.value === true) {
obj.value = message.value;
}
return obj;
},

create<I extends Exact<DeepPartial<Boolean>, I>>(base?: I): Boolean {
return Boolean.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Boolean>, I>>(object: I): Boolean {
const message = createBaseBoolean();
message.value = object.value ?? false;
return message;
},
};

function createBaseNumber(): Number {
return { value: 0 };
}

export const Number = {
encode(message: Number, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== 0) {
writer.uint32(9).double(message.value);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Number {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseNumber();
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.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): Number {
return { value: isSet(object.value) ? gt.Number(object.value) : 0 };
},

toJSON(message: Number): unknown {
const obj: any = {};
if (message.value !== 0) {
obj.value = message.value;
}
return obj;
},

create<I extends Exact<DeepPartial<Number>, I>>(base?: I): Number {
return Number.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Number>, I>>(object: I): Number {
const message = createBaseNumber();
message.value = object.value ?? 0;
return message;
},
};

function createBaseArray(): Array {
return { values: [] };
}

export const Array = {
encode(message: Array, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
for (const v of message.values) {
String.encode(v!, writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Array {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseArray();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}

message.values.push(String.decode(reader, reader.uint32()));
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): Array {
return { values: gt.Array.isArray(object?.values) ? object.values.map((e: any) => String.fromJSON(e)) : [] };
},

toJSON(message: Array): unknown {
const obj: any = {};
if (message.values?.length) {
obj.values = message.values.map((e) => String.toJSON(e));
}
return obj;
},

create<I extends Exact<DeepPartial<Array>, I>>(base?: I): Array {
return Array.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Array>, I>>(object: I): Array {
const message = createBaseArray();
message.values = object.values?.map((e) => String.fromPartial(e)) || [];
return message;
},
};

declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const gt: 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> = T extends Builtin ? T
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string,
return code`BigInt(${from})`;
} else {
const cstr = capitalize(basicTypeName(ctx, field, { keepValueType: true }).toCodeString([]));
return code`${cstr}(${from})`;
return code`${utils.globalThis}.${cstr}(${from})`;
}
} else if (isObjectId(field) && options.useMongoObjectId) {
return code`${utils.fromJsonObjectId}(${from})`;
Expand Down

0 comments on commit 6c867ee

Please sign in to comment.