Skip to content

Commit

Permalink
Remove fromPartialX types for proto2
Browse files Browse the repository at this point in the history
  • Loading branch information
tresabhi committed Oct 5, 2024
1 parent 096c4b8 commit 9ec9cb0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
25 changes: 21 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,14 @@ export type Utils = ReturnType<typeof makeDeepPartial> &
ReturnType<typeof makeAssertionUtils> &
ReturnType<typeof makeMessageFns>;

export enum MakeUtilsExportCreatePartialAs {
EXPORT_AS_UNDEFINED,
EXPORT_AS_DEFINED,
EXPORT_AS_OPTIONAL,
}

/** These are runtime utility methods used by the generated code. */
export function makeUtils(options: Options): Utils {
export function makeUtils(options: Options, exportPartialAs: MakeUtilsExportCreatePartialAs): Utils {
const bytes = makeByteUtils(options);
const longs = makeLongUtils(options, bytes);
const deepPartial = makeDeepPartial(options, longs);
Expand All @@ -485,7 +491,7 @@ export function makeUtils(options: Options): Utils {
...makeGrpcWebErrorClass(bytes),
...extension,
...makeAssertionUtils(bytes),
...makeMessageFns(options, deepPartial, extension),
...makeMessageFns(options, deepPartial, extension, exportPartialAs),
};
}

Expand Down Expand Up @@ -707,6 +713,7 @@ function makeMessageFns(
options: Options,
deepPartial: ReturnType<typeof makeDeepPartial>,
extension: ReturnType<typeof makeExtensionClass>,
exportPartialAs: MakeUtilsExportCreatePartialAs,
) {
const BinaryWriter = imp("BinaryWriter@@bufbuild/protobuf/wire");
const BinaryReader = imp("BinaryReader@@bufbuild/protobuf/wire");
Expand Down Expand Up @@ -773,10 +780,20 @@ function makeMessageFns(
if (options.outputPartialMethods) {
if (options.useExactTypes) {
commonStaticMembers.push(code`create<I extends ${Exact}<${DeepPartial}<T>, I>>(base?: I): T;`);
commonStaticMembers.push(code`fromPartial<I extends ${Exact}<${DeepPartial}<T>, I>>(object: I): T;`);

if (exportPartialAs === MakeUtilsExportCreatePartialAs.EXPORT_AS_DEFINED) {
commonStaticMembers.push(code`fromPartial<I extends ${Exact}<${DeepPartial}<T>, I>>(object: I): T;`);
} else if (exportPartialAs === MakeUtilsExportCreatePartialAs.EXPORT_AS_OPTIONAL) {
commonStaticMembers.push(code`fromPartial?: <I extends ${Exact}<${DeepPartial}<T>, I>>(object: I) => T;`);
}
} else {
commonStaticMembers.push(code`create(base?: DeepPartial<T>): T;`);
commonStaticMembers.push(code`fromPartial(object: DeepPartial<T>): T;`);

if (exportPartialAs === MakeUtilsExportCreatePartialAs.EXPORT_AS_DEFINED) {
commonStaticMembers.push(code`fromPartial(object: DeepPartial<T>): T;`);
} else if (exportPartialAs === MakeUtilsExportCreatePartialAs.EXPORT_AS_OPTIONAL) {
commonStaticMembers.push(code`fromPartial?: (object: DeepPartial<T>) => T;`);
}
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
FileDescriptorProto,
} from "ts-proto-descriptors";
import { promisify } from "util";
import { generateIndexFiles, getVersions, protoFilesToGenerate, readToBuffer } from "./utils";
import { generateFile, makeUtils } from "./main";
import { createTypeMap } from "./types";
import { BaseContext, createFileContext } from "./context";
import { getTsPoetOpts, optionsFromParameter } from "./options";
import { generateTypeRegistry } from "./generate-type-registry";
import { generateFile, makeUtils, MakeUtilsExportCreatePartialAs } from "./main";
import { getTsPoetOpts, optionsFromParameter } from "./options";
import { createTypeMap } from "./types";
import { generateIndexFiles, getVersions, protoFilesToGenerate, readToBuffer } from "./utils";

// this would be the plugin called by the protoc compiler
async function main() {
Expand All @@ -23,8 +23,6 @@ async function main() {

const options = optionsFromParameter(request.parameter);
const typeMap = createTypeMap(request, options);
const utils = makeUtils(options);
const ctx: BaseContext = { typeMap, options, utils };

let filesToGenerate: FileDescriptorProto[];

Expand All @@ -50,14 +48,22 @@ async function main() {

const files = await Promise.all(
filesToGenerate.map(async (file) => {
const [path, code] = generateFile({ ...ctx, currentFile: createFileContext(file) }, file);
const fileContext = createFileContext(file);
const utils = makeUtils(
options,
fileContext.isProto3Syntax ?
MakeUtilsExportCreatePartialAs.EXPORT_AS_DEFINED :
MakeUtilsExportCreatePartialAs.EXPORT_AS_UNDEFINED
);
const ctx: BaseContext = { typeMap, options, utils };
const [path, code] = generateFile({ ...ctx, currentFile: fileContext }, file);
const content = code.toString({ ...getTsPoetOpts(options, tsProtoVersion, protocVersion, file.name), path });
return { name: path, content };
}),
);

if (options.outputTypeRegistry) {
const utils = makeUtils(options);
const utils = makeUtils(options, MakeUtilsExportCreatePartialAs.EXPORT_AS_OPTIONAL);
const ctx: BaseContext = { options, typeMap, utils };

const path = "typeRegistry.ts";
Expand Down

0 comments on commit 9ec9cb0

Please sign in to comment.