Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change non-cast variable #95

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update toJson
  • Loading branch information
namnt-xproz committed Aug 5, 2022
commit 0c00e2ba41b204939bd095333faff719eab846e7
18 changes: 9 additions & 9 deletions src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const toJsonClass = (
// By default this line starts with keyword List, slice will remove it.
if (input.nullSafety) {
const isNullable = typeDef.nullable;
sb += printLine(`'${typeDef.jsonKey}': ${thisKey}?`);
sb += printLine(`if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}?`);
sb += Array.from(result).map(_ => printLine('.map((e) => e')).slice(0, -1).join('');
if (typeDef.isDate) {
sb += printLine(`.map((e) => ${toIsoString('e', isNullable)})`);
Expand All @@ -321,7 +321,7 @@ const toJsonClass = (
sb += Array.from(result).map(_ => printLine('.toList())')).slice(0, -1).join('');
sb += printLine('.toList(),');
} else {
sb += printLine(`'${typeDef.jsonKey}': ${thisKey}`);
sb += printLine(`if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}`);
sb += Array.from(result).map(_ => printLine('?.map((e) => e')).slice(0, -1).join('');
if (typeDef.isDate) {
sb += printLine(`?.map((e) => ${toIsoString('e')})`);
Expand All @@ -335,25 +335,25 @@ const toJsonClass = (
if (input.nullSafety) {
const isNullable = typeDef.nullable;
if (typeDef.isDate) {
sb = `'${typeDef.jsonKey}': ${thisKey}?.map((e) => ${toIsoString('e', isNullable)}).toList(),`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}?.map((e) => ${toIsoString('e', isNullable)}).toList(),`;
} else {
sb = `'${typeDef.jsonKey}': ${thisKey}?.map((e) => ${buildToJsonClass('e', isNullable, input)}).toList(),`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}?.map((e) => ${buildToJsonClass('e', isNullable, input)}).toList(),`;
}
} else {
if (typeDef.isDate) {
sb = `'${typeDef.jsonKey}': ${thisKey}?.map((e) => ${toIsoString('e')})?.toList(),`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}?.map((e) => ${toIsoString('e')})?.toList(),`;
} else {
sb = `'${typeDef.jsonKey}': ${thisKey}?.map((e) => ${buildToJsonClass('e', false, input)})?.toList(),`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey}?.map((e) => ${buildToJsonClass('e', false, input)})?.toList(),`;
}
}
}
} else {
// Class
const isNullable = input.nullSafety && !typeDef.nullable;
if (typeDef.isDate) {
sb = `'${typeDef.jsonKey}': ${toIsoString(thisKey, isNullable)},`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${toIsoString(thisKey, isNullable)},`;
} else {
sb = `'${typeDef.jsonKey}': ${buildToJsonClass(thisKey, isNullable, input)},`;
sb = `if (${thisKey} != null) '${typeDef.jsonKey}': ${buildToJsonClass(thisKey, isNullable, input)},`;
}
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ export function toJsonExpression(
if (typeDef.isDate) {
return toJsonClass(typeDef, privateField, input);
} else {
return `if (${typeDef.jsonKey} != null) '${typeDef.jsonKey}': ${thisKey},`;
return `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey},`;
}
} else {
return toJsonClass(typeDef, privateField, input);
Expand Down