Blimster / resp_client

A RESP (REdis Serialization Protocol) client for Dart.
MIT License
8 stars 4 forks source link

Feature request: use generics to spesifcy type in command get #9

Closed erf closed 3 years ago

erf commented 3 years ago

get now always return a string, I think it would be valuable to use generics to specify which type you'd like to return.

E.g.

  final commands = RespCommands(client);
  final int visitors = await commands.get<int>('visitors');

Would return an int.

In the dartis package they did this:

final commands = client.asCommands<String, String>();

but not sure if the key here would be anything else than String..

Blimster commented 3 years ago

This is logic I don't want in this package. Redis always returns a string as the result of a get command and it is not the responsibility of this package to interpret this string. And what if you use a Date or bool as type parameter?

I suggest to write an extension for you specific use case.

erf commented 3 years ago

I understand if you don't want to do this, but you already do this with e.g. Command.dbsize (you have helper functions to parse spesific types ) ? Could you not have a similar generic function for getting a any key with a given type and then just throw an exception if the type wasn't supported?

erf commented 3 years ago

I just made a test with a bool and a Date now, and i think it should be fine to parse them from a string to their correct type.

Future<void> testBoolean() async {
  final server = await connectSocket('localhost');
  final client = RespClient(server);
  final commands = RespCommands(client);
  final result = await commands.set('someBool', true);
  final value = await commands.get('someBool');
  print(value);
  final boolValue = value == 'true';
  print(boolValue);
}

Future<void> testDate() async {
  final server = await connectSocket('localhost');
  final client = RespClient(server);
  final commands = RespCommands(client);
  final result = await commands.set('someDate', DateTime.now());
  final value = await commands.get('someDate');
  print(value);
  final dateValue = DateTime.parse(value);
  print(dateValue);
}
erf commented 3 years ago

But i'm thinking primarily of the basic types. I could help with a PR, if interested in this.

erf commented 3 years ago

Could also just make some helper functions for spesific types like: getInt, getBool, etc.

Blimster commented 3 years ago

Sorry, I really don't want that kind of logic in this package. But thanks for your offer to contribute.

erf commented 3 years ago

Ok. No problem :)