Skip to content

Commit

Permalink
resolve supabase_generators to shared spec
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor committed Aug 27, 2024
1 parent 11e3fa0 commit 5e9b15f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 63 deletions.
1 change: 1 addition & 0 deletions packages/brick_supabase/lib/brick_supabase.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'package:brick_core/field_rename.dart';
export 'package:brick_supabase/src/annotations/supabase.dart';
export 'package:brick_supabase/src/annotations/supabase_serializable.dart';
export 'package:brick_supabase/src/runtime_supabase_column_definition.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
/// Values for the automatic field renaming behavior for [SupabaseSerializable].
///
/// Heavily borrowed/inspired by [JsonSerializable](https://github.com/dart-lang/json_serializable/blob/a581e5cc9ee25bf4ad61e8f825a311289ade905c/json_serializable/lib/src/json_key_utils.dart#L164-L179)
enum FieldRename {
/// Leave fields unchanged
none,

/// Encodes field name from `snakeCase` to `snake_case`.
snake,

/// Encodes field name from `kebabCase` to `kebab-case`.
kebab,

/// Capitalizes first letter of field name
pascal,
}
import 'package:brick_core/field_rename.dart';

/// An annotation used to specify a class to generate Supabase code for.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/brick_supabase/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_supabase
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 0.1.1+1
version: 0.1.1+2

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
32 changes: 13 additions & 19 deletions packages/brick_supabase_generators/lib/src/supabase_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,12 @@ import 'package:brick_build/generators.dart';
import 'package:brick_supabase/brick_supabase.dart';

/// Find `@Supabase` given a field
class SupabaseAnnotationFinder extends AnnotationFinder<Supabase> {
class SupabaseAnnotationFinder extends AnnotationFinder<Supabase>
with AnnotationFinderWithFieldRename<Supabase> {
final SupabaseSerializable? config;

SupabaseAnnotationFinder([this.config]);

/// Change serialization key based on the configuration.
/// `name` defined with a field annotation (`@Graphql`) take precedence.
String _renameField(String name) {
final renameTo = config?.fieldRename ?? SupabaseSerializable.defaults.fieldRename;
switch (renameTo) {
case FieldRename.none:
return name;
case FieldRename.snake:
return StringHelpers.snakeCase(name);
case FieldRename.kebab:
return StringHelpers.kebabCase(name);
case FieldRename.pascal:
return StringHelpers.pascalCase(name);
}
}

@override
Supabase from(element) {
final obj = objectForField(element);
Expand All @@ -35,7 +20,11 @@ class SupabaseAnnotationFinder extends AnnotationFinder<Supabase> {
ignore: Supabase.defaults.ignore,
ignoreFrom: Supabase.defaults.ignoreFrom,
ignoreTo: Supabase.defaults.ignoreTo,
name: _renameField(element.name),
name: renameField(
element.name,
config?.fieldRename,
SupabaseSerializable.defaults.fieldRename,
),
nullable: Supabase.defaults.nullable,
enumAsString: Supabase.defaults.enumAsString,
);
Expand All @@ -49,7 +38,12 @@ class SupabaseAnnotationFinder extends AnnotationFinder<Supabase> {
ignore: obj.getField('ignore')!.toBoolValue() ?? Supabase.defaults.ignore,
ignoreFrom: obj.getField('ignoreFrom')!.toBoolValue() ?? Supabase.defaults.ignoreFrom,
ignoreTo: obj.getField('ignoreTo')!.toBoolValue() ?? Supabase.defaults.ignoreTo,
name: obj.getField('name')?.toStringValue() ?? _renameField(element.name),
name: obj.getField('name')?.toStringValue() ??
renameField(
element.name,
config?.fieldRename,
SupabaseSerializable.defaults.fieldRename,
),
nullable: obj.getField('nullable')!.toBoolValue() ?? Supabase.defaults.nullable,
toGenerator: obj.getField('toGenerator')!.toStringValue(),
unique: obj.getField('unique')!.toBoolValue() ?? Supabase.defaults.unique,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:brick_json_generators/json_serialize.dart';
import 'package:brick_supabase/brick_supabase.dart';
import 'package:brick_supabase_generators/src/supabase_fields.dart';
Expand Down Expand Up @@ -34,7 +33,7 @@ class SupabaseSerialize extends SupabaseSerdesGenerator
if (annotation.foreignKey != null) {
definition += "associationForeignKey: '${annotation.foreignKey}',";
}
if (isAssociation) definition += 'associationType: ${_finalTypeForField(field.type)},';
if (isAssociation) definition += 'associationType: ${checker.withoutNullResultType},';
definition += ')';
fieldsToColumns.add(definition);

Expand Down Expand Up @@ -64,24 +63,4 @@ class SupabaseSerialize extends SupabaseSerdesGenerator
fieldAnnotation: fieldAnnotation,
);
}

String _finalTypeForField(DartType type) {
final checker = checkerForType(type);
final typeRemover = RegExp(r'\<[,\s\w]+\>');

// Future<?>, Iterable<?>
if (checker.isFuture || checker.isIterable) {
return _finalTypeForField(checker.argType);
}

if (checker.toJsonMethod != null) {
return checker.toJsonMethod!.returnType
.getDisplayString()
.replaceAll('?', '')
.replaceAll(typeRemover, '');
}

// remove arg types as they can't be declared in final fields
return type.getDisplayString().replaceAll('?', '').replaceAll(typeRemover, '');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:brick_build/generators.dart';
import 'package:brick_supabase/brick_supabase.dart';
import 'package:brick_core/field_rename.dart';
import 'package:brick_supabase/brick_supabase.dart' show SupabaseSerializable;
import 'package:brick_supabase_generators/src/supabase_deserialize.dart';
import 'package:brick_supabase_generators/src/supabase_fields.dart';
import 'package:brick_supabase_generators/src/supabase_serialize.dart';
Expand Down
8 changes: 4 additions & 4 deletions packages/brick_supabase_generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_supabase_
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 0.1.1+2
version: 0.1.1+3

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
analyzer: ">=6.0.0 <7.0.0"
brick_build: ">=3.0.0 <4.0.0"
brick_core: ^1.1.1
brick_build: ">=3.2.0 <4.0.0"
brick_core: ">=1.2.1 <2.0.0"
brick_json_generators: ">=3.1.0 <4.0.0"
brick_supabase: ">=0.0.1 <2.0.0"
brick_supabase: ">=0.1.1+2 <2.0.0"
build: ">=2.0.0 <3.0.0"
dart_style: ">=2.0.0 <3.0.0"
source_gen: ">=1.2.2 <2.0.0"
Expand Down

0 comments on commit 5e9b15f

Please sign in to comment.