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

add $key for all object and support private class name #32

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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,6 @@ this package will read `.json` file, and generate `.dart` file, asign the `type
| asign list of type and import (can also be recursive) | {`...`:`"$[]value"`} | `{"addreses":"$[]address"}` | `List<Address> addreses;` | `import 'address.dart'` |
| import other library(input value can be array) | {`"@import"`:`...`} | `{"@import":"package:otherlibrary/otherlibrary.dart"}` | | `import 'package:otherlibrary/otherlibrary.dart'` |
| Datetime type | {`...`:`"@datetime"`} | `{"createdAt": "@datetime:2020-02-15T15:47:51.742Z"}` | `DateTime createdAt;` | |
| Enum type | {`...`:`"@enum:(folowed by enum separated by ',')"`} | `{"@import":"@enum:admin,app_user,normal"}` | `enum UserTypeEnum { Admin, AppUser, Normal }` |
| Enum type with values {`...`:`"@enum:(folowed by enum separated by ',')"`} | `{"@import":"@enum:admin(0),app_user(1),normal(2)"}` | `enum UserTypeEnum { Admin, AppUser, Normal }`| |
| Enum type | {`...`:`"@enum:(folowed by enum separated by ',')"`} | `{"@import":"@enum:admin,app_user,normal"}` | `enum UserTypeEnum { Admin, AppUser, Normal }` ||
| Enum type with values {`...`:`"@enum:(folowed by enum separated by ',')"`} | `{"@import":"@enum:admin(0),app_user(1),normal(2)"}` | `enum UserTypeEnum { Admin, AppUser, Normal }`| ||
| Custom class key (support private key, such as _InfoModel. By the way, not only in array, but also in normal object.) | {"$key": "CustomClassName"} | {"info":{"$key": "InfoModel", "name?":"Nick"}} | class InfoModel{final String? name; ...} ||
23 changes: 14 additions & 9 deletions lib/core/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ DartDeclaration defaultCommandCallback(DartDeclaration self, dynamic testSubject
}

if (value is Map) {
self.type = key.toTitleCase();
self.nestedClasses.add(JsonModel.fromMap(key, value as Map<String, dynamic>));
String customKey = key;
if (value['\$key'] != null && value['\$key'] != '') {
customKey = value['\$key'] as String;
}
value.remove('\$key');
self.type = customKey.toTitleCase();
self.nestedClasses.add(JsonModel.fromMap(customKey, value as Map<String, dynamic>));
return self;
}

Expand All @@ -49,16 +54,16 @@ DartDeclaration defaultCommandCallback(DartDeclaration self, dynamic testSubject
if (firstListValue is List) {
final nestedFirst = firstListValue.first;
if (nestedFirst is Map) {
final key = nestedFirst['\$key'];
final key = nestedFirst['\$key'] as String;
nestedFirst.remove('\$key');
self.type = 'List<List<$key>>';
self.nestedClasses.add(JsonModel.fromMap(key as String, nestedFirst as Map<String, dynamic>));
self.type = 'List<List<${key.toTitleCase()}>>';
self.nestedClasses.add(JsonModel.fromMap(key, nestedFirst as Map<String, dynamic>));
}
} else if (firstListValue is Map) {
final key = firstListValue['\$key'];
final key = firstListValue['\$key'] as String;
firstListValue.remove('\$key');
self.type = 'List<$key>';
self.nestedClasses.add(JsonModel.fromMap(key as String, firstListValue as Map<String, dynamic>));
self.type = 'List<${key.toTitleCase()}>';
self.nestedClasses.add(JsonModel.fromMap(key, firstListValue as Map<String, dynamic>));
} else {
final listValueType = firstListValue.runtimeType.toString();
self.type = 'List<$listValueType>';
Expand Down Expand Up @@ -158,7 +163,7 @@ final List<Command> keyComands = [
];

final List<Command> valueCommands = [
Command(
Command(
prefix: '#',
callback: (DartDeclaration self, dynamic testSubject, {required String key, dynamic value}) {
final subject = testSubject as String;
Expand Down
11 changes: 8 additions & 3 deletions lib/utils/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:json_to_model/core/model_template.dart';
extension StringExtension on String {
String toTitleCase() {
final firstWord = toCamelCase();
if (firstWord.startsWith(RegExp('_'))) {
return '_${firstWord.substring(1, 2).toUpperCase()}${firstWord.substring(2)}';
}
return '${firstWord.substring(0, 1).toUpperCase()}${firstWord.substring(1)}';
}

Expand Down Expand Up @@ -32,12 +35,14 @@ extension StringExtension on String {

List<String> getWords() {
List<String> value;

bool isPrivate = false;
if (startsWith(RegExp('_'))) {
isPrivate = true;
}
value = trim().split(RegExp(r'[_\W]'));
value = value.where((element) => element.isNotEmpty).toList();
value = value.expand((e) => e.split(RegExp('(?=[A-Z])'))).where((element) => element.isNotEmpty).toList();

return value;
return isPrivate ? ['_', ...value] : value;
}

bool isTitleCase() {
Expand Down