Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into grpc-web
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Nov 18, 2022
2 parents c976f76 + fa40a57 commit 8fca833
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Debug where options came from
build --announce_rc

build --noshow_progress

# Don't rely on test logs being easily accessible from the test runner,
# though it makes the log noisier.
test --test_output=errors
3 changes: 3 additions & 0 deletions .github/workflows/ci.nix.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This directory is configured in GitHub actions to be persisted between runs.
build --disk_cache=~/.cache/bazel
build --repository_cache=~/.cache/bazel-repo
2 changes: 0 additions & 2 deletions .github/workflows/ci.windows.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
startup --output_user_root=C:/tmp
build --noshow_progress
test --test_output=errors
38 changes: 34 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,52 @@ on:
jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node_version: [14.x, 16.x]
protoc_version: [3.x]
folder:
- '.'
- 'examples/pure'

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: ${{matrix.node_version}}

- uses: arduino/setup-protoc@v1
with:
version: ${{matrix.protoc_version}}
repo-token: ${{ secrets.GITHUB_TOKEN }}
- run: copy .github/workflows/ci.windows.bazelrc user.bazelrc

- name: Mount bazel caches
uses: actions/cache@v3
with:
path: |
"~/.cache/bazel"
"~/.cache/bazel-repo"
key: bazel-cache-${{ hashFiles('**/BUILD.bazel', '**/*.bzl', 'WORKSPACE') }}
restore-keys: bazel-cache-

- run: |
type .github/workflows/ci.bazelrc >> user.bazelrc
type .github/workflows/ci.windows.bazelrc >> user.bazelrc
if: ${{ matrix.os == 'windows-latest' }}
- run: yarn --frozen-lockfile
- run: yarn test --test_tag_filters=-no-${{ matrix.os }}
- run: cd examples/pure && yarn --frozen-lockfile && yarn test
- run: |
cat .github/workflows/ci.bazelrc >> user.bazelrc
cat .github/workflows/ci.nix.bazelrc >> user.bazelrc
if: ${{ matrix.os != 'windows-latest' }}
- name: install dependencies
run: yarn --frozen-lockfile
working-directory: ${{ matrix.folder }}
if: ${{ matrix.folder != '.' }}

- name: test
run: yarn test --test_tag_filters=-no-${{ matrix.os }}
working-directory: ${{ matrix.folder }}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[![npm](https://img.shields.io/npm/dm/protoc-gen-ts)](https://www.npmjs.com/package/protoc-gen-ts?activeTab=versions)
[![npm](https://opencollective.com/protoc-gen-ts/tiers/backer/badge.svg?label=Backer&color=brightgreen)](https://opencollective.com/protoc-gen-ts)

Aim of this protoc plugin is to make usage of protocol buffers easy in Javascript/Typescript by taking a modern approach.
This plugin generates plain **TypeScript** sources that can be used in ESM, AMD, UMD, and CommonJS module system.
Compile `.proto` files to plain TypeScript. Supports gRPC Node and gRPC Web.

## Key Differences

Expand All @@ -15,7 +14,7 @@ This plugin generates plain **TypeScript** sources that can be used in ESM, AMD,
- Enums as **enums**.
- Messages within a **namespace** if the proto has a **package** directive. (can be controlled via --ts_opt=no_namespace)
- **fromObject** and **toObject** methods to work with json data.
- Support for gRPC Node and gRPC Web (Work-in-progress) [#102](https://github.com/thesayyn/protoc-gen-ts/pull/102)
- Support for gRPC Node and gRPC Web.
- You get what you define in proto files. No such prefixes as "getField" or "getFieldList".
- Generates bindings with either as-is names (`message.field_name`) or JSON-compatible names (`message.fieldName`).

Expand Down Expand Up @@ -144,6 +143,10 @@ ts_proto_library(

* With `--ts_opt=explicit_override`, inherited methods are generated with `override` modifier, this fixes transpilation error when `noImplicitOverride` is enabled.

* With `--ts_opt=target=node`, the generated client class will be compatible with gRPC Node `@grpc/grpc-js` or `grpc`.

* With `--ts_opt=target=web`, the generated client class will be compatible with gRPC Web via `grpc-web`.

## Support

We need your constant support to keep protoc-gen-ts well maintained and add new features.
Expand Down
9 changes: 7 additions & 2 deletions bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")

package(default_visibility = ["//visibility:public"])

copy_to_bin(
name = "copy_entrypoint",
srcs = ["protoc-gen-ts.js"]
)

nodejs_binary(
name = "bin",
data = [
":protoc-gen-ts.js",
"//src",
],
entry_point = ":protoc-gen-ts.js",
# See: https://github.com/bazelbuild/rules_nodejs/issues/2600
templated_args = ["--bazel_patch_module_resolver"],
entry_point = ":copy_entrypoint",
)

exports_files(["protoc-gen-ts.js"])
2 changes: 1 addition & 1 deletion bin/protoc-gen-ts.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../src/index')
require("../src/index.js")
5 changes: 3 additions & 2 deletions index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _ts_proto_library(ctx):
outputs = outputs,
arguments = [args],
progress_message = "Generating Protocol Buffers for Typescript %s" % ctx.label,
env = {"BAZEL_BINDIR": ctx.bin_dir.path},
)

return [
Expand All @@ -92,13 +93,13 @@ ts_proto_library_ = rule(
"outs": attr.output_list(),
"_protoc_gen_ts_bin": attr.label(
executable = True,
cfg = "host",
cfg = "exec",
default = (
"//protoc-gen-ts/bin:protoc-gen-ts"
),
),
"_protoc": attr.label(
cfg = "host",
cfg = "exec",
executable = True,
allow_single_file = True,
default = (
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
},
"scripts": {
"test": "bazel test //...",
"update_checked_in": "bazel query --noshow_progress 'attr(tags, \"checked_in_update\", //...)' | xargs -n 1 bazel run --ui_event_filters=-info,-stderr --noshow_progress",
"update_checked_in_test": "bazel query --noshow_progress 'attr(tags, \"checked_in_update\", //test/...)' | xargs -n 1 bazel run --ui_event_filters=-info,-stderr --noshow_progress",
"update_checked_in_src": "bazel query --noshow_progress 'attr(tags, \"checked_in_update\", //src/...)' | xargs -n 1 bazel run --ui_event_filters=-info,-stderr --noshow_progress",
"release": "bazel run //:package.publish -- --access public --tag latest --registry https://registry.npmjs.org"
},
"bin": {
Expand All @@ -36,7 +33,6 @@
"@bazel/typescript": "5.7.1",
"@bazel/rollup": "5.7.1",
"@bazel/cypress": "5.7.1",
"@bazel/runfiles": "5.7.1",
"cypress": "7.3.0",
"@grpc/grpc-js": "^1.3.4",
"google-protobuf": "^3.19.1",
Expand Down
16 changes: 8 additions & 8 deletions src/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,20 +975,20 @@ function createGetterCall(
getterMethod = "getFieldWithDefault";
let _default: ts.Expression;

if (field.isEnum(fieldDescriptor)) {
_default = ts.factory.createPropertyAccessExpression(
type.getTypeReferenceExpr(rootDescriptor, fieldDescriptor.type_name),
fieldDescriptor.has_default_value
? fieldDescriptor.default_value
: type.getLeadingEnumMember(fieldDescriptor.type_name),
);
} else if (field.isRepeated(fieldDescriptor)) {
if (field.isRepeated(fieldDescriptor)) {
_default = fieldDescriptor.has_default_value
? ts.factory.createIdentifier(fieldDescriptor.default_value)
: ts.factory.createArrayLiteralExpression(
[],
false
);
} else if (field.isEnum(fieldDescriptor)) {
_default = ts.factory.createPropertyAccessExpression(
type.getTypeReferenceExpr(rootDescriptor, fieldDescriptor.type_name),
fieldDescriptor.has_default_value
? fieldDescriptor.default_value
: type.getLeadingEnumMember(fieldDescriptor.type_name),
);
} else if (field.isBytes(fieldDescriptor)) {
_default = fieldDescriptor.has_default_value
? ts.factory.createIdentifier(fieldDescriptor.default_value)
Expand Down
10 changes: 10 additions & 0 deletions test/_/issues/171.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

enum E {
One = 0;
Two = 1;
}

message Test {
repeated E Enums = 1;
}
9 changes: 8 additions & 1 deletion test/_/issues/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//:index.bzl", "ts_proto_library")

# gazelle:go_generate_proto false
SRCS = glob(["**/*.proto"])

proto_library(
name = "protos",
srcs = glob(["**/*.proto"]),
srcs = SRCS,
)

ts_proto_library(
name = "issues",
deps = [":protos"],
outs = [
src.replace(".proto", ".ts")
for src in SRCS
],
visibility = ["//visibility:public"],
)

build_test(
Expand Down
5 changes: 4 additions & 1 deletion test/conformance/packedproto2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ ts_project(
jasmine_node_test(
name = "test",
data = glob(["**/*.bin"]),
tags = ["no-windows-latest"],
target_compatible_with = [
"@platforms//os:linux",
"@platforms//os:macos"
],
deps = [
":tests",
],
Expand Down
77 changes: 77 additions & 0 deletions test/issues/171.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Generated by the protoc-gen-ts. DO NOT EDIT!
* compiler version: 3.19.1
* source: test/_/issues/171.proto
* git: https://github.com/thesayyn/protoc-gen-ts */
import * as pb_1 from "google-protobuf";
export enum E {
One = 0,
Two = 1
}
export class Test extends pb_1.Message {
#one_of_decls: number[][] = [];
constructor(data?: any[] | {
Enums?: E[];
}) {
super();
pb_1.Message.initialize(this, Array.isArray(data) ? data : [], 0, -1, [1], this.#one_of_decls);
if (!Array.isArray(data) && typeof data == "object") {
if ("Enums" in data && data.Enums != undefined) {
this.Enums = data.Enums;
}
}
}
get Enums() {
return pb_1.Message.getFieldWithDefault(this, 1, []) as E[];
}
set Enums(value: E[]) {
pb_1.Message.setField(this, 1, value);
}
static fromObject(data: {
Enums?: E[];
}): Test {
const message = new Test({});
if (data.Enums != null) {
message.Enums = data.Enums;
}
return message;
}
toObject() {
const data: {
Enums?: E[];
} = {};
if (this.Enums != null) {
data.Enums = this.Enums;
}
return data;
}
serialize(): Uint8Array;
serialize(w: pb_1.BinaryWriter): void;
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter();
if (this.Enums.length)
writer.writePackedEnum(1, this.Enums);
if (!w)
return writer.getResultBuffer();
}
static deserialize(bytes: Uint8Array | pb_1.BinaryReader): Test {
const reader = bytes instanceof pb_1.BinaryReader ? bytes : new pb_1.BinaryReader(bytes), message = new Test();
while (reader.nextField()) {
if (reader.isEndGroup())
break;
switch (reader.getFieldNumber()) {
case 1:
message.Enums = reader.readPackedEnum();
break;
default: reader.skipField();
}
}
return message;
}
serializeBinary(): Uint8Array {
return this.serialize();
}
static deserializeBinary(bytes: Uint8Array): Test {
return Test.deserialize(bytes);
}
}
33 changes: 33 additions & 0 deletions test/issues/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@npm//@bazel/typescript:index.bzl", "ts_project")

write_source_files(
name = "diff",
files = {
"171.ts": "//test/_/issues:171.ts"
}
)

ts_project(
name = "default",
srcs = glob(["*.ts"]),
tsconfig = {
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS"
},
},
deps = [
"@npm//@types/jasmine",
"@npm//@types/node",
"@npm//@types/google-protobuf",
"@npm//google-protobuf",
"@npm//@grpc/grpc-js",
],
)

build_test(
name = "build_test",
targets = [":default"],
)
4 changes: 4 additions & 0 deletions test/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ cypress_web_test(
"//test/web/client",
],
plugin_file = "spawn.js",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//os:macos"
]
)

go_binary(
Expand Down
Loading

0 comments on commit 8fca833

Please sign in to comment.