@bufbuild/protoplugin 1.3.3 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/ecmascript/gencommon.d.ts +2 -2
- package/dist/cjs/ecmascript/gencommon.js +11 -5
- package/dist/cjs/ecmascript/index.d.ts +2 -1
- package/dist/cjs/ecmascript/index.js +4 -2
- package/dist/cjs/ecmascript/reify-wkt.d.ts +100 -0
- package/dist/cjs/ecmascript/reify-wkt.js +170 -0
- package/dist/cjs/ecmascript/runtime-imports.d.ts +1 -0
- package/dist/cjs/ecmascript/runtime-imports.js +1 -0
- package/dist/esm/ecmascript/gencommon.d.ts +2 -2
- package/dist/esm/ecmascript/gencommon.js +12 -6
- package/dist/esm/ecmascript/index.d.ts +2 -1
- package/dist/esm/ecmascript/index.js +2 -1
- package/dist/esm/ecmascript/reify-wkt.d.ts +100 -0
- package/dist/esm/ecmascript/reify-wkt.js +166 -0
- package/dist/esm/ecmascript/runtime-imports.d.ts +1 -0
- package/dist/esm/ecmascript/runtime-imports.js +1 -0
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, ScalarType } from "@bufbuild/protobuf";
|
|
1
|
+
import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, LongType, ScalarType } from "@bufbuild/protobuf";
|
|
2
2
|
import type { GeneratedFile, Printable } from "./generated-file.js";
|
|
3
3
|
import type { ImportSymbol } from "./import-symbol.js";
|
|
4
4
|
export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
|
|
@@ -12,7 +12,7 @@ export declare function getFieldTyping(field: DescField, file: GeneratedFile): {
|
|
|
12
12
|
typing: Printable;
|
|
13
13
|
optional: boolean;
|
|
14
14
|
};
|
|
15
|
-
export declare function scalarTypeScriptType(type: ScalarType): Printable;
|
|
15
|
+
export declare function scalarTypeScriptType(type: ScalarType, longType: LongType): Printable;
|
|
16
16
|
export declare function literalString(value: string): string;
|
|
17
17
|
export declare function getFieldExplicitDefaultValue(field: DescField, protoInt64Symbol: ImportSymbol): Printable | undefined;
|
|
18
18
|
export declare function getFieldIntrinsicDefaultValue(field: DescField): {
|
|
@@ -138,13 +138,13 @@ function getFieldTyping(field, file) {
|
|
|
138
138
|
let optional = false;
|
|
139
139
|
switch (field.fieldKind) {
|
|
140
140
|
case "scalar":
|
|
141
|
-
typing.push(scalarTypeScriptType(field.scalar));
|
|
141
|
+
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
142
142
|
optional = field.optional;
|
|
143
143
|
break;
|
|
144
144
|
case "message": {
|
|
145
145
|
const baseType = getUnwrappedFieldType(field);
|
|
146
146
|
if (baseType !== undefined) {
|
|
147
|
-
typing.push(scalarTypeScriptType(baseType));
|
|
147
|
+
typing.push(scalarTypeScriptType(baseType, protobuf_1.LongType.BIGINT));
|
|
148
148
|
}
|
|
149
149
|
else {
|
|
150
150
|
typing.push(file.import(field.message).toTypeOnly());
|
|
@@ -173,7 +173,7 @@ function getFieldTyping(field, file) {
|
|
|
173
173
|
let valueType;
|
|
174
174
|
switch (field.mapValue.kind) {
|
|
175
175
|
case "scalar":
|
|
176
|
-
valueType = scalarTypeScriptType(field.mapValue.scalar);
|
|
176
|
+
valueType = scalarTypeScriptType(field.mapValue.scalar, protobuf_1.LongType.BIGINT);
|
|
177
177
|
break;
|
|
178
178
|
case "message":
|
|
179
179
|
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
@@ -194,7 +194,7 @@ function getFieldTyping(field, file) {
|
|
|
194
194
|
return { typing, optional };
|
|
195
195
|
}
|
|
196
196
|
exports.getFieldTyping = getFieldTyping;
|
|
197
|
-
function scalarTypeScriptType(type) {
|
|
197
|
+
function scalarTypeScriptType(type, longType) {
|
|
198
198
|
switch (type) {
|
|
199
199
|
case protobuf_1.ScalarType.STRING:
|
|
200
200
|
return "string";
|
|
@@ -205,6 +205,9 @@ function scalarTypeScriptType(type) {
|
|
|
205
205
|
case protobuf_1.ScalarType.FIXED64:
|
|
206
206
|
case protobuf_1.ScalarType.SINT64:
|
|
207
207
|
case protobuf_1.ScalarType.INT64:
|
|
208
|
+
if (longType === protobuf_1.LongType.STRING) {
|
|
209
|
+
return "string";
|
|
210
|
+
}
|
|
208
211
|
return "bigint";
|
|
209
212
|
case protobuf_1.ScalarType.BYTES:
|
|
210
213
|
return "Uint8Array";
|
|
@@ -319,7 +322,10 @@ function getFieldIntrinsicDefaultValue(field) {
|
|
|
319
322
|
}
|
|
320
323
|
else {
|
|
321
324
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
|
|
322
|
-
defaultValue = scalarDefaultValue(field.scalar);
|
|
325
|
+
defaultValue = scalarDefaultValue(field.scalar, field.longType);
|
|
326
|
+
if (typeof defaultValue === "string") {
|
|
327
|
+
defaultValue = literalString(defaultValue);
|
|
328
|
+
}
|
|
323
329
|
}
|
|
324
330
|
}
|
|
325
331
|
break;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
export { reifyWkt } from "./reify-wkt.js";
|
|
1
2
|
export { Target } from "./target.js";
|
|
2
3
|
export { Schema } from "./schema.js";
|
|
3
4
|
export { RuntimeImports } from "./runtime-imports.js";
|
|
4
5
|
export { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
|
|
5
6
|
export { ImportSymbol } from "./import-symbol.js";
|
|
6
|
-
export declare const localName:
|
|
7
|
+
export declare const localName: (desc: import("@bufbuild/protobuf").DescEnum | import("@bufbuild/protobuf").DescMessage | import("@bufbuild/protobuf").DescService | import("@bufbuild/protobuf").DescEnumValue | import("@bufbuild/protobuf").DescField | import("@bufbuild/protobuf").DescOneof | import("@bufbuild/protobuf").DescMethod) => string;
|
|
7
8
|
export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
|
|
8
9
|
export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./custom-options.js";
|
|
@@ -13,9 +13,11 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.literalString = exports.makeJsDoc = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.createJsDocBlock = exports.
|
|
16
|
+
exports.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.literalString = exports.makeJsDoc = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.createJsDocBlock = exports.localName = exports.reifyWkt = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
-
|
|
18
|
+
var reify_wkt_js_1 = require("./reify-wkt.js");
|
|
19
|
+
Object.defineProperty(exports, "reifyWkt", { enumerable: true, get: function () { return reify_wkt_js_1.reifyWkt; } });
|
|
20
|
+
exports.localName = protobuf_1.codegenInfo.localName;
|
|
19
21
|
var gencommon_js_1 = require("./gencommon.js");
|
|
20
22
|
Object.defineProperty(exports, "createJsDocBlock", { enumerable: true, get: function () { return gencommon_js_1.createJsDocBlock; } });
|
|
21
23
|
Object.defineProperty(exports, "getFieldExplicitDefaultValue", { enumerable: true, get: function () { return gencommon_js_1.getFieldExplicitDefaultValue; } });
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { DescField, DescMessage, DescOneof } from "@bufbuild/protobuf";
|
|
2
|
+
type DescWkt = {
|
|
3
|
+
typeName: "google.protobuf.Any";
|
|
4
|
+
typeUrl: DescField;
|
|
5
|
+
value: DescField;
|
|
6
|
+
} | {
|
|
7
|
+
typeName: "google.protobuf.Timestamp";
|
|
8
|
+
seconds: DescField;
|
|
9
|
+
nanos: DescField;
|
|
10
|
+
} | {
|
|
11
|
+
typeName: "google.protobuf.Duration";
|
|
12
|
+
seconds: DescField;
|
|
13
|
+
nanos: DescField;
|
|
14
|
+
} | {
|
|
15
|
+
typeName: "google.protobuf.Struct";
|
|
16
|
+
fields: DescField & {
|
|
17
|
+
fieldKind: "map";
|
|
18
|
+
};
|
|
19
|
+
} | {
|
|
20
|
+
typeName: "google.protobuf.Value";
|
|
21
|
+
kind: DescOneof;
|
|
22
|
+
nullValue: DescField & {
|
|
23
|
+
fieldKind: "enum";
|
|
24
|
+
};
|
|
25
|
+
numberValue: DescField;
|
|
26
|
+
stringValue: DescField;
|
|
27
|
+
boolValue: DescField;
|
|
28
|
+
structValue: DescField & {
|
|
29
|
+
fieldKind: "message";
|
|
30
|
+
};
|
|
31
|
+
listValue: DescField & {
|
|
32
|
+
fieldKind: "message";
|
|
33
|
+
};
|
|
34
|
+
} | {
|
|
35
|
+
typeName: "google.protobuf.ListValue";
|
|
36
|
+
values: DescField & {
|
|
37
|
+
fieldKind: "message";
|
|
38
|
+
};
|
|
39
|
+
} | {
|
|
40
|
+
typeName: "google.protobuf.FieldMask";
|
|
41
|
+
paths: DescField;
|
|
42
|
+
} | {
|
|
43
|
+
typeName: "google.protobuf.DoubleValue";
|
|
44
|
+
value: DescField & {
|
|
45
|
+
fieldKind: "scalar";
|
|
46
|
+
};
|
|
47
|
+
} | {
|
|
48
|
+
typeName: "google.protobuf.FloatValue";
|
|
49
|
+
value: DescField & {
|
|
50
|
+
fieldKind: "scalar";
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
typeName: "google.protobuf.Int64Value";
|
|
54
|
+
value: DescField & {
|
|
55
|
+
fieldKind: "scalar";
|
|
56
|
+
};
|
|
57
|
+
} | {
|
|
58
|
+
typeName: "google.protobuf.UInt64Value";
|
|
59
|
+
value: DescField & {
|
|
60
|
+
fieldKind: "scalar";
|
|
61
|
+
};
|
|
62
|
+
} | {
|
|
63
|
+
typeName: "google.protobuf.Int32Value";
|
|
64
|
+
value: DescField & {
|
|
65
|
+
fieldKind: "scalar";
|
|
66
|
+
};
|
|
67
|
+
} | {
|
|
68
|
+
typeName: "google.protobuf.UInt32Value";
|
|
69
|
+
value: DescField & {
|
|
70
|
+
fieldKind: "scalar";
|
|
71
|
+
};
|
|
72
|
+
} | {
|
|
73
|
+
typeName: "google.protobuf.BoolValue";
|
|
74
|
+
value: DescField & {
|
|
75
|
+
fieldKind: "scalar";
|
|
76
|
+
};
|
|
77
|
+
} | {
|
|
78
|
+
typeName: "google.protobuf.StringValue";
|
|
79
|
+
value: DescField & {
|
|
80
|
+
fieldKind: "scalar";
|
|
81
|
+
};
|
|
82
|
+
} | {
|
|
83
|
+
typeName: "google.protobuf.BytesValue";
|
|
84
|
+
value: DescField & {
|
|
85
|
+
fieldKind: "scalar";
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Reifies a given DescMessage into a more concrete object representing its
|
|
90
|
+
* respective well-known type. The returned object will contain properties
|
|
91
|
+
* representing the WKT's defined fields.
|
|
92
|
+
*
|
|
93
|
+
* Useful during code generation when immediate access to a particular field
|
|
94
|
+
* is needed without having to search the object's typename and DescField list.
|
|
95
|
+
*
|
|
96
|
+
* Returns undefined if the WKT cannot be completely constructed via the
|
|
97
|
+
* DescMessage.
|
|
98
|
+
*/
|
|
99
|
+
export declare function reifyWkt(message: DescMessage): DescWkt | undefined;
|
|
100
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021-2023 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.reifyWkt = void 0;
|
|
17
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
+
/**
|
|
19
|
+
* Reifies a given DescMessage into a more concrete object representing its
|
|
20
|
+
* respective well-known type. The returned object will contain properties
|
|
21
|
+
* representing the WKT's defined fields.
|
|
22
|
+
*
|
|
23
|
+
* Useful during code generation when immediate access to a particular field
|
|
24
|
+
* is needed without having to search the object's typename and DescField list.
|
|
25
|
+
*
|
|
26
|
+
* Returns undefined if the WKT cannot be completely constructed via the
|
|
27
|
+
* DescMessage.
|
|
28
|
+
*/
|
|
29
|
+
function reifyWkt(message) {
|
|
30
|
+
switch (message.typeName) {
|
|
31
|
+
case "google.protobuf.Any": {
|
|
32
|
+
const typeUrl = message.fields.find((f) => f.number == 1 &&
|
|
33
|
+
f.fieldKind == "scalar" &&
|
|
34
|
+
f.scalar === protobuf_1.ScalarType.STRING);
|
|
35
|
+
const value = message.fields.find((f) => f.number == 2 &&
|
|
36
|
+
f.fieldKind == "scalar" &&
|
|
37
|
+
f.scalar === protobuf_1.ScalarType.BYTES);
|
|
38
|
+
if (typeUrl && value) {
|
|
39
|
+
return {
|
|
40
|
+
typeName: message.typeName,
|
|
41
|
+
typeUrl,
|
|
42
|
+
value,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case "google.protobuf.Timestamp": {
|
|
48
|
+
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
49
|
+
f.fieldKind == "scalar" &&
|
|
50
|
+
f.scalar === protobuf_1.ScalarType.INT64);
|
|
51
|
+
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
52
|
+
f.fieldKind == "scalar" &&
|
|
53
|
+
f.scalar === protobuf_1.ScalarType.INT32);
|
|
54
|
+
if (seconds && nanos) {
|
|
55
|
+
return {
|
|
56
|
+
typeName: message.typeName,
|
|
57
|
+
seconds,
|
|
58
|
+
nanos,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case "google.protobuf.Duration": {
|
|
64
|
+
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
65
|
+
f.fieldKind == "scalar" &&
|
|
66
|
+
f.scalar === protobuf_1.ScalarType.INT64);
|
|
67
|
+
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
68
|
+
f.fieldKind == "scalar" &&
|
|
69
|
+
f.scalar === protobuf_1.ScalarType.INT32);
|
|
70
|
+
if (seconds && nanos) {
|
|
71
|
+
return {
|
|
72
|
+
typeName: message.typeName,
|
|
73
|
+
seconds,
|
|
74
|
+
nanos,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case "google.protobuf.Struct": {
|
|
80
|
+
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
81
|
+
if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
|
|
82
|
+
fields.mapValue.kind !== "message" ||
|
|
83
|
+
fields.mapValue.message.typeName !== "google.protobuf.Value") {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
return { typeName: message.typeName, fields };
|
|
87
|
+
}
|
|
88
|
+
case "google.protobuf.Value": {
|
|
89
|
+
const kind = message.oneofs.find((o) => o.name === "kind");
|
|
90
|
+
const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
|
|
91
|
+
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
|
|
92
|
+
nullValue.enum.typeName !== "google.protobuf.NullValue") {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const numberValue = message.fields.find((f) => f.number == 2 &&
|
|
96
|
+
f.fieldKind == "scalar" &&
|
|
97
|
+
f.scalar === protobuf_1.ScalarType.DOUBLE &&
|
|
98
|
+
f.oneof === kind);
|
|
99
|
+
const stringValue = message.fields.find((f) => f.number == 3 &&
|
|
100
|
+
f.fieldKind == "scalar" &&
|
|
101
|
+
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
102
|
+
f.oneof === kind);
|
|
103
|
+
const boolValue = message.fields.find((f) => f.number == 4 &&
|
|
104
|
+
f.fieldKind == "scalar" &&
|
|
105
|
+
f.scalar === protobuf_1.ScalarType.BOOL &&
|
|
106
|
+
f.oneof === kind);
|
|
107
|
+
const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
|
|
108
|
+
if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
|
|
109
|
+
structValue.message.typeName !== "google.protobuf.Struct") {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
|
|
113
|
+
if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
|
|
114
|
+
listValue.message.typeName !== "google.protobuf.ListValue") {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
if (kind && numberValue && stringValue && boolValue) {
|
|
118
|
+
return {
|
|
119
|
+
typeName: message.typeName,
|
|
120
|
+
kind,
|
|
121
|
+
nullValue,
|
|
122
|
+
numberValue,
|
|
123
|
+
stringValue,
|
|
124
|
+
boolValue,
|
|
125
|
+
structValue,
|
|
126
|
+
listValue,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case "google.protobuf.ListValue": {
|
|
132
|
+
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
133
|
+
if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
|
|
134
|
+
values.message.typeName !== "google.protobuf.Value") {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
return { typeName: message.typeName, values };
|
|
138
|
+
}
|
|
139
|
+
case "google.protobuf.FieldMask": {
|
|
140
|
+
const paths = message.fields.find((f) => f.number == 1 &&
|
|
141
|
+
f.fieldKind == "scalar" &&
|
|
142
|
+
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
143
|
+
f.repeated);
|
|
144
|
+
if (paths) {
|
|
145
|
+
return { typeName: message.typeName, paths };
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case "google.protobuf.DoubleValue":
|
|
150
|
+
case "google.protobuf.FloatValue":
|
|
151
|
+
case "google.protobuf.Int64Value":
|
|
152
|
+
case "google.protobuf.UInt64Value":
|
|
153
|
+
case "google.protobuf.Int32Value":
|
|
154
|
+
case "google.protobuf.UInt32Value":
|
|
155
|
+
case "google.protobuf.BoolValue":
|
|
156
|
+
case "google.protobuf.StringValue":
|
|
157
|
+
case "google.protobuf.BytesValue": {
|
|
158
|
+
const value = message.fields.find((f) => f.number == 1 && f.name == "value");
|
|
159
|
+
if (!value) {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
if (value.fieldKind !== "scalar") {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
return { typeName: message.typeName, value };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
exports.reifyWkt = reifyWkt;
|
|
@@ -35,6 +35,7 @@ function createRuntimeImports(bootstrapWkt) {
|
|
|
35
35
|
protoDouble: infoToSymbol("protoDouble", bootstrapWkt),
|
|
36
36
|
protoInt64: infoToSymbol("protoInt64", bootstrapWkt),
|
|
37
37
|
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
|
|
38
|
+
LongType: infoToSymbol("LongType", bootstrapWkt),
|
|
38
39
|
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
|
|
39
40
|
MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
|
|
40
41
|
IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, ScalarType } from "@bufbuild/protobuf";
|
|
1
|
+
import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, LongType, ScalarType } from "@bufbuild/protobuf";
|
|
2
2
|
import type { GeneratedFile, Printable } from "./generated-file.js";
|
|
3
3
|
import type { ImportSymbol } from "./import-symbol.js";
|
|
4
4
|
export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
|
|
@@ -12,7 +12,7 @@ export declare function getFieldTyping(field: DescField, file: GeneratedFile): {
|
|
|
12
12
|
typing: Printable;
|
|
13
13
|
optional: boolean;
|
|
14
14
|
};
|
|
15
|
-
export declare function scalarTypeScriptType(type: ScalarType): Printable;
|
|
15
|
+
export declare function scalarTypeScriptType(type: ScalarType, longType: LongType): Printable;
|
|
16
16
|
export declare function literalString(value: string): string;
|
|
17
17
|
export declare function getFieldExplicitDefaultValue(field: DescField, protoInt64Symbol: ImportSymbol): Printable | undefined;
|
|
18
18
|
export declare function getFieldIntrinsicDefaultValue(field: DescField): {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import { codegenInfo, ScalarType, } from "@bufbuild/protobuf";
|
|
14
|
+
import { codegenInfo, LongType, ScalarType, } from "@bufbuild/protobuf";
|
|
15
15
|
const { localName, getUnwrappedFieldType, scalarDefaultValue } = codegenInfo;
|
|
16
16
|
export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
|
|
17
17
|
const builder = [];
|
|
@@ -132,13 +132,13 @@ export function getFieldTyping(field, file) {
|
|
|
132
132
|
let optional = false;
|
|
133
133
|
switch (field.fieldKind) {
|
|
134
134
|
case "scalar":
|
|
135
|
-
typing.push(scalarTypeScriptType(field.scalar));
|
|
135
|
+
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
136
136
|
optional = field.optional;
|
|
137
137
|
break;
|
|
138
138
|
case "message": {
|
|
139
139
|
const baseType = getUnwrappedFieldType(field);
|
|
140
140
|
if (baseType !== undefined) {
|
|
141
|
-
typing.push(scalarTypeScriptType(baseType));
|
|
141
|
+
typing.push(scalarTypeScriptType(baseType, LongType.BIGINT));
|
|
142
142
|
}
|
|
143
143
|
else {
|
|
144
144
|
typing.push(file.import(field.message).toTypeOnly());
|
|
@@ -167,7 +167,7 @@ export function getFieldTyping(field, file) {
|
|
|
167
167
|
let valueType;
|
|
168
168
|
switch (field.mapValue.kind) {
|
|
169
169
|
case "scalar":
|
|
170
|
-
valueType = scalarTypeScriptType(field.mapValue.scalar);
|
|
170
|
+
valueType = scalarTypeScriptType(field.mapValue.scalar, LongType.BIGINT);
|
|
171
171
|
break;
|
|
172
172
|
case "message":
|
|
173
173
|
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
@@ -187,7 +187,7 @@ export function getFieldTyping(field, file) {
|
|
|
187
187
|
}
|
|
188
188
|
return { typing, optional };
|
|
189
189
|
}
|
|
190
|
-
export function scalarTypeScriptType(type) {
|
|
190
|
+
export function scalarTypeScriptType(type, longType) {
|
|
191
191
|
switch (type) {
|
|
192
192
|
case ScalarType.STRING:
|
|
193
193
|
return "string";
|
|
@@ -198,6 +198,9 @@ export function scalarTypeScriptType(type) {
|
|
|
198
198
|
case ScalarType.FIXED64:
|
|
199
199
|
case ScalarType.SINT64:
|
|
200
200
|
case ScalarType.INT64:
|
|
201
|
+
if (longType === LongType.STRING) {
|
|
202
|
+
return "string";
|
|
203
|
+
}
|
|
201
204
|
return "bigint";
|
|
202
205
|
case ScalarType.BYTES:
|
|
203
206
|
return "Uint8Array";
|
|
@@ -309,7 +312,10 @@ export function getFieldIntrinsicDefaultValue(field) {
|
|
|
309
312
|
}
|
|
310
313
|
else {
|
|
311
314
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
|
|
312
|
-
defaultValue = scalarDefaultValue(field.scalar);
|
|
315
|
+
defaultValue = scalarDefaultValue(field.scalar, field.longType);
|
|
316
|
+
if (typeof defaultValue === "string") {
|
|
317
|
+
defaultValue = literalString(defaultValue);
|
|
318
|
+
}
|
|
313
319
|
}
|
|
314
320
|
}
|
|
315
321
|
break;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
export { reifyWkt } from "./reify-wkt.js";
|
|
1
2
|
export { Target } from "./target.js";
|
|
2
3
|
export { Schema } from "./schema.js";
|
|
3
4
|
export { RuntimeImports } from "./runtime-imports.js";
|
|
4
5
|
export { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
|
|
5
6
|
export { ImportSymbol } from "./import-symbol.js";
|
|
6
|
-
export declare const localName:
|
|
7
|
+
export declare const localName: (desc: import("@bufbuild/protobuf").DescEnum | import("@bufbuild/protobuf").DescMessage | import("@bufbuild/protobuf").DescService | import("@bufbuild/protobuf").DescEnumValue | import("@bufbuild/protobuf").DescField | import("@bufbuild/protobuf").DescOneof | import("@bufbuild/protobuf").DescMethod) => string;
|
|
7
8
|
export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
|
|
8
9
|
export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./custom-options.js";
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { codegenInfo } from "@bufbuild/protobuf";
|
|
15
|
-
export
|
|
15
|
+
export { reifyWkt } from "./reify-wkt.js";
|
|
16
|
+
export const { localName } = codegenInfo;
|
|
16
17
|
export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
|
|
17
18
|
export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./custom-options.js";
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { DescField, DescMessage, DescOneof } from "@bufbuild/protobuf";
|
|
2
|
+
type DescWkt = {
|
|
3
|
+
typeName: "google.protobuf.Any";
|
|
4
|
+
typeUrl: DescField;
|
|
5
|
+
value: DescField;
|
|
6
|
+
} | {
|
|
7
|
+
typeName: "google.protobuf.Timestamp";
|
|
8
|
+
seconds: DescField;
|
|
9
|
+
nanos: DescField;
|
|
10
|
+
} | {
|
|
11
|
+
typeName: "google.protobuf.Duration";
|
|
12
|
+
seconds: DescField;
|
|
13
|
+
nanos: DescField;
|
|
14
|
+
} | {
|
|
15
|
+
typeName: "google.protobuf.Struct";
|
|
16
|
+
fields: DescField & {
|
|
17
|
+
fieldKind: "map";
|
|
18
|
+
};
|
|
19
|
+
} | {
|
|
20
|
+
typeName: "google.protobuf.Value";
|
|
21
|
+
kind: DescOneof;
|
|
22
|
+
nullValue: DescField & {
|
|
23
|
+
fieldKind: "enum";
|
|
24
|
+
};
|
|
25
|
+
numberValue: DescField;
|
|
26
|
+
stringValue: DescField;
|
|
27
|
+
boolValue: DescField;
|
|
28
|
+
structValue: DescField & {
|
|
29
|
+
fieldKind: "message";
|
|
30
|
+
};
|
|
31
|
+
listValue: DescField & {
|
|
32
|
+
fieldKind: "message";
|
|
33
|
+
};
|
|
34
|
+
} | {
|
|
35
|
+
typeName: "google.protobuf.ListValue";
|
|
36
|
+
values: DescField & {
|
|
37
|
+
fieldKind: "message";
|
|
38
|
+
};
|
|
39
|
+
} | {
|
|
40
|
+
typeName: "google.protobuf.FieldMask";
|
|
41
|
+
paths: DescField;
|
|
42
|
+
} | {
|
|
43
|
+
typeName: "google.protobuf.DoubleValue";
|
|
44
|
+
value: DescField & {
|
|
45
|
+
fieldKind: "scalar";
|
|
46
|
+
};
|
|
47
|
+
} | {
|
|
48
|
+
typeName: "google.protobuf.FloatValue";
|
|
49
|
+
value: DescField & {
|
|
50
|
+
fieldKind: "scalar";
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
typeName: "google.protobuf.Int64Value";
|
|
54
|
+
value: DescField & {
|
|
55
|
+
fieldKind: "scalar";
|
|
56
|
+
};
|
|
57
|
+
} | {
|
|
58
|
+
typeName: "google.protobuf.UInt64Value";
|
|
59
|
+
value: DescField & {
|
|
60
|
+
fieldKind: "scalar";
|
|
61
|
+
};
|
|
62
|
+
} | {
|
|
63
|
+
typeName: "google.protobuf.Int32Value";
|
|
64
|
+
value: DescField & {
|
|
65
|
+
fieldKind: "scalar";
|
|
66
|
+
};
|
|
67
|
+
} | {
|
|
68
|
+
typeName: "google.protobuf.UInt32Value";
|
|
69
|
+
value: DescField & {
|
|
70
|
+
fieldKind: "scalar";
|
|
71
|
+
};
|
|
72
|
+
} | {
|
|
73
|
+
typeName: "google.protobuf.BoolValue";
|
|
74
|
+
value: DescField & {
|
|
75
|
+
fieldKind: "scalar";
|
|
76
|
+
};
|
|
77
|
+
} | {
|
|
78
|
+
typeName: "google.protobuf.StringValue";
|
|
79
|
+
value: DescField & {
|
|
80
|
+
fieldKind: "scalar";
|
|
81
|
+
};
|
|
82
|
+
} | {
|
|
83
|
+
typeName: "google.protobuf.BytesValue";
|
|
84
|
+
value: DescField & {
|
|
85
|
+
fieldKind: "scalar";
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Reifies a given DescMessage into a more concrete object representing its
|
|
90
|
+
* respective well-known type. The returned object will contain properties
|
|
91
|
+
* representing the WKT's defined fields.
|
|
92
|
+
*
|
|
93
|
+
* Useful during code generation when immediate access to a particular field
|
|
94
|
+
* is needed without having to search the object's typename and DescField list.
|
|
95
|
+
*
|
|
96
|
+
* Returns undefined if the WKT cannot be completely constructed via the
|
|
97
|
+
* DescMessage.
|
|
98
|
+
*/
|
|
99
|
+
export declare function reifyWkt(message: DescMessage): DescWkt | undefined;
|
|
100
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Copyright 2021-2023 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { ScalarType } from "@bufbuild/protobuf";
|
|
15
|
+
/**
|
|
16
|
+
* Reifies a given DescMessage into a more concrete object representing its
|
|
17
|
+
* respective well-known type. The returned object will contain properties
|
|
18
|
+
* representing the WKT's defined fields.
|
|
19
|
+
*
|
|
20
|
+
* Useful during code generation when immediate access to a particular field
|
|
21
|
+
* is needed without having to search the object's typename and DescField list.
|
|
22
|
+
*
|
|
23
|
+
* Returns undefined if the WKT cannot be completely constructed via the
|
|
24
|
+
* DescMessage.
|
|
25
|
+
*/
|
|
26
|
+
export function reifyWkt(message) {
|
|
27
|
+
switch (message.typeName) {
|
|
28
|
+
case "google.protobuf.Any": {
|
|
29
|
+
const typeUrl = message.fields.find((f) => f.number == 1 &&
|
|
30
|
+
f.fieldKind == "scalar" &&
|
|
31
|
+
f.scalar === ScalarType.STRING);
|
|
32
|
+
const value = message.fields.find((f) => f.number == 2 &&
|
|
33
|
+
f.fieldKind == "scalar" &&
|
|
34
|
+
f.scalar === ScalarType.BYTES);
|
|
35
|
+
if (typeUrl && value) {
|
|
36
|
+
return {
|
|
37
|
+
typeName: message.typeName,
|
|
38
|
+
typeUrl,
|
|
39
|
+
value,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case "google.protobuf.Timestamp": {
|
|
45
|
+
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
46
|
+
f.fieldKind == "scalar" &&
|
|
47
|
+
f.scalar === ScalarType.INT64);
|
|
48
|
+
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
49
|
+
f.fieldKind == "scalar" &&
|
|
50
|
+
f.scalar === ScalarType.INT32);
|
|
51
|
+
if (seconds && nanos) {
|
|
52
|
+
return {
|
|
53
|
+
typeName: message.typeName,
|
|
54
|
+
seconds,
|
|
55
|
+
nanos,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case "google.protobuf.Duration": {
|
|
61
|
+
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
62
|
+
f.fieldKind == "scalar" &&
|
|
63
|
+
f.scalar === ScalarType.INT64);
|
|
64
|
+
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
65
|
+
f.fieldKind == "scalar" &&
|
|
66
|
+
f.scalar === ScalarType.INT32);
|
|
67
|
+
if (seconds && nanos) {
|
|
68
|
+
return {
|
|
69
|
+
typeName: message.typeName,
|
|
70
|
+
seconds,
|
|
71
|
+
nanos,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case "google.protobuf.Struct": {
|
|
77
|
+
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
78
|
+
if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
|
|
79
|
+
fields.mapValue.kind !== "message" ||
|
|
80
|
+
fields.mapValue.message.typeName !== "google.protobuf.Value") {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
return { typeName: message.typeName, fields };
|
|
84
|
+
}
|
|
85
|
+
case "google.protobuf.Value": {
|
|
86
|
+
const kind = message.oneofs.find((o) => o.name === "kind");
|
|
87
|
+
const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
|
|
88
|
+
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
|
|
89
|
+
nullValue.enum.typeName !== "google.protobuf.NullValue") {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const numberValue = message.fields.find((f) => f.number == 2 &&
|
|
93
|
+
f.fieldKind == "scalar" &&
|
|
94
|
+
f.scalar === ScalarType.DOUBLE &&
|
|
95
|
+
f.oneof === kind);
|
|
96
|
+
const stringValue = message.fields.find((f) => f.number == 3 &&
|
|
97
|
+
f.fieldKind == "scalar" &&
|
|
98
|
+
f.scalar === ScalarType.STRING &&
|
|
99
|
+
f.oneof === kind);
|
|
100
|
+
const boolValue = message.fields.find((f) => f.number == 4 &&
|
|
101
|
+
f.fieldKind == "scalar" &&
|
|
102
|
+
f.scalar === ScalarType.BOOL &&
|
|
103
|
+
f.oneof === kind);
|
|
104
|
+
const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
|
|
105
|
+
if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
|
|
106
|
+
structValue.message.typeName !== "google.protobuf.Struct") {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
|
|
110
|
+
if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
|
|
111
|
+
listValue.message.typeName !== "google.protobuf.ListValue") {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (kind && numberValue && stringValue && boolValue) {
|
|
115
|
+
return {
|
|
116
|
+
typeName: message.typeName,
|
|
117
|
+
kind,
|
|
118
|
+
nullValue,
|
|
119
|
+
numberValue,
|
|
120
|
+
stringValue,
|
|
121
|
+
boolValue,
|
|
122
|
+
structValue,
|
|
123
|
+
listValue,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "google.protobuf.ListValue": {
|
|
129
|
+
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
130
|
+
if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
|
|
131
|
+
values.message.typeName !== "google.protobuf.Value") {
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
return { typeName: message.typeName, values };
|
|
135
|
+
}
|
|
136
|
+
case "google.protobuf.FieldMask": {
|
|
137
|
+
const paths = message.fields.find((f) => f.number == 1 &&
|
|
138
|
+
f.fieldKind == "scalar" &&
|
|
139
|
+
f.scalar === ScalarType.STRING &&
|
|
140
|
+
f.repeated);
|
|
141
|
+
if (paths) {
|
|
142
|
+
return { typeName: message.typeName, paths };
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case "google.protobuf.DoubleValue":
|
|
147
|
+
case "google.protobuf.FloatValue":
|
|
148
|
+
case "google.protobuf.Int64Value":
|
|
149
|
+
case "google.protobuf.UInt64Value":
|
|
150
|
+
case "google.protobuf.Int32Value":
|
|
151
|
+
case "google.protobuf.UInt32Value":
|
|
152
|
+
case "google.protobuf.BoolValue":
|
|
153
|
+
case "google.protobuf.StringValue":
|
|
154
|
+
case "google.protobuf.BytesValue": {
|
|
155
|
+
const value = message.fields.find((f) => f.number == 1 && f.name == "value");
|
|
156
|
+
if (!value) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
if (value.fieldKind !== "scalar") {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
return { typeName: message.typeName, value };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
@@ -32,6 +32,7 @@ export function createRuntimeImports(bootstrapWkt) {
|
|
|
32
32
|
protoDouble: infoToSymbol("protoDouble", bootstrapWkt),
|
|
33
33
|
protoInt64: infoToSymbol("protoInt64", bootstrapWkt),
|
|
34
34
|
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
|
|
35
|
+
LongType: infoToSymbol("LongType", bootstrapWkt),
|
|
35
36
|
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
|
|
36
37
|
MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
|
|
37
38
|
IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoplugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"license": "(Apache-2.0 AND BSD-3-Clause)",
|
|
5
5
|
"description": "Helps to create your own Protocol Buffers code generators.",
|
|
6
6
|
"repository": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@bufbuild/protobuf": "1.
|
|
42
|
+
"@bufbuild/protobuf": "1.4.1",
|
|
43
43
|
"@typescript/vfs": "^1.4.0",
|
|
44
44
|
"typescript": "4.5.2"
|
|
45
45
|
},
|