ts-proto 1.129.0 → 1.131.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/CHANGELOG.md +14 -0
- package/README.markdown +20 -0
- package/build/main.js +11 -11
- package/build/options.js +2 -0
- package/build/types.js +10 -3
- package/build/visit.js +2 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [1.131.0](https://github.com/stephenh/ts-proto/compare/v1.130.0...v1.131.0) (2022-10-25)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* option useSnakeTypeName ([#694](https://github.com/stephenh/ts-proto/issues/694)) ([ad73ff9](https://github.com/stephenh/ts-proto/commit/ad73ff9341d0d593156d10b5d96c4a47afdc802d))
|
|
7
|
+
|
|
8
|
+
# [1.130.0](https://github.com/stephenh/ts-proto/compare/v1.129.0...v1.130.0) (2022-10-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* support `useReadonlyTypes` option ([#691](https://github.com/stephenh/ts-proto/issues/691)) ([4b87334](https://github.com/stephenh/ts-proto/commit/4b8733452fb59cea3b9fb4721159b97e6df59854))
|
|
14
|
+
|
|
1
15
|
# [1.129.0](https://github.com/stephenh/ts-proto/compare/v1.128.0...v1.129.0) (2022-10-16)
|
|
2
16
|
|
|
3
17
|
|
package/README.markdown
CHANGED
|
@@ -462,6 +462,26 @@ Generated code will be placed in the Gradle build directory.
|
|
|
462
462
|
|
|
463
463
|
The default behavior is `useMapType=false`, which makes it generate the code for protobuf `map<key_type, value_type` with the key-value pair like `{[key: key_type]: value_type}`.
|
|
464
464
|
|
|
465
|
+
- With `--ts_proto_opt=useReadonlyTypes=true`, the generated types will be declared as immutable using typescript's `readonly` modifer.
|
|
466
|
+
|
|
467
|
+
- With `--ts_proto_opt=useSnakeTypeName=false` will remove snake casing from types.
|
|
468
|
+
|
|
469
|
+
Example Protobuf
|
|
470
|
+
```protobuf
|
|
471
|
+
message Box {
|
|
472
|
+
message Element {
|
|
473
|
+
message Image {
|
|
474
|
+
enum Alignment {
|
|
475
|
+
LEFT = 1;
|
|
476
|
+
CENTER = 2;
|
|
477
|
+
RIGHT = 3;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
by default this is enabled which would generate a type of `Box_Element_Image_Alignment`. By disabling this option the type that is generated would be `BoxElementImageAlignment`.
|
|
484
|
+
|
|
465
485
|
### NestJS Support
|
|
466
486
|
|
|
467
487
|
We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown).
|
package/build/main.js
CHANGED
|
@@ -530,7 +530,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
530
530
|
const name = (0, case_1.maybeSnakeToCamel)(fieldDesc.name, options);
|
|
531
531
|
const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc);
|
|
532
532
|
const q = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options) ? "?" : "";
|
|
533
|
-
chunks.push((0, ts_poet_1.code) `${name}${q}: ${type}, `);
|
|
533
|
+
chunks.push((0, ts_poet_1.code) `${ctx.options.useReadonlyTypes ? "readonly " : ""}${name}${q}: ${type}, `);
|
|
534
534
|
});
|
|
535
535
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
536
536
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
@@ -623,8 +623,8 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
623
623
|
): ${fullName} {
|
|
624
624
|
const reader = input instanceof ${Reader} ? input : new ${Reader}(input);
|
|
625
625
|
let end = length === undefined ? reader.len : reader.pos + length;
|
|
626
|
-
const message = ${createBase};
|
|
627
626
|
`);
|
|
627
|
+
chunks.push((0, ts_poet_1.code) `const message = ${createBase}${options.useReadonlyTypes ? " as any" : ""};`);
|
|
628
628
|
if (options.unknownFields) {
|
|
629
629
|
chunks.push((0, ts_poet_1.code) `(message as any)._unknownFields = {}`);
|
|
630
630
|
}
|
|
@@ -1377,7 +1377,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1377
1377
|
if (options.usePrototypeForDefaults) {
|
|
1378
1378
|
createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
|
|
1379
1379
|
}
|
|
1380
|
-
chunks.push((0, ts_poet_1.code) `const message = ${createBase};`);
|
|
1380
|
+
chunks.push((0, ts_poet_1.code) `const message = ${createBase}${options.useReadonlyTypes ? " as any" : ""};`);
|
|
1381
1381
|
// add a check for each incoming field
|
|
1382
1382
|
messageDesc.field.forEach((field) => {
|
|
1383
1383
|
const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
|
|
@@ -1524,7 +1524,7 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1524
1524
|
if ((0, types_1.isAnyValueTypeName)(fullProtoTypeName)) {
|
|
1525
1525
|
if (ctx.options.oneof === options_1.OneofOption.UNIONS) {
|
|
1526
1526
|
chunks.push((0, ts_poet_1.code) `wrap(value: any): Value {
|
|
1527
|
-
const result = createBaseValue();
|
|
1527
|
+
const result = createBaseValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
|
|
1528
1528
|
|
|
1529
1529
|
if (value === null) {
|
|
1530
1530
|
result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: NullValue.NULL_VALUE};
|
|
@@ -1547,7 +1547,7 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1547
1547
|
}
|
|
1548
1548
|
else {
|
|
1549
1549
|
chunks.push((0, ts_poet_1.code) `wrap(value: any): Value {
|
|
1550
|
-
const result = createBaseValue();
|
|
1550
|
+
const result = createBaseValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
|
|
1551
1551
|
|
|
1552
1552
|
if (value === null) {
|
|
1553
1553
|
result.${fieldNames.nullValue} = NullValue.NULL_VALUE;
|
|
@@ -1570,8 +1570,8 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1570
1570
|
}
|
|
1571
1571
|
}
|
|
1572
1572
|
if ((0, types_1.isListValueTypeName)(fullProtoTypeName)) {
|
|
1573
|
-
chunks.push((0, ts_poet_1.code) `wrap(value: Array<any> | undefined): ListValue {
|
|
1574
|
-
const result = createBaseListValue();
|
|
1573
|
+
chunks.push((0, ts_poet_1.code) `wrap(value: ${ctx.options.useReadonlyTypes ? "ReadonlyArray<any>" : "Array<any>"} | undefined): ListValue {
|
|
1574
|
+
const result = createBaseListValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
|
|
1575
1575
|
|
|
1576
1576
|
result.values = value ?? [];
|
|
1577
1577
|
|
|
@@ -1579,8 +1579,8 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1579
1579
|
}`);
|
|
1580
1580
|
}
|
|
1581
1581
|
if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
|
|
1582
|
-
chunks.push((0, ts_poet_1.code) `wrap(paths: string[]): FieldMask {
|
|
1583
|
-
const result = createBaseFieldMask();
|
|
1582
|
+
chunks.push((0, ts_poet_1.code) `wrap(paths: ${ctx.options.useReadonlyTypes ? "readonly " : ""} string[]): FieldMask {
|
|
1583
|
+
const result = createBaseFieldMask()${ctx.options.useReadonlyTypes ? " as any" : ""};
|
|
1584
1584
|
|
|
1585
1585
|
result.paths = paths;
|
|
1586
1586
|
|
|
@@ -1640,12 +1640,12 @@ function generateUnwrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1640
1640
|
}
|
|
1641
1641
|
}
|
|
1642
1642
|
if ((0, types_1.isListValueTypeName)(fullProtoTypeName)) {
|
|
1643
|
-
chunks.push((0, ts_poet_1.code) `unwrap(message: ListValue): Array<any> {
|
|
1643
|
+
chunks.push((0, ts_poet_1.code) `unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "ListValue"}): Array<any> {
|
|
1644
1644
|
return message.values;
|
|
1645
1645
|
}`);
|
|
1646
1646
|
}
|
|
1647
1647
|
if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
|
|
1648
|
-
chunks.push((0, ts_poet_1.code) `unwrap(message: FieldMask): string[] {
|
|
1648
|
+
chunks.push((0, ts_poet_1.code) `unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "FieldMask"}): string[] {
|
|
1649
1649
|
return message.paths;
|
|
1650
1650
|
}`);
|
|
1651
1651
|
}
|
package/build/options.js
CHANGED
package/build/types.js
CHANGED
|
@@ -457,13 +457,17 @@ function valueTypeName(ctx, typeName) {
|
|
|
457
457
|
? (0, ts_poet_1.code) `string`
|
|
458
458
|
: (0, ts_poet_1.code) `Uint8Array`;
|
|
459
459
|
case ".google.protobuf.ListValue":
|
|
460
|
-
return (0, ts_poet_1.code) `Array<any>`;
|
|
460
|
+
return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `ReadonlyArray<any>` : (0, ts_poet_1.code) `Array<any>`;
|
|
461
461
|
case ".google.protobuf.Value":
|
|
462
462
|
return (0, ts_poet_1.code) `any`;
|
|
463
463
|
case ".google.protobuf.Struct":
|
|
464
|
-
return (0, ts_poet_1.code) `{[key: string]: any}`;
|
|
464
|
+
return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `{readonly [key: string]: any}` : (0, ts_poet_1.code) `{[key: string]: any}`;
|
|
465
465
|
case ".google.protobuf.FieldMask":
|
|
466
|
-
return ctx.options.useJsonWireFormat
|
|
466
|
+
return ctx.options.useJsonWireFormat
|
|
467
|
+
? (0, ts_poet_1.code) `string`
|
|
468
|
+
: ctx.options.useReadonlyTypes
|
|
469
|
+
? (0, ts_poet_1.code) `readonly string[]`
|
|
470
|
+
: (0, ts_poet_1.code) `string[]`;
|
|
467
471
|
case ".google.protobuf.Duration":
|
|
468
472
|
return ctx.options.useJsonWireFormat ? (0, ts_poet_1.code) `string` : undefined;
|
|
469
473
|
case ".google.protobuf.Timestamp":
|
|
@@ -562,6 +566,9 @@ function toTypeName(ctx, messageDesc, field) {
|
|
|
562
566
|
}
|
|
563
567
|
return (0, ts_poet_1.code) `{ [key: ${keyType} ]: ${valueType} }`;
|
|
564
568
|
}
|
|
569
|
+
if (ctx.options.useReadonlyTypes) {
|
|
570
|
+
return (0, ts_poet_1.code) `readonly ${type}[]`;
|
|
571
|
+
}
|
|
565
572
|
return (0, ts_poet_1.code) `${type}[]`;
|
|
566
573
|
}
|
|
567
574
|
if (isValueType(ctx, field)) {
|
package/build/visit.js
CHANGED
|
@@ -23,7 +23,8 @@ function visit(proto, sourceInfo, messageFn, options, enumFn = () => { }, tsPref
|
|
|
23
23
|
const tsFullName = tsPrefix + (0, case_1.maybeSnakeToCamel)(messageName(message), options);
|
|
24
24
|
const nestedSourceInfo = sourceInfo.open(childType, index);
|
|
25
25
|
messageFn(tsFullName, message, nestedSourceInfo, protoFullName);
|
|
26
|
-
|
|
26
|
+
const delim = options.useSnakeTypeName ? "_" : "";
|
|
27
|
+
visit(message, nestedSourceInfo, messageFn, options, enumFn, tsFullName + delim, protoFullName + ".");
|
|
27
28
|
});
|
|
28
29
|
}
|
|
29
30
|
exports.visit = visit;
|