Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converter #28

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ class Books implements SupadartClass<Books> {
static String get c_description => 'description';
static String get c_price => 'price';
static String get c_created_at => 'created_at';

static List<Books>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => Books.fromJson(data)).toList();
}

static Map<String, dynamic> insert({
BigInt? id,
required String name,
Expand Down Expand Up @@ -225,7 +230,7 @@ we now have a typesafe'ish to interact with the database.
final books = await supabase
.books
.select("*")
.withConverter((data) => data.map(Books.fromJson).toList());
.withConverter(books.converter);
```

#### Insert Data
Expand Down
4 changes: 2 additions & 2 deletions generators/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateStaticColumnNames } from "./staticColumnNames";
import { generateUpdateMethod } from "./updateMethod";
import { DartClass, Definitions } from "./types";
import { getDartTypeByFormat, snakeCasingToPascaleCasing } from "./utils";
import { generateWithConverterMethod } from "./withConverterMethod";
import { generateConverterMethod } from "./withConverterMethod";

export const generateDartClasses = (definitions: Definitions): DartClass[] => {
const generatedClasses: DartClass[] = [];
Expand Down Expand Up @@ -47,7 +47,7 @@ export const generateDartClasses = (definitions: Definitions): DartClass[] => {
dartCode += generateStaticColumnNames(properties);

// Helper functions
dartCode += generateWithConverterMethod(className);
dartCode += generateConverterMethod(className);
dartCode += generateInsertMethod(properties, required);
dartCode += generateUpdateMethod(properties);
dartCode += generateFromJsonMethod(className, properties, required);
Expand Down
2 changes: 1 addition & 1 deletion generators/SupadartAbstractClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class SupadartClass<T> {
throw UnimplementedError();
}

static withConverter(List<Map<String, dynamic>> data) {
static converter(List<Map<String, dynamic>> data) {
throw UnimplementedError();
}
}`;
4 changes: 2 additions & 2 deletions generators/withConverterMethod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const generateWithConverterMethod = (className: string) => {
let code = `static List<${className}>? withConverter(List<Map<String, dynamic>> data) {\n`;
export const generateConverterMethod = (className: string) => {
let code = `static List<${className}>? converter(List<Map<String, dynamic>> data) {\n`;
code += `return data.map((data) => ${className}.fromJson(data)).toList();\n`;
code += `}\n`;
return code;
Expand Down
22 changes: 11 additions & 11 deletions test/lib/generated_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class SupadartClass<T> {
throw UnimplementedError();
}

static withConverter(List<Map<String, dynamic>> data) {
static converter(List<Map<String, dynamic>> data) {
throw UnimplementedError();
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ class StringTypes implements SupadartClass<StringTypes> {
'col_charactervarying_array';
static String get c_col_text => 'col_text';
static String get c_col_text_array => 'col_text_array';
static List<StringTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<StringTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => StringTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -211,7 +211,7 @@ class BooleanBitTypes implements SupadartClass<BooleanBitTypes> {
static String get c_col_bit_array => 'col_bit_array';
static String get c_col_bitvarying => 'col_bitvarying';
static String get c_col_bitvarying_array => 'col_bitvarying_array';
static List<BooleanBitTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<BooleanBitTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => BooleanBitTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -348,7 +348,7 @@ class MiscTypes implements SupadartClass<MiscTypes> {
static String get c_col_tsvector_array => 'col_tsvector_array';
static String get c_col_txid_snapshot => 'col_txid_snapshot';
static String get c_col_txid_snapshot_array => 'col_txid_snapshot_array';
static List<MiscTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<MiscTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => MiscTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -539,7 +539,7 @@ class Books implements SupadartClass<Books> {
static String get c_description => 'description';
static String get c_price => 'price';
static String get c_created_at => 'created_at';
static List<Books>? withConverter(List<Map<String, dynamic>> data) {
static List<Books>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => Books.fromJson(data)).toList();
}

Expand Down Expand Up @@ -642,7 +642,7 @@ class GeometricTypes implements SupadartClass<GeometricTypes> {
static String get c_col_polygon_array => 'col_polygon_array';
static String get c_col_circle => 'col_circle';
static String get c_col_circle_array => 'col_circle_array';
static List<GeometricTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<GeometricTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => GeometricTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -830,7 +830,7 @@ class JsonTypes implements SupadartClass<JsonTypes> {
static String get c_col_json_array => 'col_json_array';
static String get c_col_jsonb => 'col_jsonb';
static String get c_col_jsonb_array => 'col_jsonb_array';
static List<JsonTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<JsonTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => JsonTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -934,7 +934,7 @@ class BinaryXmlTypes implements SupadartClass<BinaryXmlTypes> {
static String get c_col_bytea_array => 'col_bytea_array';
static String get c_col_xml => 'col_xml';
static String get c_col_xml_array => 'col_xml_array';
static List<BinaryXmlTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<BinaryXmlTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => BinaryXmlTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -1034,7 +1034,7 @@ class NetworkTypes implements SupadartClass<NetworkTypes> {
static String get c_col_macaddr_array => 'col_macaddr_array';
static String get c_col_macaddr8 => 'col_macaddr8';
static String get c_col_macaddr8_array => 'col_macaddr8_array';
static List<NetworkTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<NetworkTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => NetworkTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -1206,7 +1206,7 @@ class NumericTypes implements SupadartClass<NumericTypes> {
static String get c_col_real_array => 'col_real_array';
static String get c_col_numeric => 'col_numeric';
static String get c_col_numeric_array => 'col_numeric_array';
static List<NumericTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<NumericTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => NumericTypes.fromJson(data)).toList();
}

Expand Down Expand Up @@ -1486,7 +1486,7 @@ class DatetimeTypes implements SupadartClass<DatetimeTypes> {
static String get c_col_timestamptz_array => 'col_timestamptz_array';
static String get c_col_interval => 'col_interval';
static String get c_col_interval_array => 'col_interval_array';
static List<DatetimeTypes>? withConverter(List<Map<String, dynamic>> data) {
static List<DatetimeTypes>? converter(List<Map<String, dynamic>> data) {
return data.map((data) => DatetimeTypes.fromJson(data)).toList();
}

Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/boolean_bit/bool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Future<List<BooleanBitTypes>?> readBoolean(SupabaseClient supabase) async {
try {
return await supabase.boolean_bit_types
.select()
.withConverter((data) => data.map(BooleanBitTypes.fromJson).toList());
.withConverter(BooleanBitTypes.converter);
} catch (error) {
print("readBoolean error");
print(error);
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/boolean_bit/bool_arr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future<List<BooleanBitTypes>?> readBooleanArray(SupabaseClient supabase) async {
try {
return await supabase.boolean_bit_types
.select()
.withConverter((data) => data.map(BooleanBitTypes.fromJson).toList());
.withConverter(BooleanBitTypes.converter);
} catch (error) {
print("readBooleanArray error");
print(error);
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Future<List<DatetimeTypes>?> readDate(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readDate error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/date_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Future<List<DatetimeTypes>?> readDateArray(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readDate Array error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Future<List<DatetimeTypes>?> readTime(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTime error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/time_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Future<List<DatetimeTypes>?> readTimeArray(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTime error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timestamp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Future<List<DatetimeTypes>?> readTimestamp(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimestamp error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timestamp_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Future<List<DatetimeTypes>?> readTimestampArr(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimestampArr error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timestamptz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Future<List<DatetimeTypes>?> readTimestamptz(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimestamptz error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timestamptz_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Future<List<DatetimeTypes>?> readTimestamptzArray(
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimestamptzArr error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timetz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Future<List<DatetimeTypes>?> readTimeTz(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimeTz error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/datetime/timetz_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Future<List<DatetimeTypes>?> readTimeTzArr(SupabaseClient supabase) async {
try {
var res = await supabase.datetime_types
.select()
.withConverter((data) => data.map(DatetimeTypes.fromJson).toList());
.withConverter(DatetimeTypes.converter);
return res;
} catch (error) {
print("readTimeTzArr error");
Expand Down
5 changes: 2 additions & 3 deletions test/test/datatypes/json/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ Future<Object?> createJson(

Future<List<JsonTypes>?> readJson(SupabaseClient supabase) async {
try {
var res = await supabase.json_types
.select()
.withConverter((data) => data.map(JsonTypes.fromJson).toList());
var res =
await supabase.json_types.select().withConverter(JsonTypes.converter);
return res;
} catch (error) {
print("readJson error");
Expand Down
5 changes: 2 additions & 3 deletions test/test/datatypes/json/json_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ Future<Object?> createJsonArray(

Future<List<JsonTypes>?> readJsonArray(SupabaseClient supabase) async {
try {
var res = await supabase.json_types
.select()
.withConverter((data) => data.map(JsonTypes.fromJson).toList());
var res =
await supabase.json_types.select().withConverter(JsonTypes.converter);
return res;
} catch (error) {
print("readJsonArray error");
Expand Down
5 changes: 2 additions & 3 deletions test/test/datatypes/json/jsonb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ Future<Object?> createJsonB(

Future<List<JsonTypes>?> readJsonB(SupabaseClient supabase) async {
try {
var res = await supabase.json_types
.select()
.withConverter((data) => data.map(JsonTypes.fromJson).toList());
var res =
await supabase.json_types.select().withConverter(JsonTypes.converter);
return res;
} catch (error) {
print("readJsonb error");
Expand Down
5 changes: 2 additions & 3 deletions test/test/datatypes/json/jsonb_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ Future<Object?> createJsonBArray(

Future<List<JsonTypes>?> readJsonBArray(SupabaseClient supabase) async {
try {
var res = await supabase.json_types
.select()
.withConverter((data) => data.map(JsonTypes.fromJson).toList());
var res =
await supabase.json_types.select().withConverter(JsonTypes.converter);
return res;
} catch (error) {
print("readJsonArray error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/big_serial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Future<List<NumericTypes>?> readBigSerial(SupabaseClient supabase) async {
try {
var res = await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
return res;
} catch (error) {
print("readBigSerial error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/float4_real.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Future<List<NumericTypes>?> readReal(SupabaseClient supabase) async {
try {
return await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
} catch (error) {
print("readReal error");
print(error);
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/float4_real_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Future<List<NumericTypes>?> readRealArray(SupabaseClient supabase) async {
try {
return await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
} catch (error) {
print("readRealArray error");
print(error);
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/float8_double_precision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Future<List<NumericTypes>?> readDoublePrecision(SupabaseClient supabase) async {
try {
return await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
} catch (error) {
print("readDoublePrecision error");
print(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Future<List<NumericTypes>?> readDoublePrecisionArray(
try {
return await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
} catch (error) {
print("readDoublePrecisionArray error");
print(error);
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/int2_small_int.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Future<List<NumericTypes>?> readSmallint(SupabaseClient supabase) async {
try {
var res = await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
return res;
} catch (error) {
print("readSmallint error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/int2_small_int_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Future<List<NumericTypes>?> readSmallintArray(SupabaseClient supabase) async {
try {
var res = await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
return res;
} catch (error) {
print("readSmallintArray error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/int4_int.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Future<List<NumericTypes>?> readInteger(SupabaseClient supabase) async {
try {
var res = await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
return res;
} catch (error) {
print("readInteger error");
Expand Down
2 changes: 1 addition & 1 deletion test/test/datatypes/numeric/int4_int_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Future<List<NumericTypes>?> readIntegerArray(SupabaseClient supabase) async {
try {
var res = await supabase.numeric_types
.select()
.withConverter((data) => data.map(NumericTypes.fromJson).toList());
.withConverter(NumericTypes.converter);
return res;
} catch (error) {
print("readIntegerArray error");
Expand Down
Loading
Loading