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

Use Map for key value stores instead of generics #13

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 21 additions & 18 deletions example/services/nitric_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ import 'package:nitric_sdk/src/context/common.dart';
import 'package:uuid/uuid.dart';

class Profile {
String name;
int age;
String homeTown;
late String name;
late int age;
late String homeTown;
late List<String> contacts;

Profile(this.name, this.age, this.homeTown);
Profile({required this.name, required this.age, required this.homeTown});

Profile.fromJson(Map<String, dynamic> contents)
: name = contents["name"] as String,
age = contents["age"] as int,
homeTown = contents["homeTown"] as String;
homeTown = contents["homeTown"] as String,
contacts = List<String>.from(contents["contacts"]);

Map<String, dynamic> toJson() => {
'name': name,
'age': age,
'homeTown': homeTown,
};
Map<String, dynamic> toJson() =>
{'name': name, 'age': age, 'homeTown': homeTown, 'contacts': contacts};
}

void main() {
// Create an API named 'public'
final profileApi = Nitric.api("public");

// Define a collection named 'profiles', then request reading and writing permissions.
final profiles = Nitric.store<Profile>("profiles").requires([
final profiles = Nitric.store("profiles").requires([
KeyValueStorePermission.getting,
KeyValueStorePermission.deleting,
KeyValueStorePermission.setting
Expand All @@ -45,13 +44,17 @@ void main() {

final id = uuid.v4();

final profile = Profile.fromJson(ctx.req.json());

// Store the new profile in the profiles collection
await profiles.set(id, profile);
try {
var profile = Profile.fromJson(ctx.req.json());
// Store the new profile in the profiles collection
await profiles.set(id, profile.toJson());

// Send a success response.
ctx.resp.body = "Profile $id created.";
// Send a success response.
ctx.resp.body = "Profile $id created.";
} on Exception catch (e) {
ctx.resp.status = 400;
ctx.resp.body = "An error occurred: $e";
}

return ctx;
});
Expand All @@ -62,7 +65,7 @@ void main() {
try {
// Retrieve and return the profile data
final profile = await profiles.get(id);
ctx.resp.json(profile.toJson());
ctx.resp.json(profile);
} on Exception catch (e) {
print(e);
ctx.resp.status = 404;
Expand Down
31 changes: 25 additions & 6 deletions lib/src/api/keyvalue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'dart:convert';
import 'package:nitric_sdk/src/grpc_helper.dart';
import 'package:nitric_sdk/src/nitric/proto/keyvalue/v1/keyvalue.pbgrpc.dart';

import '../google/protobuf/struct.pb.dart';
import '../nitric/google/protobuf/struct.pb.dart' as $s;

/// A Key Value Store.
class KeyValueStore<T> {
class KeyValueStore {
late KeyValueClient _keyValueClient;

final String name;
Expand All @@ -18,24 +18,43 @@ class KeyValueStore<T> {
}

/// Get a reference to a key in the store.
Future<T> get(String key) async {
Future<Map<String, dynamic>> get(String key) async {
var req = KeyValueGetRequest(ref: ValueRef(key: key, store: name));

var resp = await _keyValueClient.get(req);

return json.decode(resp.writeToJson());
}

Future<void> set(String key, T value) async {
var content = Struct();
content.mergeFromJson(json.encode(value));
Future<void> set(String key, Map<String, dynamic> value) async {
var content = $s.Struct();
value.forEach((key, value) => content.fields[key] = _asValue(value));

var req = KeyValueSetRequest(
ref: ValueRef(key: key, store: name), content: content);

await _keyValueClient.set(req);
}

$s.Value _asValue(dynamic value) {
if (value is String) {
return $s.Value(stringValue: value);
} else if (value is int) {
return $s.Value(numberValue: value.toDouble());
} else if (value is double) {
return $s.Value(numberValue: value);
} else if (value is List) {
return $s.Value(
listValue: $s.ListValue(values: value.map((e) => _asValue(e))));
} else if (value is Map) {
var content = $s.Struct();
value.forEach((key, value) => content.fields[key] = _asValue(value));
return $s.Value(structValue: content);
}

return $s.Value(nullValue: null);
}

Future<void> delete(String key) async {
var req = KeyValueDeleteRequest(ref: ValueRef(key: key, store: name));

Expand Down
4 changes: 2 additions & 2 deletions lib/src/nitric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Nitric {
}

/// Create a [name]d collection for storing documents.
static KeyValueStoreResource<T> store<T>(String name) {
var res = KeyValueStoreResource<T>(name);
static KeyValueStoreResource store(String name) {
var res = KeyValueStoreResource(name);

res.register();

Expand Down
6 changes: 3 additions & 3 deletions lib/src/resources/keyvalue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'common.dart';

enum KeyValueStorePermission { getting, setting, deleting }

class KeyValueStoreResource<T> extends SecureResource<KeyValueStorePermission> {
class KeyValueStoreResource extends SecureResource<KeyValueStorePermission> {
KeyValueStoreResource(String name) : super(name);

@override
Expand Down Expand Up @@ -40,13 +40,13 @@ class KeyValueStoreResource<T> extends SecureResource<KeyValueStorePermission> {
return actions;
}

KeyValueStore<T> requires(List<KeyValueStorePermission> permissions) {
KeyValueStore requires(List<KeyValueStorePermission> permissions) {
if (permissions.isEmpty) {
throw "Must supply at least one permission for key value store $name";
}

registerPolicy(permissions);

return KeyValueStore<T>(name);
return KeyValueStore(name);
}
}
Loading