Skip to content

Commit

Permalink
Expose helper WKT methods (#263)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smaye81 authored Oct 31, 2022
1 parent 3bf8328 commit fd75fa1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
3 changes: 3 additions & 0 deletions packages/protobuf/src/codegen-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ interface CodegenInfo {
readonly getUnwrappedFieldType: typeof getUnwrappedFieldType;
readonly wktSourceFiles: readonly string[];
readonly scalarDefaultValue: typeof scalarDefaultValue;
readonly reifyWkt: typeof reifyWkt;
}

type RuntimeSymbolName =
Expand Down Expand Up @@ -55,6 +57,7 @@ const packageName = "@bufbuild/protobuf";
export const codegenInfo: CodegenInfo = {
packageName,
localName,
reifyWkt,
getUnwrappedFieldType,
scalarDefaultValue,
// prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| {
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions packages/protoc-gen-es/src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/protoc-gen-es/src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/protoc-gen-es/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protoplugin/src/ecmascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fd75fa1

Please sign in to comment.