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

json文件带下划线情况下生成大驼峰类名,key带下划线或者空格时生成小驼峰成员名 #53

Open
wants to merge 1 commit 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
32 changes: 23 additions & 9 deletions bin/json_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool generateModelClass(
//generated class name
String? className = meta['className'] as String?;
if (className == null || className.isEmpty) {
className = fileName[0].toUpperCase() + fileName.substring(1);
className = convName(fileName);
}

//set ignore
Expand Down Expand Up @@ -129,7 +129,7 @@ bool generateModelClass(
if (key.startsWith("_")) return;
if (key.startsWith("@")) {
if (comments[v] != null) {
_writeComments(comments[v],fields);
_writeComments(comments[v], fields);
}
fields.write(key);
fields.write(" ");
Expand All @@ -145,17 +145,22 @@ bool generateModelClass(
!notNull && (optionalField || _nullable);

if (comments[key] != null) {
_writeComments(comments[key],fields);
_writeComments(comments[key], fields);
}

if (key.contains(RegExp(r'_|\s+'))) {
fields.write("@JsonKey(name: '$key') ");
}

if (!shouldAppendOptionalFlag) {
fields.write('late ');
}
fields.write(getDataType(v, importSet, fileName, tag));
if (shouldAppendOptionalFlag) {
fields.write('?');
}
fields.write(" ");
fields.write(key);
fields.write(' ${convName(key, false)}');

//new line
fields.writeln(";");
}
Expand Down Expand Up @@ -192,8 +197,8 @@ bool generateModelClass(
return indexFile.isNotEmpty;
}

_writeComments(dynamic comments,StringBuffer sb){
final arr='$comments'.replaceAll('\r', '').split('\n');
_writeComments(dynamic comments, StringBuffer sb) {
final arr = '$comments'.replaceAll('\r', '').split('\n');
arr.forEach((element) {
sb.writeln('// $element');
sb.write(' ');
Expand All @@ -215,6 +220,15 @@ bool isBuiltInType(String type) {
return ['int', 'num', 'string', 'double', 'map', 'list'].contains(type);
}

String convName(String str, [bool upper = true]) {
return (upper ? str[0].toUpperCase() : str[0].toLowerCase()) +
str
.split(RegExp(r'_|\s+'))
.map((e) => e[0].toUpperCase() + e.substring(1))
.join()
.substring(1);
}

String getDataType(v, Set<String> set, String current, String tag) {
current = current.toLowerCase();
if (v is bool) {
Expand All @@ -232,13 +246,13 @@ String getDataType(v, Set<String> set, String current, String tag) {
if (type.toLowerCase() != current && !isBuiltInType(type)) {
set.add('import "$type.dart"');
}
return "List<${changeFirstChar(type)}>";
return "List<${convName(type)}>";
} else if (v.startsWith(tag)) {
final fileName = changeFirstChar(v.substring(1), false);
if (fileName.toLowerCase() != current) {
set.add('import "$fileName.dart"');
}
return changeFirstChar(fileName);
return convName(fileName);
} else if (v.startsWith("@")) {
return v;
}
Expand Down
5 changes: 3 additions & 2 deletions example/jsons/card.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"no":"28838388383756",
"name":"xx bank"
"phone_no": "28838388383756",
"name": "xx bank",
"show_user": "[]$user"
}
42 changes: 28 additions & 14 deletions lib/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ void run(List<String> args) {
}

bool generateModelClass(
String srcDir,
String distDir,
String tag, {
required bool nullable,
}) {
String srcDir,
String distDir,
String tag, {
required bool nullable,
}) {
const metaTag = "@meta";
if (srcDir.endsWith("/")) srcDir = srcDir.substring(0, srcDir.length - 1);
if (distDir.endsWith("/")) distDir = distDir.substring(0, distDir.length - 1);
Expand Down Expand Up @@ -101,7 +101,7 @@ bool generateModelClass(
//generated class name
String? className = meta['className'] as String?;
if (className == null || className.isEmpty) {
className = fileName[0].toUpperCase() + fileName.substring(1);
className = convName(fileName);
}

//set ignore
Expand Down Expand Up @@ -129,7 +129,7 @@ bool generateModelClass(
if (key.startsWith("_")) return;
if (key.startsWith("@")) {
if (comments[v] != null) {
_writeComments(comments[v],fields);
_writeComments(comments[v], fields);
}
fields.write(key);
fields.write(" ");
Expand All @@ -145,17 +145,22 @@ bool generateModelClass(
!notNull && (optionalField || _nullable);

if (comments[key] != null) {
_writeComments(comments[key],fields);
_writeComments(comments[key], fields);
}

if (key.contains(RegExp(r'_|\s+'))) {
fields.write("@JsonKey(name: '$key') ");
}

if (!shouldAppendOptionalFlag) {
fields.write('late ');
}
fields.write(getDataType(v, importSet, fileName, tag));
if (shouldAppendOptionalFlag) {
fields.write('?');
}
fields.write(" ");
fields.write(key);
fields.write(' ${convName(key, false)}');

//new line
fields.writeln(";");
}
Expand Down Expand Up @@ -192,8 +197,8 @@ bool generateModelClass(
return indexFile.isNotEmpty;
}

_writeComments(dynamic comments,StringBuffer sb){
final arr='$comments'.replaceAll('\r', '').split('\n');
_writeComments(dynamic comments, StringBuffer sb) {
final arr = '$comments'.replaceAll('\r', '').split('\n');
arr.forEach((element) {
sb.writeln('// $element');
sb.write(' ');
Expand All @@ -215,6 +220,15 @@ bool isBuiltInType(String type) {
return ['int', 'num', 'string', 'double', 'map', 'list'].contains(type);
}

String convName(String str, [bool upper = true]) {
return (upper ? str[0].toUpperCase() : str[0].toLowerCase()) +
str
.split(RegExp(r'_|\s+'))
.map((e) => e[0].toUpperCase() + e.substring(1))
.join()
.substring(1);
}

String getDataType(v, Set<String> set, String current, String tag) {
current = current.toLowerCase();
if (v is bool) {
Expand All @@ -232,13 +246,13 @@ String getDataType(v, Set<String> set, String current, String tag) {
if (type.toLowerCase() != current && !isBuiltInType(type)) {
set.add('import "$type.dart"');
}
return "List<${changeFirstChar(type)}>";
return "List<${convName(type)}>";
} else if (v.startsWith(tag)) {
final fileName = changeFirstChar(v.substring(1), false);
if (fileName.toLowerCase() != current) {
set.add('import "$fileName.dart"');
}
return changeFirstChar(fileName);
return convName(fileName);
} else if (v.startsWith("@")) {
return v;
}
Expand Down