@bufbuild/protoplugin 1.4.0 → 1.4.2
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/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/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/package.json +2 -2
|
@@ -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;
|
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoplugin",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
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.4.
|
|
42
|
+
"@bufbuild/protobuf": "1.4.2",
|
|
43
43
|
"@typescript/vfs": "^1.4.0",
|
|
44
44
|
"typescript": "4.5.2"
|
|
45
45
|
},
|