Skip to content

Commit

Permalink
flutter_supabase_macro_0.0.4 (#4)
Browse files Browse the repository at this point in the history
The primary key can only be excluded from the map conditionally (see CHANGELOG.md)
  • Loading branch information
ThomasDevApps authored Oct 15, 2024
1 parent b796127 commit 59f54d0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -62,15 +66,7 @@ class User {
// ...
}
```
It will generate a `toJsonSupabase()` method that returns a
`Map<String, dynamic>` 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<String, dynamic>`.

## 📖 Additional information

Expand Down
Binary file added assets/after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 33 additions & 14 deletions lib/src/mixins/to_json_supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,16 @@ 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(
(field) => addEntryForField(
field,
builder,
introspectionData,
isPrimaryKey: field.identifier.name == primaryKey,
),
),
),
Expand All @@ -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.'
]),
);
}
Expand Down Expand Up @@ -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<Code> addEntryForField(
FieldDeclaration field,
DefinitionBuilder builder,
_SharedIntrospectionData introspectionData,
) async {
_SharedIntrospectionData introspectionData, {
bool isPrimaryKey = false,
}) async {
final parts = <Object>[];
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,
Expand All @@ -264,7 +282,8 @@ mixin _ToJsonSupabase on _Shared {
),
';\n ',
]);
if (doNullCheck) {
// Close the condition
if (needCondition) {
parts.add('}\n ');
}
return RawCode.fromParts(parts);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_supabase_macro
description: "A new Flutter project."
version: 0.0.3
version: 0.0.4
homepage:

environment:
Expand Down
14 changes: 12 additions & 2 deletions test/flutter_supabase_macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 59f54d0

Please sign in to comment.