Skip to content

Commit

Permalink
Deprecate field helper functions from @bufbuild/protoplugin (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Feb 12, 2024
1 parent cdce618 commit 8745d72
Show file tree
Hide file tree
Showing 9 changed files with 489 additions and 159 deletions.
36 changes: 15 additions & 21 deletions packages/protoc-gen-es/src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ import type {
Printable,
Schema,
} from "@bufbuild/protoplugin/ecmascript";
import {
getFieldTyping,
getFieldIntrinsicDefaultValue,
localName,
reifyWkt,
} from "@bufbuild/protoplugin/ecmascript";
import { localName, reifyWkt } from "@bufbuild/protoplugin/ecmascript";
import { getNonEditionRuntime } from "./editions.js";
import { getFieldTypeInfo } from "./util.js";

export function generateDts(schema: Schema) {
for (const file of schema.files) {
Expand Down Expand Up @@ -128,26 +124,24 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
f.print(` } | {`);
}
f.print(f.jsDoc(field, " "));
const { typing } = getFieldTyping(field, f);
const { typing } = getFieldTypeInfo(field, f);
f.print(` value: `, typing, `;`);
f.print(` case: "`, localName(field), `";`);
}
f.print(` } | { case: undefined; value?: undefined };`);
}

// prettier-ignore
function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
f.print(f.jsDoc(field, " "));
const e: Printable = [];
e.push(" ", localName(field));
const { defaultValue } = getFieldIntrinsicDefaultValue(field);
const { typing, optional } = getFieldTyping(field, f);
if (optional || defaultValue === undefined) {
e.push("?: ", typing);
} else {
e.push(": ", typing);
}
e.push(";");
f.print(e);
f.print(f.jsDoc(field, " "));
const e: Printable = [];
const { typing, optional } = getFieldTypeInfo(field, f);
if (!optional) {
e.push(" ", localName(field), ": ", typing, ";");
} else {
e.push(" ", localName(field), "?: ", typing, ";");
}
f.print(e);
}

// prettier-ignore
Expand All @@ -156,7 +150,7 @@ function generateExtension(
f: GeneratedFile,
ext: DescExtension,
) {
const { typing } = getFieldTyping(ext, f);
const { typing } = getFieldTypeInfo(ext, f);
f.print(f.jsDoc(ext));
f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
f.print();
Expand Down Expand Up @@ -236,7 +230,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
case "google.protobuf.BoolValue":
case "google.protobuf.StringValue":
case "google.protobuf.BytesValue": {
const {typing} = getFieldTyping(ref.value, f);
const {typing} = getFieldTypeInfo(ref.value, f);
f.print(" static readonly fieldWrapper: {")
f.print(" wrapField(value: ", typing, "): ", message, ",")
f.print(" unwrapField(value: ", message, "): ", typing, ",")
Expand Down
12 changes: 6 additions & 6 deletions packages/protoc-gen-es/src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ import type {
Printable,
Schema,
} from "@bufbuild/protoplugin/ecmascript";
import {
getFieldExplicitDefaultValue,
localName,
reifyWkt,
} from "@bufbuild/protoplugin/ecmascript";
import { localName, reifyWkt } from "@bufbuild/protoplugin/ecmascript";
import { getNonEditionRuntime } from "./editions.js";
import { getFieldDefaultValueExpression } from "./util.js";

export function generateJs(schema: Schema) {
for (const file of schema.files) {
Expand Down Expand Up @@ -172,7 +169,10 @@ export function getFieldInfoLiteral(schema: Schema, field: DescField | DescExten
} else if (field.proto.label === FieldDescriptorProto_Label.REQUIRED) {
e.push(`req: true, `);
}
const defaultValue = getFieldExplicitDefaultValue(field, schema.runtime.protoInt64);
const defaultValue = getFieldDefaultValueExpression(field, {
enumAs: "enum_value_ref",
protoInt64Symbol: schema.runtime.protoInt64,
});
if (defaultValue !== undefined) {
e.push(`default: `, defaultValue, `, `);
}
Expand Down
44 changes: 23 additions & 21 deletions packages/protoc-gen-es/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ import type {
Printable,
Schema,
} from "@bufbuild/protoplugin/ecmascript";
import {
getFieldIntrinsicDefaultValue,
getFieldTyping,
localName,
reifyWkt,
} from "@bufbuild/protoplugin/ecmascript";
import { localName, reifyWkt } from "@bufbuild/protoplugin/ecmascript";
import { generateFieldInfo, getFieldInfoLiteral } from "./javascript.js";
import { getNonEditionRuntime } from "./editions.js";
import { getFieldTypeInfo, getFieldZeroValueExpression } from "./util.js";

export function generateTs(schema: Schema) {
for (const file of schema.files) {
Expand Down Expand Up @@ -152,29 +148,35 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
f.print(` } | {`);
}
f.print(f.jsDoc(field, " "));
const { typing } = getFieldTyping(field, f);
const { typing } = getFieldTypeInfo(field, f);
f.print(` value: `, typing, `;`);
f.print(` case: "`, localName(field), `";`);
}
f.print(` } | { case: undefined; value?: undefined } = { case: undefined };`);
}

// prettier-ignore
function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
f.print(f.jsDoc(field, " "));
const e: Printable = [];
e.push(" ", localName(field));
const { defaultValue, typingInferrable } =
getFieldIntrinsicDefaultValue(field);
const { typing, optional } = getFieldTyping(field, f);
if (optional || defaultValue === undefined) {
e.push("?: ", typing);
} else if (!typingInferrable) {
e.push(": ", typing);
}
if (defaultValue !== undefined) {
e.push(" = ", defaultValue);
const { typing, optional, typingInferrableFromZeroValue } = getFieldTypeInfo(field, f);
if (optional) {
e.push(" ", localName(field), "?: ", typing, ";");
} else {
if (typingInferrableFromZeroValue) {
e.push(" ", localName(field));
} else {
e.push(" ", localName(field), ": ", typing);
}
const zeroValue = getFieldZeroValueExpression(field, {
enumAs: "enum_value_ref",
protoInt64Symbol: schema.runtime.protoInt64,
});
if (zeroValue !== undefined) {
e.push(" = ", zeroValue);
}
e.push(";");
}
e.push(";");
f.print(e);
}

Expand All @@ -185,7 +187,7 @@ function generateExtension(
ext: DescExtension,
) {
const protoN = getNonEditionRuntime(schema, ext.file);
const { typing } = getFieldTyping(ext, f);
const { typing } = getFieldTypeInfo(ext, f);
f.print(f.jsDoc(ext));
f.print(f.exportDecl("const", ext), " = ", protoN, ".makeExtension<", ext.extendee, ", ", typing, ">(");
f.print(" ", f.string(ext.typeName), ", ");
Expand Down Expand Up @@ -652,7 +654,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
case "google.protobuf.BoolValue":
case "google.protobuf.StringValue":
case "google.protobuf.BytesValue": {
const {typing} = getFieldTyping(ref.value, f);
const {typing} = getFieldTypeInfo(ref.value, f);
f.print(" static readonly fieldWrapper = {")
f.print(" wrapField(value: ", typing, "): ", message, " {")
f.print(" return new ", message, "({value});")
Expand Down
Loading

0 comments on commit 8745d72

Please sign in to comment.