Skip to content

Conversation

@timostamm
Copy link
Member

This switches the code generator from Go to a TypeScript implementation. There are only very minor changes in the generated code, and some API related to reflection.

New features

  • createDescriptorSet() provides a nice way to inspect a set of google.protobuf.FileDescriptorProto
  • @bufbuild/protoplugin provides an early version of an API to write your own code generator plugins in TypeScript

Breaking changes

  • The code generator can no longer be installed via go.
  • class TypeRegistry is deprecated. Please use createRegistry() instead.
  • class DescriptorRegistry has been replaced by createRegistryFromDescriptors(). The old version is still available as class LegacyDescriptorRegistry
  • class DescriptorSet has been replaced by createDescriptorSet(). The old version is still available as class LegacyDescriptorSet.
  • The signatures of makeEnum(), setEnumType() and makeEnumType() have changed slightly for improved enum value names.

Changes in generated code

Imported names are now sorted case-insensitively:

- import {StringValue, proto3} from "@bufbuild/protobuf";
+ import {proto3, StringValue} from "@bufbuild/protobuf";

Uint8Array literals drop the trailing comma:

- { no: 75, name: "default_bytes", kind: "scalar", T: 12 /* ScalarType.BYTES */, opt: true, default: new Uint8Array([0x77, 0x6F, 0x72, 0x6C, 0x64, ]) },
+ { no: 75, name: "default_bytes", kind: "scalar", T: 12 /* ScalarType.BYTES */, opt: true, default: new Uint8Array([0x77, 0x6F, 0x72, 0x6C, 0x64 ]) },

Float and double literals do not use the exponential notation anymore:

- { no: 251, name: "default_float", kind: "scalar", T: 2 /* ScalarType.FLOAT */, opt: true, default: 9e+09 },
+ { no: 251, name: "default_float", kind: "scalar", T: 2 /* ScalarType.FLOAT */, opt: true, default: 9000000000 },

Whitespace in comments is treated slightly different in some edge cases:

  /**
-  *
-  *
   * @generated from field: string this_field_has_an_empty_comment = 4;
   */
  thisFieldHasAnEmptyComment = "";

Prefixed enum values are now stripped more consequently:

export enum BufAlphaRegistryV1Alpha1UserState {
  /**
   * @generated from enum value: BUF_ALPHA_REGISTRY_V1_ALPHA1_USER_STATE_UNSPECIFIED = 0;
   */
- BUF_ALPHA_REGISTRY_V1_ALPHA1_USER_STATE_UNSPECIFIED = 0,
+ UNSPECIFIED = 0,

In generated JavaScript, stripped enum value names are now passed explicitly instead of deriving them:

export const Field_Cardinality = proto3.makeEnum(
  "google.protobuf.Field.Cardinality",
  [
-   {no: 0, name: "CARDINALITY_UNKNOWN"},
-   {no: 1, name: "CARDINALITY_OPTIONAL"},
-   {no: 2, name: "CARDINALITY_REQUIRED"},
-   {no: 3, name: "CARDINALITY_REPEATED"},
+   {no: 0, name: "CARDINALITY_UNKNOWN", localName: "UNKNOWN"},
+   {no: 1, name: "CARDINALITY_OPTIONAL", localName: "OPTIONAL"},
+   {no: 2, name: "CARDINALITY_REQUIRED", localName: "REQUIRED"},
+   {no: 3, name: "CARDINALITY_REPEATED", localName: "REPEATED"},
 ],
- {sharedPrefix: "CARDINALITY_"},
);

@smaye81 smaye81 self-requested a review July 21, 2022 23:20
@timostamm timostamm merged commit f15b514 into main Jul 22, 2022
@timostamm timostamm deleted the tstamm/port-code-gen-to-ts branch July 22, 2022 08:10
@timostamm timostamm mentioned this pull request Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants