Skip to content

Commit

Permalink
another attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
danylo-safonov-solid committed Nov 15, 2023
1 parent 0fcacde commit 020f003
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions add_imports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ classes_map:
'mod.ts':
- QueryClient
- Client
- 'Client': PostgresClient
- Transaction
13 changes: 10 additions & 3 deletions bin/add_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ String createNewSource(String sourceString, Config config) {
.map((e) => e.group(1))
.whereNotNull()
.toSet()
.intersection(config.classes)
.where((alias) => config.classes.any((e) => e.$2 == alias))
.whereNot((e) => sourceString.contains('import { $e }'))
.toList();

return [
...[config.importStringForClass, (e) => 'self.$e = $e;']
.map(classes.map)
...[
((String, String) e) => config.importStringForClass(e.$1),
((String, String) e) => 'self.${e.$2} = ${e.$1};',
]
.map(
classes
.map((alias) => config.classes.firstWhere((e) => e.$2 == alias))
.map,
)
.flattened,
sourceString,
].join('\n');
Expand Down
21 changes: 16 additions & 5 deletions lib/src/add_imports/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:yaml/yaml.dart';

class Config {
final String fileUrlPrefix;
final Map<String, List<String>> classesMap;
final Map<String, List<(String, String)>> classesMap;

Set<String> get classes => classesMap.values.flattened.toSet();
Set<(String, String)> get classes => classesMap.values.flattened.toSet();

Config({required this.fileUrlPrefix, required this.classesMap});

Expand All @@ -19,7 +19,13 @@ class Config {
final classesMap = (parsedYaml['classes_map'] as YamlMap).map(
(key, value) => MapEntry(
key as String,
[...value as YamlList].cast<String>(),
[...value as YamlList]
.map(
(e) => e is YamlMap
? (e.keys.first as String, e.values.first as String)
: (e as String, e),
)
.toList(),
),
);

Expand All @@ -32,8 +38,13 @@ class Config {
}
}

String _filenameForClass(String classname) =>
classesMap.entries.firstWhere((e) => e.value.contains(classname)).key;
String _filenameForClass(String classname) => classesMap.entries
.firstWhere(
(e) => e.value.any(
(classnamePair) => classnamePair.$1 == classname,
),
)
.key;

String importStringForClass(String classname) {
final filename = _filenameForClass(classname);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/postgres_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'package:deno_postgres_interop/src/client_options.dart';
import 'package:deno_postgres_interop/src/query_client.dart';

/// [deno-postgres@v​0.17.0/Client](https://deno.land/x/[email protected]/mod.ts?s=Client).
@JS('Client')
@JS()
class PostgresClient extends QueryClient {
/// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/[email protected]/mod.ts?s=Client#ctor_0).
external factory PostgresClient(String dbUrl);

/// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/[email protected]/mod.ts?s=Client#ctor_0).
factory PostgresClient.config(ClientOptions config) =>
callConstructor('Client', [config]);
callConstructor('PostgresClient', [config]);

/// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/[email protected]/mod.ts?s=Client#ctor_0).
factory PostgresClient.empty() => callConstructor('Client', null);
factory PostgresClient.empty() => callConstructor('PostgresClient', null);
}

0 comments on commit 020f003

Please sign in to comment.