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
Support type converters in .moor files #103
Comments
Felt like this deserves its own issue because it requires some substantial changes. I think the best way to use type mappers on columns is to extend the grammar, which is also what sqldelight is doing: https://github.com/square/sqldelight/blob/7db97c86ae0c83fe128bb9298fbf0ca3d15fbd2d/sqldelight-compiler/src/main/grammars/sqldelight.bnf#L51 CREATE TABLE users (
preferences TEXT MAPPED BY `const CustomConverter()` NOT NULL
) With that approach, we'd then have to parse the import 'converters.dart';
CREATE TABLE users (
preferences TEXT MAPPED BY `const CustomConverter()` NOT NULL
) I've also never used the analyzer to parse and analyze segments of Dart code, it will be interesting to see how that works. I think angular is doing something similar with expressions in html files, I'll take a look at how they're doing it. |
That would be pretty slick! |
AFAIK the build package doesn't support this at the moment, but dart-lang/build#2480 would help. The problem is that we don't get to resolve arbitrary Dart files yet. As soon as we're able to do that, we can generate a temporary, fake Dart file that contains the constructor invocation of the converter, so something like // temp$0.dart
import 'converters.dart';
var converter = const CustomConverter(); for the example above. We should then be able to read the type by looking at the type of the declared "converter" field. |
Darn that's too bad, although it looks like your PR is on track to be merged! I tried messing around with import 'dart:isolate';
class TypeConverter<A, B> {}
class Preferences {}
class PreferenceConverter extends TypeConverter<Preferences, String> {}
void main() async {
final typeConverterClass = 'PreferenceConverter';
final uri = Uri.dataFromString(
'''
import "dart:isolate";
import 'package:converter_test/main.dart';
import "dart:mirrors";
void main(_, SendPort port) {
final Type type = reflectClass($typeConverterClass).superclass.typeArguments.first.reflectedType;
print(type);
port.send(type);
}
''',
mimeType: 'application/dart',
);
final port = ReceivePort();
await Isolate.spawnUri(uri, [], port.sendPort);
final Type type = await port.first;
print(type);
}
// prints:
// Preferences
// Unhandled exception:
// Invalid argument(s): Illegal argument in isolate message : (object is a regular Dart Instance) |
Looks like we're unblocked, but getting the next release ready is more important to me at the moment. |
Sorry for the long delay on this, but finally there is some good news. This was still blocked because there's no This should work on import 'converter.dart'; -- import the Dart file declaring the type converter
CREATE TABLE config (
config_key TEXT not null primary key,
config_value TEXT,
sync_state INTEGER MAPPED BY `const SyncTypeConverter()`
) AS "Config"; One limitation: We can't have import statements in generated Thanks for the patience! |
Works on develop:
The missing imports are a minor annoyance, it's not only the converter that needs to be imported but also the Dart type if it is not any default type. One suggestion, add some field(s) to |
This is a great feature! I like being able to have table columns automatically mapped to enums. Any thoughts about adding support for this in
.moor
files? (#85)Originally posted by @Mike278 in #64 (comment)
The text was updated successfully, but these errors were encountered: