ts-proto 1.97.1 → 1.97.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/CHANGELOG.md +7 -0
- package/build/main.js +36 -22
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.97.2](https://github.com/stephenh/ts-proto/compare/v1.97.1...v1.97.2) (2022-01-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Performance Improvements
|
|
5
|
+
|
|
6
|
+
* fromJSON returns object literal to allow v8 optimizations ([#463](https://github.com/stephenh/ts-proto/issues/463)) ([5fcd05b](https://github.com/stephenh/ts-proto/commit/5fcd05b79e7c02547c4b6db46fae7a7202f97629))
|
|
7
|
+
|
|
1
8
|
## [1.97.1](https://github.com/stephenh/ts-proto/compare/v1.97.0...v1.97.1) (2022-01-05)
|
|
2
9
|
|
|
3
10
|
|
package/build/main.js
CHANGED
|
@@ -814,11 +814,16 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
814
814
|
function generateFromJson(ctx, fullName, messageDesc) {
|
|
815
815
|
const { options, utils, typeMap } = ctx;
|
|
816
816
|
const chunks = [];
|
|
817
|
+
const choiceFieldChunks = [];
|
|
817
818
|
// create the basic function declaration
|
|
818
819
|
chunks.push(ts_poet_1.code `
|
|
819
820
|
fromJSON(${messageDesc.field.length > 0 ? 'object' : '_'}: any): ${fullName} {
|
|
820
|
-
|
|
821
|
+
return {
|
|
821
822
|
`);
|
|
823
|
+
if (ctx.options.outputTypeRegistry) {
|
|
824
|
+
chunks.push(ts_poet_1.code `$type: ${fullName}.$type,`);
|
|
825
|
+
}
|
|
826
|
+
const oneofFieldsCases = messageDesc.oneofDecl.map((oneof, oneofIndex) => messageDesc.field.filter(types_1.isWithinOneOf).filter((field) => field.oneofIndex === oneofIndex));
|
|
822
827
|
// add a check for each incoming field
|
|
823
828
|
messageDesc.field.forEach((field) => {
|
|
824
829
|
const fieldName = case_1.maybeSnakeToCamel(field.name, options);
|
|
@@ -931,62 +936,71 @@ function generateFromJson(ctx, fullName, messageDesc) {
|
|
|
931
936
|
const fieldType = types_1.toTypeName(ctx, messageDesc, field);
|
|
932
937
|
const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
|
|
933
938
|
chunks.push(ts_poet_1.code `
|
|
934
|
-
|
|
935
|
-
acc[
|
|
936
|
-
|
|
937
|
-
|
|
939
|
+
${fieldName}: ${ctx.utils.isObject}(object.${jsonName})
|
|
940
|
+
? Object.entries(object.${jsonName}).reduce<${fieldType}>((acc, [key, value]) => {
|
|
941
|
+
acc[${i}] = ${readSnippet('value')};
|
|
942
|
+
return acc;
|
|
943
|
+
}, {})
|
|
944
|
+
: {},
|
|
938
945
|
`);
|
|
939
946
|
}
|
|
940
947
|
else {
|
|
941
948
|
const readValueSnippet = readSnippet('e');
|
|
942
949
|
if (readValueSnippet.toString() === ts_poet_1.code `e`.toString()) {
|
|
943
|
-
chunks.push(ts_poet_1.code
|
|
950
|
+
chunks.push(ts_poet_1.code `${fieldName}: Array.isArray(object?.${jsonName}) ? [...object.${jsonName}] : [],`);
|
|
944
951
|
}
|
|
945
952
|
else {
|
|
946
953
|
// Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
|
|
947
954
|
chunks.push(ts_poet_1.code `
|
|
948
|
-
|
|
955
|
+
${fieldName}: Array.isArray(object?.${jsonName}) ? object.${jsonName}.map((e: any) => ${readValueSnippet}): [],
|
|
949
956
|
`);
|
|
950
957
|
}
|
|
951
958
|
}
|
|
952
959
|
}
|
|
953
960
|
else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
|
|
954
|
-
|
|
955
|
-
const
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
961
|
+
const cases = oneofFieldsCases[field.oneofIndex];
|
|
962
|
+
const firstCase = cases[0];
|
|
963
|
+
const lastCase = cases[cases.length - 1];
|
|
964
|
+
if (field === firstCase) {
|
|
965
|
+
const fieldName = case_1.maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
|
|
966
|
+
chunks.push(ts_poet_1.code `${fieldName}: `);
|
|
967
|
+
}
|
|
968
|
+
const ternaryIf = ts_poet_1.code `${ctx.utils.isSet}(object.${jsonName})`;
|
|
969
|
+
const ternaryThen = ts_poet_1.code `{ $case: '${fieldName}', ${fieldName}: ${readSnippet(`object.${jsonName}`)}`;
|
|
970
|
+
chunks.push(ts_poet_1.code `${ternaryIf} ? ${ternaryThen}} : `);
|
|
971
|
+
if (field === lastCase) {
|
|
972
|
+
chunks.push(ts_poet_1.code `undefined,`);
|
|
973
|
+
}
|
|
960
974
|
}
|
|
961
975
|
else if (types_1.isAnyValueType(field)) {
|
|
962
|
-
chunks.push(ts_poet_1.code
|
|
976
|
+
chunks.push(ts_poet_1.code `${fieldName}: ${ctx.utils.isSet}(object?.${jsonName})
|
|
963
977
|
? ${readSnippet(`object.${jsonName}`)}
|
|
964
|
-
: undefined
|
|
978
|
+
: undefined,
|
|
965
979
|
`);
|
|
966
980
|
}
|
|
967
981
|
else if (types_1.isStructType(field)) {
|
|
968
|
-
chunks.push(ts_poet_1.code
|
|
982
|
+
chunks.push(ts_poet_1.code `${fieldName}: ${ctx.utils.isObject}(object.${jsonName})
|
|
969
983
|
? ${readSnippet(`object.${jsonName}`)}
|
|
970
|
-
: undefined
|
|
984
|
+
: undefined,`);
|
|
971
985
|
}
|
|
972
986
|
else if (types_1.isListValueType(field)) {
|
|
973
987
|
chunks.push(ts_poet_1.code `
|
|
974
|
-
|
|
988
|
+
${fieldName}: Array.isArray(object.${jsonName})
|
|
975
989
|
? ${readSnippet(`object.${jsonName}`)}
|
|
976
|
-
:
|
|
990
|
+
: undefined,
|
|
977
991
|
`);
|
|
978
992
|
}
|
|
979
993
|
else {
|
|
980
994
|
const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
|
|
981
995
|
chunks.push(ts_poet_1.code `
|
|
982
|
-
|
|
996
|
+
${fieldName}: ${ctx.utils.isSet}(object.${jsonName})
|
|
983
997
|
? ${readSnippet(`object.${jsonName}`)}
|
|
984
|
-
: ${fallback}
|
|
998
|
+
: ${fallback},
|
|
985
999
|
`);
|
|
986
1000
|
}
|
|
987
1001
|
});
|
|
988
1002
|
// and then wrap up the switch/while/return
|
|
989
|
-
chunks.push(ts_poet_1.code `
|
|
1003
|
+
chunks.push(ts_poet_1.code `};`);
|
|
990
1004
|
chunks.push(ts_poet_1.code `}`);
|
|
991
1005
|
return ts_poet_1.joinCode(chunks, { on: '\n' });
|
|
992
1006
|
}
|