diff --git a/README.md b/README.md index cd06f4a..72e3e73 100644 --- a/README.md +++ b/README.md @@ -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
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; ...} || diff --git a/lib/core/command.dart b/lib/core/command.dart index 0855b46..2dd4da2 100644 --- a/lib/core/command.dart +++ b/lib/core/command.dart @@ -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 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)); return self; } @@ -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>'; - self.nestedClasses.add(JsonModel.fromMap(key as String, nestedFirst as Map)); + self.type = 'List>'; + self.nestedClasses.add(JsonModel.fromMap(key, nestedFirst as Map)); } } 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)); + self.type = 'List<${key.toTitleCase()}>'; + self.nestedClasses.add(JsonModel.fromMap(key, firstListValue as Map)); } else { final listValueType = firstListValue.runtimeType.toString(); self.type = 'List<$listValueType>'; @@ -158,7 +163,7 @@ final List keyComands = [ ]; final List valueCommands = [ - Command( + Command( prefix: '#', callback: (DartDeclaration self, dynamic testSubject, {required String key, dynamic value}) { final subject = testSubject as String; diff --git a/lib/utils/extensions.dart b/lib/utils/extensions.dart index 85345d0..ef3e332 100644 --- a/lib/utils/extensions.dart +++ b/lib/utils/extensions.dart @@ -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)}'; } @@ -32,12 +35,14 @@ extension StringExtension on String { List getWords() { List 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() {