ts-proto 1.180.0 → 1.181.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/README.markdown +5 -1
- package/build/main.js +5 -5
- package/build/options.d.ts +2 -0
- package/build/options.js +2 -0
- package/build/visit.js +4 -2
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -555,6 +555,10 @@ Generated code will be placed in the Gradle build directory.
|
|
|
555
555
|
|
|
556
556
|
- With `--ts_proto_opt=useNullAsOptional=true`, `undefined` values will be converted to `null`, and if you use `optional` label in your `.proto` file, the field will have `undefined` type as well. for example:
|
|
557
557
|
|
|
558
|
+
- With `--ts_proto_opt=typePrefix=MyPrefix`, the generated interfaces, enums, and factories will have a prefix of `MyPrefix` in their names.
|
|
559
|
+
|
|
560
|
+
- With `--ts_proto_opt=typeSuffix=MySuffix`, the generated interfaces, enums, and factories will have a suffix of `MySuffix` in their names.
|
|
561
|
+
|
|
558
562
|
```protobuf
|
|
559
563
|
message ProfileInfo {
|
|
560
564
|
int32 id = 1;
|
|
@@ -772,7 +776,7 @@ Will generate a `Foo` type with two fields: `field_a: string | undefined;` and `
|
|
|
772
776
|
|
|
773
777
|
With this output, you'll have to check both `if object.field_a` and `if object.field_b`, and if you set one, you'll have to remember to unset the other.
|
|
774
778
|
|
|
775
|
-
Instead, we recommend using the `oneof=unions-value` option, which will change the output to be an
|
|
779
|
+
Instead, we recommend using the `oneof=unions-value` option, which will change the output to be an Algebraic Data Type/ADT like:
|
|
776
780
|
|
|
777
781
|
```typescript
|
|
778
782
|
interface YourMessage {
|
package/build/main.js
CHANGED
|
@@ -1750,14 +1750,14 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1750
1750
|
}
|
|
1751
1751
|
else {
|
|
1752
1752
|
const fallback = noDefaultValue ? (0, utils_1.nullOrUndefined)(options) : "[]";
|
|
1753
|
-
const
|
|
1754
|
-
if (
|
|
1753
|
+
const needMap = readSnippet("e").toString() !== (0, ts_poet_1.code) `e`.toString();
|
|
1754
|
+
if (!needMap) {
|
|
1755
1755
|
chunks.push((0, ts_poet_1.code) `${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`);
|
|
1756
1756
|
}
|
|
1757
1757
|
else {
|
|
1758
1758
|
// Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
|
|
1759
1759
|
chunks.push((0, ts_poet_1.code) `
|
|
1760
|
-
${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${
|
|
1760
|
+
${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readSnippet("e")}): ${fallback},
|
|
1761
1761
|
`);
|
|
1762
1762
|
}
|
|
1763
1763
|
}
|
|
@@ -1969,8 +1969,8 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1969
1969
|
}
|
|
1970
1970
|
else if ((0, types_1.isRepeated)(field)) {
|
|
1971
1971
|
// Arrays might need their elements transformed
|
|
1972
|
-
const
|
|
1973
|
-
const maybeMap =
|
|
1972
|
+
const needMap = readSnippet("e").toCodeString([]) !== "e";
|
|
1973
|
+
const maybeMap = needMap ? (0, ts_poet_1.code) `.map(e => ${readSnippet("e")})` : "";
|
|
1974
1974
|
chunks.push((0, ts_poet_1.code) `
|
|
1975
1975
|
if (${messageProperty}?.length) {
|
|
1976
1976
|
${jsonProperty} = ${messageProperty}${maybeMap};
|
package/build/options.d.ts
CHANGED
|
@@ -99,6 +99,8 @@ export type Options = {
|
|
|
99
99
|
annotateFilesWithVersion: boolean;
|
|
100
100
|
noDefaultsForOptionals: boolean;
|
|
101
101
|
bigIntLiteral: boolean;
|
|
102
|
+
typePrefix: string;
|
|
103
|
+
typeSuffix: string;
|
|
102
104
|
};
|
|
103
105
|
export declare function defaultOptions(): Options;
|
|
104
106
|
export declare function optionsFromParameter(parameter: string | undefined): Options;
|
package/build/options.js
CHANGED
package/build/visit.js
CHANGED
|
@@ -11,8 +11,9 @@ function visit(proto, sourceInfo, messageFn, options, enumFn = () => { }, tsPref
|
|
|
11
11
|
const protoFullName = protoPrefix + enumDesc.name;
|
|
12
12
|
// I.e. FooBar_ZazInner
|
|
13
13
|
const tsFullName = tsPrefix + (0, case_1.maybeSnakeToCamel)(enumDesc.name, options);
|
|
14
|
+
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
|
|
14
15
|
const nestedSourceInfo = sourceInfo.open(childEnumType, index);
|
|
15
|
-
enumFn(
|
|
16
|
+
enumFn(tsFullNameWithAffixes, enumDesc, nestedSourceInfo, protoFullName);
|
|
16
17
|
});
|
|
17
18
|
const messages = "messageType" in proto ? proto.messageType : proto.nestedType;
|
|
18
19
|
const childType = isRootFile ? sourceInfo_1.Fields.file.message_type : sourceInfo_1.Fields.message.nested_type;
|
|
@@ -21,8 +22,9 @@ function visit(proto, sourceInfo, messageFn, options, enumFn = () => { }, tsPref
|
|
|
21
22
|
const protoFullName = protoPrefix + message.name;
|
|
22
23
|
// I.e. FooBar_ZazInner
|
|
23
24
|
const tsFullName = tsPrefix + (0, case_1.maybeSnakeToCamel)(messageName(message), options);
|
|
25
|
+
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
|
|
24
26
|
const nestedSourceInfo = sourceInfo.open(childType, index);
|
|
25
|
-
messageFn(
|
|
27
|
+
messageFn(tsFullNameWithAffixes, message, nestedSourceInfo, protoFullName);
|
|
26
28
|
const delim = options.useSnakeTypeName ? "_" : "";
|
|
27
29
|
visit(message, nestedSourceInfo, messageFn, options, enumFn, tsFullName + delim, protoFullName + ".");
|
|
28
30
|
});
|