Skip to content

Commit

Permalink
flutter_supabase_macro_0.0.1 (#1)
Browse files Browse the repository at this point in the history
First commit of `FlutterSupabaseMacro` :

• Exclude `primaryKey` from `toJsonSupabase`
  • Loading branch information
ThomasDevApps authored Oct 11, 2024
1 parent 896d8b9 commit 5aa4a06
Show file tree
Hide file tree
Showing 10 changed files with 723 additions and 136 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Main CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test-flutter:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2.16.0
with:
channel: 'beta'

- name: Install dependencies
run: flutter pub get

- name: Analyze project source
run: flutter analyze

- name: Run tests
run: flutter test --enable-experiment=macros
56 changes: 41 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,56 @@ For general information about developing packages, see the Dart guide for
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/to/develop-packages).
-->
# Flutter Supabase Macro

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
Package greatly inspired by `JsonCodable` (from Dart), makes it easy to create a JSON format of a template for Supabase.

## Features
## 🚀 Getting started

TODO: List what your package can do. Maybe include images, gifs, or videos.
Because the macros are still under development, you need to follow these instructions to be able to test this package : https://dart.dev/language/macros#set-up-the-experiment

## Getting started
Then add in your `pubspec.yaml` :

TODO: List prerequisites and provide or point to information on how to
start using the package.
```yaml
flutter_supabase_macro:
git:
url: https://github.com/ThomasDevApps/flutter_supabase_macro.git
```
## 🔎 How it works
Let's imagine the `User` class :

## Usage
```dart
class User {
final String id;
final String name;
final int age;
const User({required this.id, required this.name, required this.age});
}
```
Let's assume that in your Supabase `users` table, the primary key is named `id`.

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
All you need to do is add the following :

```dart
const like = 'sample';
@FlutterSupabaseMacro(primaryKey: 'id') // Add this (primaryKey is 'id' by default)
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: 'id', name: 'Toto', age: 22);
final json = user.toJsonSupabase();
print(json); // {}
```

## 📖 Additional information

## Additional information
This package is still undergoing experimentation, and is in no way intended for use in production apps.

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
Not officially affiliated with Supabase.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ include: package:flutter_lints/flutter.yaml
# https://dart.dev/guides/language/analysis-options

analyzer:
errors:
non_part_of_directive_in_part: ignore
enable-experiment:
- macros
2 changes: 1 addition & 1 deletion lib/flutter_supabase_macro.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library;

export 'src/annotations.dart';
export 'src/supabase_macro.dart';
114 changes: 0 additions & 114 deletions lib/src/annotations.dart

This file was deleted.

71 changes: 71 additions & 0 deletions lib/src/mixins/shared.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ignore_for_file: deprecated_member_use, unintended_html_in_doc_comment

part of '../supabase_macro.dart';

/// Shared logic for all macros which run in the declarations phase.
mixin _Shared {
/// Returns [type] as a [NamedTypeAnnotation] if it is one, otherwise returns
/// `null` and emits relevant error diagnostics.
NamedTypeAnnotation? _checkNamedType(TypeAnnotation type, Builder builder) {
if (type is NamedTypeAnnotation) return type;
if (type is OmittedTypeAnnotation) {
builder.report(
_createDiagnostic(
type,
message:
'Only fields with explicit types are allowed on serializable '
'classes, please add a type.',
),
);
} else {
builder.report(
_createDiagnostic(
type,
message: 'Only fields with named types are allowed on serializable '
'classes.',
),
);
}
return null;
}

/// Create a [Diagnostic] according with [type] and [message].
Diagnostic _createDiagnostic(TypeAnnotation type, {required String message}) {
return Diagnostic(
DiagnosticMessage(message, target: type.asDiagnosticTarget),
Severity.error,
);
}

/// Does some basic validation on [clazz], and shared setup logic.
///
/// Returns a code representation of the [Map<String, dynamic>] class.
Future<NamedTypeAnnotationCode> _setup(
ClassDeclaration clazz,
MemberDeclarationBuilder builder,
) async {
if (clazz.typeParameters.isNotEmpty) {
throw DiagnosticException(
Diagnostic(
DiagnosticMessage(
'Cannot be applied to classes with generic type parameters',
),
Severity.error,
),
);
}

final (map, string, dynamic) = await (
builder.resolveIdentifier(_dartCore, 'Map'),
builder.resolveIdentifier(_dartCore, 'String'),
builder.resolveIdentifier(_dartCore, 'dynamic'),
).wait;
return NamedTypeAnnotationCode(
name: map,
typeArguments: [
NamedTypeAnnotationCode(name: string),
NamedTypeAnnotationCode(name: dynamic),
],
);
}
}
Loading

0 comments on commit 5aa4a06

Please sign in to comment.