Skip to content

Commit 33cf81f

Browse files
committed
fix: 👽️ due to null-safe changed
1 parent 5741d67 commit 33cf81f

File tree

4 files changed

+120
-19
lines changed

4 files changed

+120
-19
lines changed

.github/publish.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
on:
2+
push:
3+
tags:
4+
- "*"
5+
6+
name: Deploy Extension
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: 12
15+
- run: npm ci
16+
- name: Publish to Visual Studio Marketplace
17+
uses: HaaLeo/publish-vscode-extension@v0
18+
with:
19+
pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
20+
registryUrl: https://marketplace.visualstudio.com

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 0.5.3
4+
1. Add a config for `ignore_for_file`
5+
2. implement List<enum>
6+
3. do cast convert for fromMap
7+
4. add a ci template
8+
39
## 0.5.2
410
1. Due to the null-aware operator '?.' is unnecessary. replacing the operator '?.' with '.'
511
2. add const to constructor if usesEquatable

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Dart Data Class Generator",
44
"description": "Create dart data classes easily, fast and without writing boilerplate or running code generation.",
55
"publisher": "BendixMa",
6-
"version": "0.5.2",
6+
"version": "0.5.3",
77
"engines": {
88
"vscode": "^1.37.0"
99
},
@@ -92,6 +92,11 @@
9292
"default": false,
9393
"description": "If true, asks, when overriding a class (running the command on an existing class), for every single function/constructor that needs to be changed whether the generator should override the function or not. This allows you to preserve custom changes you made to the function/constructor that would be otherwise overwritten by the generator."
9494
},
95+
"dart_data_class_generator.ignoreComment.enabled": {
96+
"type": "string",
97+
"default": "",
98+
"description": "Add a analyzer comment for file"
99+
},
95100
"dart_data_class_generator.json.seperate": {
96101
"type": "string",
97102
"enum": [

src/extension.js

+88-18
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,8 @@ class DataClassGenerator {
10911091
method += ` '${p.jsonName}': `;
10921092

10931093
if (p.isEnum) {
1094-
method += `${p.name}.index,\n`;
1094+
if (p.isCollection) method += `${p.name}.map((x) => x.index).toList(),\n`
1095+
else method += `${p.name}.index,\n`;
10951096
} else if (p.isCollection) {
10961097
if (p.isMap || p.listType.isPrimitive) {
10971098
const mapFlag = p.isSet ? (p.isNullable ? '?' : '') + '.toList()' : '';
@@ -1123,32 +1124,52 @@ class DataClassGenerator {
11231124
* @param {ClassField} prop
11241125
*/
11251126
function customTypeMapping(prop, value = null) {
1127+
const materialConvertValue = prop.isCollection ? "" :" as int";
11261128
prop = prop.isCollection ? prop.listType : prop;
1127-
const addDefault = withDefaultValues && prop.rawType != 'dynamic';
1128-
value = value == null ? `${leftOfValue}map['` + prop.jsonName + "']" : value;
1129+
const isAddDefault = withDefaultValues && prop.rawType != 'dynamic'&& !prop.isNullable && prop.isPrimitive;
1130+
const addLeftDefault = isAddDefault ? leftOfValue : '';
1131+
const addRightDefault = isAddDefault ? rightOfValue : '';
1132+
value = value == null ? `${addLeftDefault}map['` + prop.jsonName + "']" : value;
11291133

11301134
switch (prop.type) {
11311135
case 'DateTime':
1132-
return `DateTime.fromMillisecondsSinceEpoch(${value})`;
1136+
value=withDefaultValues ? `${leftOfValue}${value}??0${rightOfValue}` : value;
1137+
return `DateTime.fromMillisecondsSinceEpoch(${value}${materialConvertValue})`;
11331138
case 'Color':
1134-
return `Color(${value})`;
1139+
value=withDefaultValues ? `${leftOfValue}${value}??0${rightOfValue}` : value;
1140+
return `Color(${value}${materialConvertValue})`;
11351141
case 'IconData':
1136-
return `IconData(${value}, fontFamily: 'MaterialIcons')`
1142+
value=withDefaultValues ? `${leftOfValue}${value}??0${rightOfValue}` : value;
1143+
return `IconData(${value}${materialConvertValue}, fontFamily: 'MaterialIcons')`
11371144
default:
1138-
return `${!prop.isPrimitive ? prop.type + '.fromMap(' : ''}${value}${!prop.isPrimitive ? ')' : ''}${fromJSON ? (prop.isDouble ? '.toDouble()' : prop.isInt ? '.toInt()' : '') : ''}${addDefault && !prop.isNullable ? ` ?? ${prop.defValue}` : ''}`;
1145+
return `${
1146+
!prop.isPrimitive ? prop.type + ".fromMap(" : ""
1147+
}${value}${!prop.isPrimitive ? " as Map<String,dynamic>)" : ""}${
1148+
fromJSON
1149+
? prop.isDouble
1150+
? ".toDouble()"
1151+
: prop.isInt
1152+
? ".toInt()"
1153+
: ""
1154+
: ""
1155+
}${
1156+
isAddDefault
1157+
? ` ?? ${prop.defValue}${addRightDefault}`
1158+
: ""
1159+
}`;
11391160
}
11401161
}
11411162

11421163
let method = `factory ${clazz.name}.fromMap(Map<String, dynamic> map) {\n`;
11431164
method += ' return ' + clazz.type + '(\n';
1144-
1165+
11451166
for (let p of props) {
11461167
method += ` ${clazz.hasNamedConstructor ? `${p.name}: ` : ''}`;
11471168

1148-
const value = `${leftOfValue}map['${p.jsonName}']`;
1169+
const value = `map['${p.jsonName}']`;
1170+
1171+
11491172

1150-
1151-
11521173

11531174
// Add nullable check before serialization
11541175
if (p.isNullable) {
@@ -1157,27 +1178,44 @@ class DataClassGenerator {
11571178

11581179
// serialization
11591180
if (p.isEnum) {
1160-
const defaultValue = withDefaultValues ? ' ?? 0' : '';
1161-
method += `${p.rawType}.values[${value}${defaultValue}]`;
1181+
// List<E>
1182+
if (p.isCollection) {
1183+
const defaultValue = withDefaultValues ? ' ?? <int>[]' : '';
1184+
method += `${p.type}.from((${leftOfValue}${value}${defaultValue}${rightOfValue} as List<int>).map<${p.listType.rawType}>((x) => ${p.listType.rawType}.values[x]),)`
1185+
} else {
1186+
const defaultValue = withDefaultValues ? ' ?? 0' : '';
1187+
method += `${p.rawType}.values[${leftOfValue}${value}${defaultValue}${rightOfValue} as int]`;
1188+
}
1189+
11621190
} else if (p.isCollection) {
1163-
const defaultValue = withDefaultValues && !p.isNullable ? ` ?? const ${p.isList ? '[]' : '{}'}` : '';
1191+
const defaultValue =
1192+
withDefaultValues && !p.isNullable && p.isPrimitive
1193+
? ` ?? const <${p.listType.rawType}>${
1194+
p.isList ? "[]" : "{}"
1195+
})`
1196+
: "";
11641197

11651198
method += `${p.type}.from(`;
1199+
/// List<String>.from(map['allowed'] ?? const <String>[] as List<String>),
11661200
if (p.isPrimitive) {
1167-
method += `${value}${defaultValue})`;
1201+
method += `(${value}${defaultValue} as ${p.type})`;
11681202
} else {
1169-
method += `${value}.map((x) => ${customTypeMapping(p, 'x')})${defaultValue})`;
1203+
method += `(${value} as List<int>).map<${p.listType.rawType}>((x) => ${customTypeMapping(p, 'x')},),${defaultValue})`;
11701204
}
1205+
/// (map['name'] ?? '') as String
11711206
} else {
1172-
method += customTypeMapping(p);
1207+
if (p.isPrimitive)
1208+
method += customTypeMapping(p)+` as ${p.type}`;
1209+
else
1210+
method += customTypeMapping(p);
11731211
}
11741212

11751213
// end nullable check if field is nullable
11761214
if (p.isNullable) {
11771215
method += " : null";
11781216
}
11791217

1180-
method += `${rightOfValue} as ${p.type},\n`;
1218+
method += `,\n`;
11811219

11821220
const isLast = p.name == props[props.length - 1].name;
11831221
if (isLast) method += ' );\n';
@@ -2136,6 +2174,38 @@ function getReplaceEdit(values, imports = null, showLogs = false) {
21362174
const uri = getDoc().uri;
21372175

21382176
const noChanges = [];
2177+
2178+
let ignoreComment = readSetting("ignoreComment.enabled");
2179+
if (ignoreComment == '') ignoreComment = 'public_member_api_docs, sort_constructors_first';
2180+
const lines = [];
2181+
const ignores = [];
2182+
2183+
const text = getDocText();
2184+
text.split("\n").forEach(function (line) {
2185+
if (line.includes("// ignore_for_file:")) lines.push(line);
2186+
});
2187+
let isIncluding;
2188+
ignoreComment.split(",").forEach((ignore) => {
2189+
ignore.trim();
2190+
2191+
isIncluding = false;
2192+
lines.forEach((line) => {
2193+
if (line.includes(ignore)) {
2194+
isIncluding = true;
2195+
}
2196+
});
2197+
for (let line in lines) {
2198+
}
2199+
if (!isIncluding) ignores.push(ignore);
2200+
});
2201+
if (ignores.length > 0)
2202+
edit.insert(
2203+
uri,
2204+
new vscode.Position(0, 0),
2205+
`// ignore_for_file: ${ignores.join(",")}\n`
2206+
);
2207+
2208+
21392209
for (var i = clazzes.length - 1; i >= 0; i--) {
21402210
const clazz = clazzes[i];
21412211

0 commit comments

Comments
 (0)