From fd75fa11fa565f5961271d5c4f4d1d089b59b293 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Mon, 31 Oct 2022 11:10:23 -0400 Subject: [PATCH] Expose helper WKT methods (#263) Exposes the WKT helper method previously known as `matchWkt`. This method helps to transform a general `DescMessage` into a more concrete `DescWkt` type, which allows for easier access to the WKT's fields. Useful during code generation functionality. --- packages/protobuf/src/codegen-info.ts | 3 +++ .../src/private/reify-wkt.ts} | 21 ++++++++++++------- packages/protoc-gen-es/src/declaration.ts | 6 +++--- packages/protoc-gen-es/src/javascript.ts | 6 +++--- packages/protoc-gen-es/src/typescript.ts | 6 +++--- packages/protoplugin/src/ecmascript/index.ts | 2 +- 6 files changed, 27 insertions(+), 17 deletions(-) rename packages/{protoc-gen-es/src/match-wkt.ts => protobuf/src/private/reify-wkt.ts} (92%) diff --git a/packages/protobuf/src/codegen-info.ts b/packages/protobuf/src/codegen-info.ts index d62f5e7f3..949a4e8e1 100644 --- a/packages/protobuf/src/codegen-info.ts +++ b/packages/protobuf/src/codegen-info.ts @@ -15,6 +15,7 @@ import { localName } from "./private/names.js"; import { getUnwrappedFieldType } from "./private/field-wrapper.js"; import { scalarDefaultValue } from "./private/scalars.js"; +import { reifyWkt } from "./private/reify-wkt.js"; interface CodegenInfo { readonly packageName: string; @@ -23,6 +24,7 @@ interface CodegenInfo { readonly getUnwrappedFieldType: typeof getUnwrappedFieldType; readonly wktSourceFiles: readonly string[]; readonly scalarDefaultValue: typeof scalarDefaultValue; + readonly reifyWkt: typeof reifyWkt; } type RuntimeSymbolName = @@ -55,6 +57,7 @@ const packageName = "@bufbuild/protobuf"; export const codegenInfo: CodegenInfo = { packageName, localName, + reifyWkt, getUnwrappedFieldType, scalarDefaultValue, // prettier-ignore diff --git a/packages/protoc-gen-es/src/match-wkt.ts b/packages/protobuf/src/private/reify-wkt.ts similarity index 92% rename from packages/protoc-gen-es/src/match-wkt.ts rename to packages/protobuf/src/private/reify-wkt.ts index 60cf15bdf..c0ab018df 100644 --- a/packages/protoc-gen-es/src/match-wkt.ts +++ b/packages/protobuf/src/private/reify-wkt.ts @@ -12,12 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - DescField, - DescMessage, - DescOneof, - ScalarType, -} from "@bufbuild/protobuf"; +import type { DescField, DescMessage, DescOneof } from "../descriptor-set.js"; +import { ScalarType } from "../field.js"; type DescWkt = | { @@ -94,7 +90,18 @@ type DescWkt = value: DescField & { fieldKind: "scalar" }; }; -export function matchWkt(message: DescMessage): DescWkt | undefined { +/** + * Reifies a given DescMessage into a more concrete object representing its + * respective well-known type. The returned object will contain properties + * representing the WKT's defined fields. + * + * Useful during code generation when immediate access to a particular field + * is needed without having to search the object's typename and DescField list. + * + * Returns undefined if the WKT cannot be completely constructed via the + * DescMessage. + */ +export function reifyWkt(message: DescMessage): DescWkt | undefined { switch (message.typeName) { case "google.protobuf.Any": { const typeUrl = message.fields.find( diff --git a/packages/protoc-gen-es/src/declaration.ts b/packages/protoc-gen-es/src/declaration.ts index 1605bd865..3ed932458 100644 --- a/packages/protoc-gen-es/src/declaration.ts +++ b/packages/protoc-gen-es/src/declaration.ts @@ -28,8 +28,8 @@ import { literalString, localName, makeJsDoc, + reifyWkt, } from "@bufbuild/protoplugin/ecmascript"; -import { matchWkt } from "./match-wkt.js"; export function generateDts(schema: Schema) { for (const file of schema.files) { @@ -145,7 +145,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) { // prettier-ignore function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } @@ -190,7 +190,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa // prettier-ignore function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } diff --git a/packages/protoc-gen-es/src/javascript.ts b/packages/protoc-gen-es/src/javascript.ts index 1e4520768..c63e5d6e5 100644 --- a/packages/protoc-gen-es/src/javascript.ts +++ b/packages/protoc-gen-es/src/javascript.ts @@ -24,8 +24,8 @@ import { literalString, localName, makeJsDoc, + reifyWkt, } from "@bufbuild/protoplugin/ecmascript"; -import { matchWkt } from "./match-wkt.js"; export function generateJs(schema: Schema) { for (const file of schema.files) { @@ -157,7 +157,7 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF // prettier-ignore function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } @@ -516,7 +516,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa // prettier-ignore function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } diff --git a/packages/protoc-gen-es/src/typescript.ts b/packages/protoc-gen-es/src/typescript.ts index 00601aa4b..8bd426ebb 100644 --- a/packages/protoc-gen-es/src/typescript.ts +++ b/packages/protoc-gen-es/src/typescript.ts @@ -29,8 +29,8 @@ import { getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, + reifyWkt, } from "@bufbuild/protoplugin/ecmascript"; -import { matchWkt } from "./match-wkt.js"; import { generateFieldInfo } from "./javascript.js"; import { literalString } from "@bufbuild/protoplugin/ecmascript"; @@ -175,7 +175,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) { // prettier-ignore function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } @@ -540,7 +540,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa // prettier-ignore function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) { - const ref = matchWkt(message); + const ref = reifyWkt(message); if (ref === undefined) { return; } diff --git a/packages/protoplugin/src/ecmascript/index.ts b/packages/protoplugin/src/ecmascript/index.ts index 501c73200..754a0dc38 100644 --- a/packages/protoplugin/src/ecmascript/index.ts +++ b/packages/protoplugin/src/ecmascript/index.ts @@ -20,7 +20,7 @@ export { RuntimeImports } from "./runtime-imports.js"; export { GeneratedFile, FileInfo, Printable } from "./generated-file.js"; export { ImportSymbol } from "./import-symbol.js"; -export const { localName } = codegenInfo; +export const { localName, reifyWkt } = codegenInfo; export { createJsDocBlock,