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
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 3.6.0

- Fix with dynamic List and Map response

## 3.5.9

- Non cast variable

## 3.5.8

- Now can detect fractional seconds.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

<!-- BADGES -->
<p align="center">
<a href="https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart"><img src="https://vsmarketplacebadge.apphb.com/version/hirantha.json-to-dart.svg?labelColor=009903&style=flat-square" alt="Version"></a>
<!-- <a href="https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart"><img src="https://vsmarketplacebadge.apphb.com/version/hirantha.json-to-dart.svg?labelColor=009903&style=flat-square" alt="Version"></a>
<a href="https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart"><img src="https://vsmarketplacebadge.apphb.com/installs/hirantha.json-to-dart.svg?label=Installs&labelColor=009903&style=flat-square" alt="Install"></a>
<a href="https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart"><img src="https://vsmarketplacebadge.apphb.com/downloads/hirantha.json-to-dart.svg?label=Downloads&labelColor=009903&style=flat-square" alt="Download"></a>
<a href="https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart"><img src="https://vsmarketplacebadge.apphb.com/downloads/hirantha.json-to-dart.svg?label=Downloads&labelColor=009903&style=flat-square" alt="Download"></a> -->
<a href="https://www.hirantha.xyz"><img src="https://img.shields.io/badge/Ask%20Me-Anything-1abc9c.svg?labelColor=007d80&style=flat-square" alt="Ask Me Anything"></a>
<a href="https://github.com/hiranthaR/Json-to-Dart-Model/issues"><img src="https://img.shields.io/github/issues/hiranthaR/Json-to-Dart-Model?label=Issues&labelColor=c95149&logo=github&color=ff5b4f&style=flat-square" alt="Issues"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-orange.svg?labelColor=d67c15&style=flat-square" alt="License: MIT"></a>
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "json-to-dart",
"displayName": "Json to Dart Model",
"name": "json-to-dart-enhanced",
"displayName": "Json to Dart Model Enhanced",
"description": "Extension convert Json to Dart Model class",
"version": "3.5.8",
"publisher": "hirantha",
"version": "3.6.0",
"publisher": "HuongTran",
"icon": "icon.png",
"engines": {
"vscode": "^1.59.0"
Expand Down Expand Up @@ -38,9 +38,9 @@
],
"repository": {
"type": "git",
"url": "https://github.com/hiranthar/Json-to-Dart-Model.git"
"url": "https://github.com/tranhuongk/Json-to-Dart-Model.git"
},
"author": "https://github.com/hiranthar",
"author": "https://github.com/tranhuongk",
"main": "./out/index.js",
"contributes": {
"commands": [
Expand Down
50 changes: 30 additions & 20 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 All @@ -367,9 +367,13 @@ export function jsonParseValue(
) {
const jsonValue = valueFromJson(key, input);
const nullable = questionMark(input, typeDef);
const isDouble = typeDef.type === 'double';
const isNumber = typeDef.type === 'double' || typeDef.type === 'int';
const isString = typeDef.type === 'String';
const isMap = typeDef.type?.includes('Map');
const isList = typeDef.type?.includes('List');
const isBool = typeDef.type === 'bool';
const dafaultVal = typeDef.defaultValue
? `${isDouble ? '' : '?'} ?? ${defaultValue(typeDef, input.nullSafety, false).replace(/=|const/gi, '').trim()}`
? `${isNumber ? '' : '?'} ?? ${defaultValue(typeDef, input.nullSafety, false).replace(/=|const/gi, '').trim()}`
: '';
const IfNull = `${includeIfNull(jsonValue, input)}`;
let formatedValue = '';
Expand All @@ -382,9 +386,16 @@ export function jsonParseValue(
} else {
if (!typeDef.nullable && !typeDef.isList) {
formatedValue = `${jsonValue}`;
} if (isDouble) {
const nullableDouble = input.nullSafety && !typeDef.required ? '?' : '';
formatedValue = `${IfNull}(${jsonValue}${required} as num${nullableDouble})${nullableDouble}.toDouble()` + dafaultVal;
} else if (isNumber) {
formatedValue = `${IfNull}num.tryParse(${jsonValue}.toString())`;
} else if (isString) {
formatedValue = `${IfNull}${jsonValue}${nullable}.toString()`;
} else if (isList) {
formatedValue = `${IfNull}${typeDef.type}.from(${jsonValue} ?? [])`;
} else if (isMap) {
formatedValue = `${IfNull}${typeDef.type}.from(${jsonValue} ?? {})`;
} else if (isBool) {
formatedValue = `${IfNull}${jsonValue}${nullable}.toString().contains("true")`;
} else {
formatedValue = `${IfNull}${jsonValue}${required} as ${typeDef.type}` + nullable + dafaultVal;
}
Expand All @@ -406,7 +417,7 @@ export function toJsonExpression(
if (typeDef.isDate) {
return toJsonClass(typeDef, privateField, input);
} else {
return `'${typeDef.jsonKey}': ${thisKey},`;
return `if (${thisKey} != null) '${typeDef.jsonKey}': ${thisKey},`;
}
} else {
return toJsonClass(typeDef, privateField, input);
Expand All @@ -424,7 +435,7 @@ const buildParseClass = (className: string, expression: string, typeDef: TypeDef
const suffix = input.jsonCodecs && _suffix.toLowerCase() === 'json' ? 'Map' : _suffix;
const name = pascalCase(className).replace(/_/g, '');
const bangOperator = jsonMapType(input).match('Object?') && !typeDef.isList ? '!' : '';
return `${name}.from${suffix}(${expression}${bangOperator} as ${jsonMapType(input)})`;
return `${name}.from${suffix}(${jsonMapType(input)}.from(${expression}${bangOperator}))`;
};

/**
Expand All @@ -435,8 +446,8 @@ const buildParseClass = (className: string, expression: string, typeDef: TypeDef
*/
const parseDateTime = (expression: string, withoutTypeCast: boolean = false, typeDef: TypeDefinition, input: Input): string => {
const bangOperator = input.nullSafety && input.avoidDynamicTypes && !typeDef.isList ? '!' : '';
const withArgumentType = `DateTime.parse(${expression}${bangOperator} as String)`;
const withoutArgumentType = `DateTime.parse(${expression})`;
const withArgumentType = `DateTime.tryParse(${expression}${bangOperator}.toString())`;
const withoutArgumentType = `DateTime.tryParse(${expression})`;
return withoutTypeCast ? withoutArgumentType : withArgumentType;
};

Expand Down Expand Up @@ -603,11 +614,10 @@ export class ClassDefinition {

private addType(typeDef: TypeDefinition, input: Input) {
const isDynamic = typeDef.type?.match('dynamic') && !typeDef.isList;

const isNumber = typeDef.type === 'double' || typeDef.type === 'int';
return isDynamic
? typeDef.type
: typeDef.type + questionMark(input, typeDef);

: (isNumber ? 'num' : typeDef.type) + questionMark(input, typeDef);
}

private fieldList(input: Input): string {
Expand Down
10 changes: 7 additions & 3 deletions src/syntax/empty-class.syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ export function emptyClass(className: string): string {
sb += printLine(`${className}();`, 1, 1);
sb += printLine(`factory ${className}.fromJson(Map<String, dynamic> json) {`, 2, 1);
sb += printLine('// TODO: implement fromJson', 1, 2);
sb += printLine(`throw UnimplementedError('${className}.fromJson($json) is not implemented');`, 1, 2);
sb += printLine(`return ${className}();`, 1, 2);
// sb += printLine(`throw UnimplementedError('${className}.fromJson($json) is not implemented');`, 1, 2);
sb += printLine('}', 1, 1);
sb += printLine('Map<String, dynamic> toJson() {', 2, 1);
sb += printLine('// TODO: implement toJson', 1, 2);
sb += printLine('throw UnimplementedError();', 1, 2);
sb += printLine('return {};', 1, 2);
// sb += printLine('throw UnimplementedError();', 1, 2);
sb += printLine('}', 1, 1);
sb += printLine('}', 1);
return sb;
}
}


Empty file added yarn-error.log
Empty file.
Loading