@aptre/protobuf-es-lite 0.1.5 → 0.2.0
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/README.md +4 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -1
- package/dist/is-message.d.ts +6 -2
- package/dist/is-message.js +2 -1
- package/dist/message.d.ts +34 -19
- package/dist/message.js +3 -0
- package/dist/partial.d.ts +2 -2
- package/dist/typescript.js +30 -13
- package/example/example.proto +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,9 +52,12 @@ This fork generates the ts-proto style with the protoc-gen-es tools:
|
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
54
|
const myMessage: MyMessage = {body: "Hello world"}
|
|
55
|
-
const
|
|
55
|
+
const myCompleteMessage: CompleteMessage<MyMessage> = MyMessage.create(myMessage)
|
|
56
|
+
const myMessageBin = MyMessage.toBinary(myCompleteMessage)
|
|
56
57
|
```
|
|
57
58
|
|
|
59
|
+
**Note that the default Message is equivalent to PartialMessage<T> from protobuf-es.**
|
|
60
|
+
|
|
58
61
|
## Installation
|
|
59
62
|
|
|
60
63
|
`protoc-gen-es` generates base types - messages and enumerations - from your
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { Message,
|
|
1
|
+
export { Message, CompleteMessage, AnyMessage, MessageType, Field, CompleteField, compareMessages, createMessageType, } from "./message.js";
|
|
2
|
+
export { isCompleteMessage, isCompleteField } from "./is-message.js";
|
|
2
3
|
export { newFieldList, FieldList, PartialFieldInfo, FieldInfo, OneofInfo, fieldJsonName, localFieldName, localOneofName, } from "./field.js";
|
|
3
4
|
export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.createMessageType = exports.compareMessages = void 0;
|
|
3
|
+
exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.createMessageType = exports.compareMessages = void 0;
|
|
4
4
|
var message_js_1 = require("./message.js");
|
|
5
5
|
Object.defineProperty(exports, "compareMessages", { enumerable: true, get: function () { return message_js_1.compareMessages; } });
|
|
6
6
|
Object.defineProperty(exports, "createMessageType", { enumerable: true, get: function () { return message_js_1.createMessageType; } });
|
|
7
|
+
var is_message_js_1 = require("./is-message.js");
|
|
8
|
+
Object.defineProperty(exports, "isCompleteMessage", { enumerable: true, get: function () { return is_message_js_1.isCompleteMessage; } });
|
|
9
|
+
Object.defineProperty(exports, "isCompleteField", { enumerable: true, get: function () { return is_message_js_1.isCompleteField; } });
|
|
7
10
|
var field_js_1 = require("./field.js");
|
|
8
11
|
Object.defineProperty(exports, "newFieldList", { enumerable: true, get: function () { return field_js_1.newFieldList; } });
|
|
9
12
|
Object.defineProperty(exports, "FieldList", { enumerable: true, get: function () { return field_js_1.FieldList; } });
|
package/dist/is-message.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { FieldInfo } from "./field.js";
|
|
2
|
-
import { AnyMessage, Message,
|
|
2
|
+
import { AnyMessage, CompleteMessage, Message, Field, CompleteField } from "./message.js";
|
|
3
3
|
/**
|
|
4
4
|
* Check whether the given partial has all fields present recursively.
|
|
5
5
|
*/
|
|
6
|
-
export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg:
|
|
6
|
+
export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg: Message<T>, fields: readonly FieldInfo[]): arg is CompleteMessage<T>;
|
|
7
|
+
/**
|
|
8
|
+
* Check whether the given partial field has a full value present recursively.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isCompleteField<F>(value: Field<F>, field: FieldInfo): value is CompleteField<F>;
|
package/dist/is-message.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCompleteMessage = void 0;
|
|
3
|
+
exports.isCompleteField = exports.isCompleteMessage = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Check whether the given partial has all fields present recursively.
|
|
6
6
|
*/
|
|
@@ -59,3 +59,4 @@ function isCompleteField(value, field) {
|
|
|
59
59
|
return fieldKind;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
+
exports.isCompleteField = isCompleteField;
|
package/dist/message.d.ts
CHANGED
|
@@ -1,42 +1,57 @@
|
|
|
1
1
|
import { BinaryReadOptions, JsonReadOptions, JsonValue, MessageType as BufMessageType, BinaryWriteOptions, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
|
|
2
2
|
import { FieldList, FieldListSource } from "./field.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* PartialMessage<T> constructs a type from a message. The resulting type
|
|
5
|
+
* only contains the protobuf field members of the message, and all of them
|
|
6
|
+
* are optional.
|
|
7
|
+
*
|
|
8
|
+
* Note that the optionality of the fields is the only difference between
|
|
9
|
+
* PartialMessage and PlainMessage.
|
|
10
|
+
*
|
|
11
|
+
* PartialMessage is similar to the built-in type Partial<T>, but recursive,
|
|
12
|
+
* and respects `oneof` groups.
|
|
5
13
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
14
|
+
export type Message<T extends Message<T>> = {
|
|
15
|
+
[k: string]: Field<any>;
|
|
16
|
+
};
|
|
8
17
|
/**
|
|
9
18
|
* AnyMessage is an interface implemented by all messages. If you need to
|
|
10
19
|
* handle messages of unknown type, this interface provides a convenient
|
|
11
20
|
* index signature to access fields with message["fieldname"].
|
|
12
21
|
*/
|
|
13
22
|
export interface AnyMessage extends Message<AnyMessage> {
|
|
14
|
-
[k: string]: any
|
|
23
|
+
[k: string]: Field<any>;
|
|
15
24
|
}
|
|
25
|
+
export type Field<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? Message<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
26
|
+
case: C;
|
|
27
|
+
value: Message<V>;
|
|
28
|
+
} : F extends {
|
|
29
|
+
case: string | undefined;
|
|
30
|
+
value?: unknown;
|
|
31
|
+
} ? F : F extends {
|
|
32
|
+
[key: string | number]: CompleteMessage<infer U>;
|
|
33
|
+
} ? {
|
|
34
|
+
[key: string | number]: Message<U>;
|
|
35
|
+
} : F;
|
|
16
36
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* are optional.
|
|
20
|
-
*
|
|
21
|
-
* Note that the optionality of the fields is the only difference between
|
|
22
|
-
* PartialMessage and PlainMessage.
|
|
37
|
+
* CompleteMessage<T> constructs a type from a message which requires all fields
|
|
38
|
+
* be present in the object recursively (including zero values if unset).
|
|
23
39
|
*
|
|
24
|
-
*
|
|
25
|
-
* and respects `oneof` groups.
|
|
40
|
+
* This type corresponds to Message in protobuf-es.
|
|
26
41
|
*/
|
|
27
|
-
export type
|
|
28
|
-
[P in keyof T as T[P] extends Function ? never : P]?:
|
|
42
|
+
export type CompleteMessage<T extends Message<T>> = {
|
|
43
|
+
[P in keyof T as T[P] extends Function ? never : P]?: CompleteField<T[P]>;
|
|
29
44
|
};
|
|
30
|
-
export type
|
|
45
|
+
export type CompleteField<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? CompleteMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
31
46
|
case: C;
|
|
32
|
-
value:
|
|
47
|
+
value: CompleteMessage<V>;
|
|
33
48
|
} : F extends {
|
|
34
49
|
case: string | undefined;
|
|
35
50
|
value?: unknown;
|
|
36
51
|
} ? F : F extends {
|
|
37
|
-
[key: string | number]:
|
|
52
|
+
[key: string | number]: CompleteMessage<infer U>;
|
|
38
53
|
} ? {
|
|
39
|
-
[key: string | number]:
|
|
54
|
+
[key: string | number]: CompleteMessage<U>;
|
|
40
55
|
} : F;
|
|
41
56
|
type OneofSelectedMessage<K extends string, M extends Message<M>> = {
|
|
42
57
|
case: K;
|
|
@@ -60,7 +75,7 @@ export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<Buf
|
|
|
60
75
|
/**
|
|
61
76
|
* Create a new instance of this message with zero values for fields.
|
|
62
77
|
*/
|
|
63
|
-
create(partial?:
|
|
78
|
+
create(partial?: Message<T>): T;
|
|
64
79
|
/**
|
|
65
80
|
* Create a deep copy.
|
|
66
81
|
*/
|
package/dist/message.js
CHANGED
|
@@ -22,6 +22,9 @@ const binary_js_1 = require("./binary.js");
|
|
|
22
22
|
const json_js_1 = require("./json.js");
|
|
23
23
|
// compareMessages compares two messages for equality.
|
|
24
24
|
function compareMessages(fields, a, b) {
|
|
25
|
+
if (a == null && b == null) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
25
28
|
if (a === b) {
|
|
26
29
|
return true;
|
|
27
30
|
}
|
package/dist/partial.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { FieldList } from "./field.js";
|
|
2
|
-
import { Message
|
|
3
|
-
export declare function applyPartialMessage<T extends Message<T>>(source:
|
|
2
|
+
import { Message } from "./message.js";
|
|
3
|
+
export declare function applyPartialMessage<T extends Message<T>>(source: Message<T> | undefined, target: Message<T>, fields: FieldList): void;
|
package/dist/typescript.js
CHANGED
|
@@ -80,7 +80,6 @@ function topologicalSort(messages, dependencies) {
|
|
|
80
80
|
}
|
|
81
81
|
return result;
|
|
82
82
|
}
|
|
83
|
-
// prettier-ignore
|
|
84
83
|
function generateEnum(f, enumeration) {
|
|
85
84
|
f.print(f.jsDoc(enumeration));
|
|
86
85
|
f.print(f.exportDecl("enum", enumeration), " {");
|
|
@@ -101,20 +100,18 @@ function generateEnum(f, enumeration) {
|
|
|
101
100
|
f.print("};");
|
|
102
101
|
f.print();
|
|
103
102
|
}
|
|
104
|
-
// prettier-ignore
|
|
105
103
|
function generateMessage(schema, f, message) {
|
|
106
104
|
// check if we support this runtime
|
|
107
105
|
(0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
108
106
|
f.print(f.jsDoc(message));
|
|
109
107
|
f.print(f.exportDecl("interface", message), " extends ", MessageImport, "<", message, "> {");
|
|
110
|
-
for (const
|
|
111
|
-
|
|
112
|
-
case "field":
|
|
113
|
-
generateField(f, member);
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
f.print();
|
|
108
|
+
for (const field of message.fields) {
|
|
109
|
+
generateField(f, field);
|
|
117
110
|
}
|
|
111
|
+
for (const oneof of message.oneofs) {
|
|
112
|
+
generateOneof(f, oneof);
|
|
113
|
+
}
|
|
114
|
+
f.print();
|
|
118
115
|
f.print("}");
|
|
119
116
|
f.print();
|
|
120
117
|
f.print(f.exportDecl("const", message), ": ", MessageTypeImport, "<", message, "> = ", CreateMessageTypeImport, "(");
|
|
@@ -136,18 +133,38 @@ function generateMessage(schema, f, message) {
|
|
|
136
133
|
generateMessage(schema, f, nestedMessage);
|
|
137
134
|
}
|
|
138
135
|
}
|
|
139
|
-
// prettier-ignore
|
|
140
136
|
function generateField(f, field) {
|
|
137
|
+
if (field.oneof) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
141
140
|
f.print(f.jsDoc(field, " "));
|
|
142
141
|
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
143
|
-
f.print(" ", (0, ecmascript_1.localName)(field), "
|
|
142
|
+
f.print(" ", (0, ecmascript_1.localName)(field), "?: ", typing, ";");
|
|
143
|
+
}
|
|
144
|
+
function generateOneof(f, oneof) {
|
|
145
|
+
f.print();
|
|
146
|
+
f.print(f.jsDoc(oneof, " "));
|
|
147
|
+
var oneOfCases = oneof.fields
|
|
148
|
+
.map((field) => {
|
|
149
|
+
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
150
|
+
const doc = f.jsDoc(field, " ");
|
|
151
|
+
return [
|
|
152
|
+
` | {\n`,
|
|
153
|
+
doc,
|
|
154
|
+
`\n value: `,
|
|
155
|
+
typing,
|
|
156
|
+
`;\n case: "`,
|
|
157
|
+
(0, ecmascript_1.localName)(field),
|
|
158
|
+
`";\n }`,
|
|
159
|
+
];
|
|
160
|
+
})
|
|
161
|
+
.flat();
|
|
162
|
+
f.print(" ", oneof.name, "?: {\n value?: undefined,\n case: undefined\n }", oneOfCases, ";");
|
|
144
163
|
}
|
|
145
|
-
// prettier-ignore
|
|
146
164
|
function generateFieldInfo(f, field) {
|
|
147
165
|
f.print(" ", getFieldInfoLiteral(field), ",");
|
|
148
166
|
}
|
|
149
167
|
exports.generateFieldInfo = generateFieldInfo;
|
|
150
|
-
// prettier-ignore
|
|
151
168
|
function getFieldInfoLiteral(field) {
|
|
152
169
|
const e = [];
|
|
153
170
|
e.push("{ no: ", field.number, `, `);
|
package/example/example.proto
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptre/protobuf-es-lite",
|
|
3
3
|
"description": "Lightweight Protobuf codegen for TypeScript and JavaScript.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"
|