Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support strict undefined properties. #1066

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration/use-strict-undefined/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oneof=unions,useStrictUndefined=true
18 changes: 18 additions & 0 deletions integration/use-strict-undefined/use-strict-undefined.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

message UseStrictUndefined {
message Inner {
}

message A {
}

message B {
}

Inner inner = 1;
oneof value {
A a = 2;
B b = 3;
}
}
267 changes: 267 additions & 0 deletions integration/use-strict-undefined/use-strict-undefined.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ function generateOneofProperty(
);

const name = maybeSnakeToCamel(messageDesc.oneofDecl[oneofIndex].name, options);
return code`${mbReadonly}${name}?: ${unionType} | ${nullOrUndefined(options)},`;
return code`${mbReadonly}${name}${ctx.options.useStrictUndefined ? '' : '?'}: ${unionType}${ctx.options.useStrictUndefined ? '' : ` | ${nullOrUndefined(options)}`},`;

/*
// Ideally we'd put the comments for each oneof field next to the anonymous
Expand Down Expand Up @@ -1105,7 +1105,7 @@ function generateBaseInstanceFactory(

return code`
function createBase${fullName}(): ${fullName} {
return { ${joinCode(fields, { on: "," })} };
return { ${joinCode(fields, { on: "," })} }${options.useStrictUndefined ? `as unknown as ${fullName}` : ''};
}
`;
}
Expand Down Expand Up @@ -1938,7 +1938,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string,

// create the basic function declaration
chunks.push(code`
fromJSON(${messageDesc.field.length > 0 ? "object" : "_"}: any): ${fullName} {
fromJSON(${messageDesc.field.length > 0 ? "object" : "_"}: any): ${ctx.options.useStrictUndefined ? `DeepPartial<${fullName}>` : fullName} {
return {
`);

Expand Down Expand Up @@ -2376,13 +2376,13 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri
// create the create function definition
if (ctx.options.useExactTypes) {
chunks.push(code`
create<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(base?: I): ${fullName} {
create<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(base?: I): ${options.useStrictUndefined ? `DeepPartial<${fullName}>` : fullName} {
return ${fullName}.fromPartial(base ?? ({} as any));
},
`);
} else {
chunks.push(code`
create(base?: ${utils.DeepPartial}<${fullName}>): ${fullName} {
create(base?: ${utils.DeepPartial}<${fullName}>): ${options.useStrictUndefined ? `DeepPartial<${fullName}>` : fullName} {
return ${fullName}.fromPartial(base ?? {});
},
`);
Expand All @@ -2393,11 +2393,11 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri

if (ctx.options.useExactTypes) {
chunks.push(code`
fromPartial<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(${paramName}: I): ${fullName} {
fromPartial<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(${paramName}: I): ${options.useStrictUndefined ? `DeepPartial<${fullName}>` : fullName} {
`);
} else {
chunks.push(code`
fromPartial(${paramName}: ${utils.DeepPartial}<${fullName}>): ${fullName} {
fromPartial(${paramName}: ${utils.DeepPartial}<${fullName}>): ${options.useStrictUndefined ? `DeepPartial<${fullName}>` : fullName} {
`);
}

Expand Down Expand Up @@ -2538,12 +2538,20 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri
const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
chunks.push(code`${messageProperty} = ${objectProperty} ?? ${fallback};`);
} else {
const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
chunks.push(code`
if(options.useStrictUndefined) {
chunks.push(code`
if (${objectProperty} !== undefined && ${objectProperty} !== null) {
${messageProperty} = ${readSnippet(`${objectProperty}`)};
}
`);
} else {
const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
chunks.push(code`
${messageProperty} = (${objectProperty} !== undefined && ${objectProperty} !== null)
? ${readSnippet(`${objectProperty}`)}
: ${fallback};
`);
}
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type Options = {
annotateFilesWithVersion: boolean;
noDefaultsForOptionals: boolean;
bigIntLiteral: boolean;
useStrictUndefined: boolean;
};

export function defaultOptions(): Options {
Expand Down Expand Up @@ -172,6 +173,7 @@ export function defaultOptions(): Options {
annotateFilesWithVersion: true,
noDefaultsForOptionals: false,
bigIntLiteral: true,
useStrictUndefined: false
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ export function toTypeName(
): Code {
function finalize(type: Code, isOptional: boolean) {
if (isOptional) {
return code`${type} | ${nullOrUndefined(ctx.options, field.proto3Optional)}`;
return code`${type}${ctx.options.useStrictUndefined ? '' : ` | ${nullOrUndefined(ctx.options, field.proto3Optional)}`}`;
}
return type;
}
Expand Down
Loading
Loading