Skip to content

Commit 0d3e652

Browse files
authored
Merge pull request #398 from stackb/pcj/example-ts-proto-outputs
Add example of ts-proto vendored assets
2 parents a19d9ee + d814594 commit 0d3e652

8 files changed

Lines changed: 371 additions & 11 deletions

File tree

.bazelrc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ common --incompatible_use_plus_in_repo_names
1010
# =========================================================================
1111

1212
# Don't leak PATH and LD_LIBRARY_PATH into the build.
13-
# build --incompatible_strict_action_env
13+
build --incompatible_strict_action_env
1414

1515
# Don't use legacy repository rules.
1616
build --incompatible_disable_native_repo_rules
1717

1818
# C++17 for protobuf compatibility
19-
build --host_cxxopt=-std=c++17
20-
build --cxxopt=-std=c++17
19+
build --host_cxxopt=-std=c++17 --cxxopt=-std=c++17
2120

2221
# To facilitate testing in bazelci incompatible flags
2322
# @see https://github.com/bazelbuild/bazel/pull/26906#issue-3386957462
2423
# build --incompatible_autoload_externally=@rules_python,+java_common,+JavaInfo,+JavaPluginInfo,+ProguardSpecProvider,+java_binary,+java_import,+java_library,+java_plugin,+java_test,+java_runtime,+java_toolchain,+java_package_configuration,@com_google_protobuf,@rules_shell,+@rules_android,+@rules_cc
2524
# build --incompatible_autoload_externally=@rules_shell
2625
build --incompatible_autoload_externally=
26+
27+
# This flag is present only to demonstrate the effect on generated ts-proto
28+
# assets (see example/assets/*.ts). Without this, the generated descriptor sets
29+
# from the proto_library rule will be devoid of source info.
30+
build --experimental_proto_descriptor_sets_include_source_info

example/assets/BUILD.bazel

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ load("@rules_proto//proto:defs.bzl", "proto_library")
99
# gazelle:proto_language java enable false
1010
# gazelle:proto_language python enable false
1111
# gazelle:proto_language scala enable false
12-
# gazelle:proto_language ts_proto enable false
1312

1413
# -- modifies the inherited go configuration
14+
# gazelle:proto_language go enable true
1515
# gazelle:proto_language go -rule proto_compile
16-
# gazelle:proto_language go -rule proto_go_library
1716
# gazelle:proto_language go +rule proto_compiled_sources
17+
# gazelle:proto_language go -rule proto_go_library
18+
19+
# -- modifies the inherited ts_proto configuration
20+
# gazelle:proto_language ts_proto enable true
21+
# gazelle:proto_language ts_proto -rule proto_compile
22+
# gazelle:proto_language ts_proto +rule proto_compiled_sources
23+
# gazelle:proto_language ts_proto -rule proto_ts_library
1824

1925
proto_compile_assets(
2026
name = "assets",
2127
deps = [
2228
":api_go_compiled_sources",
2329
":api_request_go_compiled_sources",
30+
":api_request_ts_proto_compiled_sources",
2431
":api_response_go_compiled_sources",
32+
":api_response_ts_proto_compiled_sources",
33+
":api_ts_proto_compiled_sources",
2534
],
2635
)
2736

@@ -73,3 +82,45 @@ proto_compiled_sources(
7382
proto = "api_response_proto",
7483
visibility = ["//visibility:public"],
7584
)
85+
86+
proto_compiled_sources(
87+
name = "api_ts_proto_compiled_sources",
88+
srcs = ["api.ts"],
89+
args = ["--include_source_info"],
90+
options = {"@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto": [
91+
"emitImportedFiles=false",
92+
"esModuleInterop=true",
93+
"comments=true",
94+
]},
95+
plugins = ["@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto"],
96+
proto = "api_proto",
97+
visibility = ["//visibility:public"],
98+
)
99+
100+
proto_compiled_sources(
101+
name = "api_request_ts_proto_compiled_sources",
102+
srcs = ["api_request.ts"],
103+
args = ["--include_source_info"],
104+
options = {"@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto": [
105+
"emitImportedFiles=false",
106+
"esModuleInterop=true",
107+
"comments=true",
108+
]},
109+
plugins = ["@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto"],
110+
proto = "api_request_proto",
111+
visibility = ["//visibility:public"],
112+
)
113+
114+
proto_compiled_sources(
115+
name = "api_response_ts_proto_compiled_sources",
116+
srcs = ["api_response.ts"],
117+
args = ["--include_source_info"],
118+
options = {"@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto": [
119+
"emitImportedFiles=false",
120+
"esModuleInterop=true",
121+
"comments=true",
122+
]},
123+
plugins = ["@build_stack_rules_proto//plugin/stephenh/ts-proto:protoc-gen-ts-proto"],
124+
proto = "api_response_proto",
125+
visibility = ["//visibility:public"],
126+
)

example/assets/api.pb.go

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/assets/api.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ts-proto: Adding a comment to the syntax will become the first
2+
// comment in the output source file.
13
syntax = "proto3";
24

35
package example.assets;
@@ -7,7 +9,11 @@ import "example/assets/api_response.proto";
79

810
option go_package = "github.com/stackb/rules_proto/example/assets;assets";
911

12+
// ApiExchange is a demonstrative example of an API call that has a request and
13+
// a response.
1014
message ApiExchange {
15+
// request is the API request
1116
ApiRequest request = 1;
17+
// response is the API response
1218
ApiResponse response = 2;
1319
}

example/assets/api.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* eslint-disable */
2+
import _m0 from "protobufjs/minimal";
3+
import { ApiRequest } from "./api_request";
4+
import { ApiResponse } from "./api_response";
5+
6+
export const protobufPackage = "example.assets";
7+
8+
/**
9+
* ts-proto: Adding a comment to the syntax will become the first
10+
* comment in the output source file.
11+
*/
12+
13+
/**
14+
* ApiExchange is a demonstrative example of an API call that has a request and
15+
* a response.
16+
*/
17+
export interface ApiExchange {
18+
/** request is the API request */
19+
request:
20+
| ApiRequest
21+
| undefined;
22+
/** response is the API response */
23+
response: ApiResponse | undefined;
24+
}
25+
26+
function createBaseApiExchange(): ApiExchange {
27+
return { request: undefined, response: undefined };
28+
}
29+
30+
export const ApiExchange = {
31+
encode(message: ApiExchange, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
32+
if (message.request !== undefined) {
33+
ApiRequest.encode(message.request, writer.uint32(10).fork()).ldelim();
34+
}
35+
if (message.response !== undefined) {
36+
ApiResponse.encode(message.response, writer.uint32(18).fork()).ldelim();
37+
}
38+
return writer;
39+
},
40+
41+
decode(input: _m0.Reader | Uint8Array, length?: number): ApiExchange {
42+
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
43+
let end = length === undefined ? reader.len : reader.pos + length;
44+
const message = createBaseApiExchange();
45+
while (reader.pos < end) {
46+
const tag = reader.uint32();
47+
switch (tag >>> 3) {
48+
case 1:
49+
if (tag !== 10) {
50+
break;
51+
}
52+
53+
message.request = ApiRequest.decode(reader, reader.uint32());
54+
continue;
55+
case 2:
56+
if (tag !== 18) {
57+
break;
58+
}
59+
60+
message.response = ApiResponse.decode(reader, reader.uint32());
61+
continue;
62+
}
63+
if ((tag & 7) === 4 || tag === 0) {
64+
break;
65+
}
66+
reader.skipType(tag & 7);
67+
}
68+
return message;
69+
},
70+
71+
fromJSON(object: any): ApiExchange {
72+
return {
73+
request: isSet(object.request) ? ApiRequest.fromJSON(object.request) : undefined,
74+
response: isSet(object.response) ? ApiResponse.fromJSON(object.response) : undefined,
75+
};
76+
},
77+
78+
toJSON(message: ApiExchange): unknown {
79+
const obj: any = {};
80+
if (message.request !== undefined) {
81+
obj.request = ApiRequest.toJSON(message.request);
82+
}
83+
if (message.response !== undefined) {
84+
obj.response = ApiResponse.toJSON(message.response);
85+
}
86+
return obj;
87+
},
88+
89+
create<I extends Exact<DeepPartial<ApiExchange>, I>>(base?: I): ApiExchange {
90+
return ApiExchange.fromPartial(base ?? {});
91+
},
92+
93+
fromPartial<I extends Exact<DeepPartial<ApiExchange>, I>>(object: I): ApiExchange {
94+
const message = createBaseApiExchange();
95+
message.request = (object.request !== undefined && object.request !== null)
96+
? ApiRequest.fromPartial(object.request)
97+
: undefined;
98+
message.response = (object.response !== undefined && object.response !== null)
99+
? ApiResponse.fromPartial(object.response)
100+
: undefined;
101+
return message;
102+
},
103+
};
104+
105+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
106+
107+
export type DeepPartial<T> = T extends Builtin ? T
108+
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
109+
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
110+
: Partial<T>;
111+
112+
type KeysOfUnion<T> = T extends T ? keyof T : never;
113+
export type Exact<P, I extends P> = P extends Builtin ? P
114+
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
115+
116+
function isSet(value: any): boolean {
117+
return value !== null && value !== undefined;
118+
}

example/assets/api_request.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* eslint-disable */
2+
import _m0 from "protobufjs/minimal";
3+
4+
export const protobufPackage = "example.assets";
5+
6+
export interface ApiRequest {
7+
header: string[];
8+
}
9+
10+
function createBaseApiRequest(): ApiRequest {
11+
return { header: [] };
12+
}
13+
14+
export const ApiRequest = {
15+
encode(message: ApiRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
16+
for (const v of message.header) {
17+
writer.uint32(10).string(v!);
18+
}
19+
return writer;
20+
},
21+
22+
decode(input: _m0.Reader | Uint8Array, length?: number): ApiRequest {
23+
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
24+
let end = length === undefined ? reader.len : reader.pos + length;
25+
const message = createBaseApiRequest();
26+
while (reader.pos < end) {
27+
const tag = reader.uint32();
28+
switch (tag >>> 3) {
29+
case 1:
30+
if (tag !== 10) {
31+
break;
32+
}
33+
34+
message.header.push(reader.string());
35+
continue;
36+
}
37+
if ((tag & 7) === 4 || tag === 0) {
38+
break;
39+
}
40+
reader.skipType(tag & 7);
41+
}
42+
return message;
43+
},
44+
45+
fromJSON(object: any): ApiRequest {
46+
return { header: Array.isArray(object?.header) ? object.header.map((e: any) => String(e)) : [] };
47+
},
48+
49+
toJSON(message: ApiRequest): unknown {
50+
const obj: any = {};
51+
if (message.header?.length) {
52+
obj.header = message.header;
53+
}
54+
return obj;
55+
},
56+
57+
create<I extends Exact<DeepPartial<ApiRequest>, I>>(base?: I): ApiRequest {
58+
return ApiRequest.fromPartial(base ?? {});
59+
},
60+
61+
fromPartial<I extends Exact<DeepPartial<ApiRequest>, I>>(object: I): ApiRequest {
62+
const message = createBaseApiRequest();
63+
message.header = object.header?.map((e) => e) || [];
64+
return message;
65+
},
66+
};
67+
68+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
69+
70+
export type DeepPartial<T> = T extends Builtin ? T
71+
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
72+
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
73+
: Partial<T>;
74+
75+
type KeysOfUnion<T> = T extends T ? keyof T : never;
76+
export type Exact<P, I extends P> = P extends Builtin ? P
77+
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

0 commit comments

Comments
 (0)