diff --git a/CHANGELOG.md b/CHANGELOG.md index 34c35c8..2281156 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,9 @@ Initial release : - Creation of a `toJsonSupabase` which exclude the `primaryKey` from the `Map` + +## 0.0.4 + +Only exclude `primaryKey` from the Map if : +- Can't be nullable then check that `!= null` +- The type is `String`, then check that the value `isNotEmpty` \ No newline at end of file diff --git a/README.md b/README.md index 5c7cfe1..5b576d5 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ and the Flutter guide for Package greatly inspired by `JsonCodable` (from Dart), makes it easy to create a JSON format of a template for Supabase. +| Before | After | +|----------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------| +| ![before](https://raw.githubusercontent.com/ThomasDevApps/flutter_supabase_macro/flutter_supabase_macro_0.0.4/assets/before.png) | ![after](https://raw.githubusercontent.com/ThomasDevApps/flutter_supabase_macro/flutter_supabase_macro_0.0.4/assets/after.png) | + - [What is a macro](#-what-is-a-macro) - [Getting started](#-getting-started) - [How it works](#-how-it-works) @@ -62,15 +66,7 @@ class User { // ... } ``` -It will generate a `toJsonSupabase()` method that returns a -`Map` that does not contain the `primaryKey` -(`id` in this case) : - -```dart -final user = User(id: 'the-id', name: 'Toto', age: 22); -final json = user.toJsonSupabase(); -print(json); // {'name': 'Toto', 'age': 22} -``` +It will generate a `toJsonSupabase()` method that returns a `Map`. ## 📖 Additional information diff --git a/assets/after.png b/assets/after.png new file mode 100644 index 0000000..97ceb90 Binary files /dev/null and b/assets/after.png differ diff --git a/assets/before.png b/assets/before.png new file mode 100644 index 0000000..4f578b3 Binary files /dev/null and b/assets/before.png differ diff --git a/lib/src/mixins/to_json_supabase.dart b/lib/src/mixins/to_json_supabase.dart index fe147a2..b9bddf2 100644 --- a/lib/src/mixins/to_json_supabase.dart +++ b/lib/src/mixins/to_json_supabase.dart @@ -75,10 +75,8 @@ mixin _ToJsonSupabase on _Shared { superclassHasToJson: superclassHasToJson); // Get all fields - final fields = introspectionData.fields.where((f) { - bool canBeAdd = f.identifier.name != primaryKey; - return canBeAdd; - }); + final fields = introspectionData.fields; + // Add entry for each fields parts.addAll( await Future.wait( fields.map( @@ -86,6 +84,7 @@ mixin _ToJsonSupabase on _Shared { field, builder, introspectionData, + isPrimaryKey: field.identifier.name == primaryKey, ), ), ), @@ -97,7 +96,8 @@ mixin _ToJsonSupabase on _Shared { docComments: CommentCode.fromParts([ ' /// Map representing the model in json format for Supabase.\n', ' ///\n', - ' /// The `primaryKey` is exclude from the map.' + ' /// The primary key [${fields.first.identifier.name}]', + ' is exclude from the map if empty.' ]), ); } @@ -235,20 +235,38 @@ mixin _ToJsonSupabase on _Shared { /// json[r'"age"'] = age!; // ! present only if the field is nullable by default. /// } // Only if the field is nullable. /// ``` + /// + /// If `age` is a `String` and [isPrimaryKey] is true, then `age.isNotEmpty` + /// will be added in the definition of the condition. Future addEntryForField( FieldDeclaration field, DefinitionBuilder builder, - _SharedIntrospectionData introspectionData, - ) async { + _SharedIntrospectionData introspectionData, { + bool isPrimaryKey = false, + }) async { final parts = []; final doNullCheck = field.type.isNullable; - if (doNullCheck) { - parts.addAll([ - 'if (', - field.identifier, - ' != null) {\n ', - ]); + final needCondition = doNullCheck || isPrimaryKey; + // Begin the definition of the condition + if (needCondition) { + parts.addAll(['if (']); + } + // Check that the field is not null + if (doNullCheck) parts.addAll([field.identifier, ' != null']); + // Check that the field is not empty (if String) + if (isPrimaryKey) { + final type = _checkNamedType(field.type, builder); + if (type != null) { + parts.addAll([ + if (doNullCheck) ' && ', + if (type.identifier.name == 'String') + '${field.identifier.name}.isNotEmpty', + ]); + } } + // Close definition of the condition and open it + if (needCondition) parts.add(') {\n '); + // Add the field in the json parts.addAll([ "json[r'", field.identifier.name, @@ -264,7 +282,8 @@ mixin _ToJsonSupabase on _Shared { ), ';\n ', ]); - if (doNullCheck) { + // Close the condition + if (needCondition) { parts.add('}\n '); } return RawCode.fromParts(parts); diff --git a/pubspec.yaml b/pubspec.yaml index 68fcc93..6e19d0e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_supabase_macro description: "A new Flutter project." -version: 0.0.3 +version: 0.0.4 homepage: environment: diff --git a/test/flutter_supabase_macro_test.dart b/test/flutter_supabase_macro_test.dart index b00c3ec..b558d7e 100644 --- a/test/flutter_supabase_macro_test.dart +++ b/test/flutter_supabase_macro_test.dart @@ -11,12 +11,22 @@ class User { } void main() { - test('Test that id is missing from the json', () { - final user = User(id: 'id', name: 'Toto', age: 22); + test('Test that id is missing from the json because is empty', () { + final user = User(id: '', name: 'Toto', age: 22); final json = user.toJsonSupabase(); expect(json.keys.length, 2); expect(json['name'], 'Toto'); expect(json['age'], 22); }); + + test('Test that id is NOT missing from the json because is NOT empty', () { + final user = User(id: 'id-123', name: 'Toto', age: 22); + final json = user.toJsonSupabase(); + + expect(json.keys.length, 3); + expect(json['id'], 'id-123'); + expect(json['name'], 'Toto'); + expect(json['age'], 22); + }); }