Skip to content

Commit

Permalink
oneof field outer comment small fix & yarn proto2ts
Browse files Browse the repository at this point in the history
  • Loading branch information
sefaphlvn committed Nov 14, 2024
1 parent e55fd99 commit b2e7853
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 44 deletions.
3 changes: 2 additions & 1 deletion integration/oneof-unions-snake/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export interface Struct_FieldsEntry {
* The JSON representation for `Value` is JSON value.
*/
export interface Value {
/** The kind of value. */ kind?:
/** The kind of value. */
kind?:
| //
/** Represents a null value. */
{ $case: "null_value"; null_value: NullValue }
Expand Down
3 changes: 2 additions & 1 deletion integration/oneof-unions-value/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export interface Struct_FieldsEntry {
* The JSON representation for `Value` is JSON value.
*/
export interface Value {
/** The kind of value. */ kind?:
/** The kind of value. */
kind?:
| //
/** Represents a null value. */
{ $case: "nullValue"; value: NullValue }
Expand Down
3 changes: 2 additions & 1 deletion integration/oneof-unions-value/oneof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export interface PleaseChoose {
/**
* Please to be choosing one of the fields within this oneof clause.
* This text exists to ensure we transpose comments correctly.
*/ choice?:
*/
choice?:
| //
/**
* Use this if you want a number. Numbers are great. Who doesn't
Expand Down
3 changes: 2 additions & 1 deletion integration/oneof-unions/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export interface Struct_FieldsEntry {
* The JSON representation for `Value` is JSON value.
*/
export interface Value {
/** The kind of value. */ kind?:
/** The kind of value. */
kind?:
| //
/** Represents a null value. */
{ $case: "nullValue"; nullValue: NullValue }
Expand Down
3 changes: 2 additions & 1 deletion integration/oneof-unions/oneof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export interface PleaseChoose {
/**
* Please to be choosing one of the fields within this oneof clause.
* This text exists to ensure we transpose comments correctly.
*/ choice?:
*/
choice?:
| //
/**
* Use this if you want a number. Numbers are great. Who doesn't
Expand Down
3 changes: 2 additions & 1 deletion integration/use-readonly-types/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export interface Struct_FieldsEntry {
* The JSON representation for `Value` is JSON value.
*/
export interface Value {
/** The kind of value. */ readonly kind?:
/** The kind of value. */
readonly kind?:
| //
/** Represents a null value. */
{ readonly $case: "nullValue"; readonly nullValue: NullValue }
Expand Down
63 changes: 25 additions & 38 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,8 @@ function makeDeepPartial(options: Options, longs: ReturnType<typeof makeLongUtil

const Builtin = conditionalOutput(
"Builtin",
code`type Builtin = Date | Function | Uint8Array | string | number | boolean |${
options.forceLong === LongOption.BIGINT ? " bigint |" : ""
} undefined;`,
code`type Builtin = Date | Function | Uint8Array | string | number | boolean |${options.forceLong === LongOption.BIGINT ? " bigint |" : ""
} undefined;`,
);

// Based on https://github.com/sindresorhus/type-fest/pull/259
Expand Down Expand Up @@ -843,9 +842,8 @@ function makeMessageFns(
code`
${maybeExport} interface FieldMaskWrapperFns {
wrap(paths: ${options.useReadonlyTypes ? "readonly" : ""} string[]): FieldMask;
unwrap(message: ${options.useReadonlyTypes ? "any" : "FieldMask"}): string[] ${
options.useOptionals === "all" ? "| undefined" : ""
};
unwrap(message: ${options.useReadonlyTypes ? "any" : "FieldMask"}): string[] ${options.useOptionals === "all" ? "| undefined" : ""
};
}
`,
);
Expand Down Expand Up @@ -943,7 +941,7 @@ function makeTimestampMethods(
}
`
: options.useDate === DateOption.STRING_NANO
? code`
? code`
function toTimestamp(dateStr: string): ${Timestamp} {
const nanoDate = new ${NanoDate}(dateStr);
Expand All @@ -959,7 +957,7 @@ function makeTimestampMethods(
return { ${maybeTypeField} seconds, nanos };
}
`
: code`
: code`
function toTimestamp(date: Date): ${Timestamp} {
const seconds = ${seconds};
const nanos = (date.getTime() % 1_000) * 1_000_000;
Expand All @@ -979,7 +977,7 @@ function makeTimestampMethods(
}
`
: options.useDate === DateOption.STRING_NANO
? code`
? code`
function fromTimestamp(t: ${Timestamp}): string {
const seconds = ${toNumberCode} || 0;
const nanos = (t.nanos || 0) % 1_000;
Expand All @@ -994,7 +992,7 @@ function makeTimestampMethods(
return nanoDate.toISOStringFull();
}
`
: code`
: code`
function fromTimestamp(t: ${Timestamp}): Date {
let millis = (${toNumberCode} || 0) * 1_000;
millis += (t.nanos || 0) / 1_000_000;
Expand Down Expand Up @@ -1192,20 +1190,13 @@ function generateOneofProperty(
let fieldComments: Code[] = [];
maybeAddComment(options, fieldInfo, fieldComments);

const combinedComments = fieldComments.join('\n');
const combinedComments = fieldComments.join("\n");
return code`| // \n ${combinedComments} { ${mbReadonly}$case: '${fieldName}', ${mbReadonly}${valueName}: ${typeName} }`;
})
}),
);

const name = maybeSnakeToCamel(messageDesc.oneofDecl[oneofIndex].name, options);
return joinCode(
[
...outerComments,
code`${mbReadonly}${name}?:`,
unionType,
code`| ${nullOrUndefined(options)},`,
]
);
return joinCode([...outerComments, code`${mbReadonly}${name}?:`, unionType, code`| ${nullOrUndefined(options)},`], { on: "\n" });
}

// Create a function that constructs 'base' instance with default values for decode to use as a prototype
Expand Down Expand Up @@ -1246,12 +1237,12 @@ function generateBaseInstanceFactory(
const val = isWithinOneOf(field)
? nullOrUndefined(options)
: isMapType(ctx, messageDesc, field)
? shouldGenerateJSMapType(ctx, messageDesc, field)
? "new Map()"
: "{}"
: isRepeated(field)
? "[]"
: defaultValue(ctx, field);
? shouldGenerateJSMapType(ctx, messageDesc, field)
? "new Map()"
: "{}"
: isRepeated(field)
? "[]"
: defaultValue(ctx, field);

fields.push(code`${fieldKey}: ${val}`);
}
Expand Down Expand Up @@ -1431,11 +1422,10 @@ function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorP
options,
`${varName}.value`,
)}`;
const maybeIfKeyCheck = `${
options.noDefaultsForOptionals
const maybeIfKeyCheck = `${options.noDefaultsForOptionals
? ` && ${varName}.key !== undefined ${withAndMaybeCheckIsNotNull(options, `${varName}.key`)}`
: ""
}`;
}`;

chunks.push(code`
${tagCheck}
Expand Down Expand Up @@ -1754,9 +1744,8 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP
writer.uint32(${tag}).fork();
for (const v of ${messageProperty}) {
if (BigInt.asIntN(64, v) !== v) {
throw new ${
utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
throw new ${utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
}
writer.${toReaderCall(field)}(${rhs("v")});
}
Expand All @@ -1769,9 +1758,8 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP
writer.uint32(${tag}).fork();
for (const v of ${messageProperty}) {
if (BigInt.asUintN(64, v) !== v) {
throw new ${
utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
throw new ${utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
}
writer.${toReaderCall(field)}(${rhs("v")});
}
Expand Down Expand Up @@ -2302,9 +2290,8 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string,
} else {
// Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
chunks.push(code`
${fieldKey}: ${
ctx.utils.globalThis
}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readSnippet("e")}): ${fallback},
${fieldKey}: ${ctx.utils.globalThis
}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readSnippet("e")}): ${fallback},
`);
}
}
Expand Down

0 comments on commit b2e7853

Please sign in to comment.