From 56e1fc62d002fc2f9a06e5a82a5c43f0f5e0561a Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 6 Feb 2018 22:24:44 -0500 Subject: [PATCH] Added `autoSnakeCaseNames` to `MapService` --- .idea/libraries/Dart_Packages.xml | 8 -- .idea/workspace.xml | 154 ++++++++++++++++-------------- CHANGELOG.md | 1 + lib/src/http/map_service.dart | 27 ++++-- pubspec.yaml | 1 - 5 files changed, 106 insertions(+), 85 deletions(-) diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 576ef34c..61931626 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -317,13 +317,6 @@ - - - - - - @@ -498,7 +491,6 @@ - diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 492c51a5..12a634ee 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,11 +1,11 @@ - + - + @@ -49,27 +49,42 @@ - - + + + + + + + + + + + + + + - - - + + + + + - - - - - - - - + + + + + + + + + @@ -132,19 +147,6 @@ - - - - - - - - - - - - - @@ -156,8 +158,6 @@ - sendRes - handleRequ sendRespon inject( sendRe @@ -186,6 +186,8 @@ cache errorHan addStre + ((cre)|(upd))atedAt + '((cre)|(upd))atedAt' _isClosed @@ -206,6 +208,7 @@ cookie req.path req.path` + autoSnakeCaseNames == false ? $0 : '$1ated_at' C:\Users\thosa\Source\Angel\framework\lib @@ -278,10 +281,10 @@ @@ -370,7 +373,7 @@ - + - + @@ -1039,7 +1049,7 @@ - @@ -1074,7 +1084,7 @@ - + @@ -1097,7 +1107,7 @@ - + @@ -1281,16 +1292,6 @@ - - - - - - - - - - @@ -1460,14 +1461,6 @@ - - - - - - - - @@ -1478,16 +1471,6 @@ - - - - - - - - - - @@ -1496,11 +1479,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 4377d9a0..14fdff46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 1.1.1 * `ResponseContext` no longer automatically closes if `serializer` returns an empty string. +* Added `autoSnakeCaseNames` to `MapService`. # 1.1.0+3 * Modified `ResponseContext#isOpen` to also return `false` if streaming is being used. diff --git a/lib/src/http/map_service.dart b/lib/src/http/map_service.dart index e07c87b1..ce2c409a 100644 --- a/lib/src/http/map_service.dart +++ b/lib/src/http/map_service.dart @@ -15,14 +15,24 @@ class MapService extends Service { /// If set to `true` (default), then the service will manage an `id` string and `createdAt` and `updatedAt` fields. final bool autoIdAndDateFields; + /// If set to `true` (default), then the keys `created_at` and `updated_at` will automatically be snake_cased. + final bool autoSnakeCaseNames; + final List> items = []; MapService( {this.allowRemoveAll: false, this.allowQuery: true, - this.autoIdAndDateFields: true}) + this.autoIdAndDateFields: true, + this.autoSnakeCaseNames: true}) : super(); + String get createdAtKey => + autoSnakeCaseNames == false ? 'createdAt' : 'created_at'; + + String get updatedAtKey => + autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'; + bool Function(Map) _matchesId(id) { return (Map item) { if (item['id'] == null) @@ -72,8 +82,8 @@ class MapService extends Service { if (autoIdAndDateFields == true) { result ..['id'] = items.length.toString() - ..['createdAt'] = now - ..['updatedAt'] = now; + ..[autoSnakeCaseNames == false ? 'createdAt' : 'created_at'] = now + ..[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] = now; } items.add(result); return result; @@ -90,7 +100,10 @@ class MapService extends Service { var item = await read(id); var result = item..addAll(data); - if (autoIdAndDateFields == true) result..['updatedAt'] = new DateTime.now(); + if (autoIdAndDateFields == true) + result + ..[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] = + new DateTime.now(); return result; } @@ -112,8 +125,10 @@ class MapService extends Service { if (autoIdAndDateFields == true) { result ..['id'] = id?.toString() - ..['createdAt'] = old['createdAt'] - ..['updatedAt'] = new DateTime.now(); + ..[autoSnakeCaseNames == false ? 'createdAt' : 'created_at'] = + old[autoSnakeCaseNames == false ? 'createdAt' : 'created_at'] + ..[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] = + new DateTime.now(); } items.add(result); return result; diff --git a/pubspec.yaml b/pubspec.yaml index 7eca6daf..6047fced 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,7 +20,6 @@ dependencies: mime: ^0.9.3 pool: ^1.0.0 random_string: ^0.0.1 - recase: ^1.0.0 tuple: ^1.0.0 dev_dependencies: mock_request: ^1.0.0