Skip to content

Commit

Permalink
Operators and Async
Browse files Browse the repository at this point in the history
  • Loading branch information
jorishermans committed Jul 4, 2014
1 parent 2079d43 commit 1548873
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This file contains highlights of what changes on each version of the force package.

#### Pub version 0.2.0 ####

- Add async for getting a value
- Add operator functions [] and []=
- Fix some issues on the length parameter

#### Pub version 0.1.0 ####

- Add start method to the storage implementation
Expand Down
11 changes: 10 additions & 1 deletion lib/server/abstract_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ abstract class Storage {
}
}

String getItem(String key);
dynamic getItemSync(String key);
Future getItem(String key);
void setItem(String key, data);
void removeItem(String key);
void clear();
int length();

void operator []=(String key, value) {
setItem(key, value);
}
dynamic operator [](String key){
return getItemSync(key);
}


Future start() => _completer.future;
}
42 changes: 36 additions & 6 deletions lib/server/json_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class JsonStorage extends Storage {

List<String> keys = new List<String>();

Map map;

JsonStorage (String dir) : super._() {
pathToStore = Platform.script.resolve(dir).toFilePath();
_completer = new Completer();
Expand All @@ -25,7 +27,7 @@ class JsonStorage extends Storage {
}
}

dynamic getItem(String key) {
dynamic getItemSync(String key) {
if (keys.contains(key)) {
var uriKey = new Uri.file(pathToStore).resolve("$key.json");
var file = new File(uriKey.toFilePath());
Expand All @@ -38,6 +40,24 @@ class JsonStorage extends Storage {
return null;
}

Future getItem(String key) {
Completer complete = new Completer();
if (keys.contains(key)) {
var uriKey = new Uri.file(pathToStore).resolve("$key.json");
var file = new File(uriKey.toFilePath());

file.exists().then((bool exist) {
// need to convert it to json!
file.readAsString().then((String fileValues) {
complete.complete(JSON.decode(fileValues));
});
});
} else {
complete.complete();
}
return complete.future;
}

void setItem(String key, data) {
var uriKey = new Uri.file(pathToStore).resolve("$key.json");
var file = new File(uriKey.toFilePath());
Expand All @@ -46,8 +66,10 @@ class JsonStorage extends Storage {
} else {
file.createSync();
_writeFile(file, key, data);
}
keys.add(key);
}
if (!keys.contains(key)) {
keys.add(key);
}
}

void _writeFile (File file, key, data) {
Expand All @@ -71,9 +93,14 @@ class JsonStorage extends Storage {
if (path.indexOf(".json") > 1) {
log.info("deleting $path");
var file = new File(path);
file.deleteSync();
try {
file.deleteSync();
} on Exception catch(e) {
print('Unknown exception: $e');
}
}
});
keys.clear();
}

int length() {
Expand All @@ -85,8 +112,11 @@ class JsonStorage extends Storage {
dir.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) {
var path = entity.path;
if (path.indexOf(".json") > 1) {
keys.add(path);
RegExp exp = new RegExp(r"^\\(.+\\)*(.+)\.(.+)$");

if (exp.hasMatch(path) && path.indexOf(".json") > 1) {
Match fileName = exp.firstMatch(path);
keys.add(fileName.toString());
}
}).onDone(() {
_completer.complete();
Expand Down
8 changes: 7 additions & 1 deletion lib/server/memory_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ class MemoryStorage extends Storage {
_completer.complete();
}

dynamic getItem(String key) {
dynamic getItemSync(String key) {
return values[key];
}

Future getItem(String key) {
Completer complete = new Completer();
complete.complete(values[key]);
return complete.future;
}

void setItem(String key, data) {
values[key] = data;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cargo
version: 0.1.0
version: 0.2.0
author: Joris Hermans <hermansj@gmail.com>
description: A key value, storage library for dart
homepage: https://github.com/jorishermans/cargo
Expand Down
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ Then you will have an asynchronous method to say that the storage is started
Add data to the storage.

storage.setItem("data", {"data": "data"});
storage["data"] = {"data": "data"};

Retrieve data from the storage.
Retrieve data from the storage on an asynchronous way.

var data = storage.getItem("data");

Or on a synchronous way.

var data = storage["data"];

Or like this

var data = storage.getItemSync("data");

### Todo's ###

- add a client implementation of storage
Expand Down
19 changes: 19 additions & 0 deletions test/json_access_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:unittest/unittest.dart';
import 'package:cargo/cargo.dart';

main() {
// First tests!
Storage storage = new Storage(path: "store/");

storage.start().then((_) {
test('test basic json storage access', () {
storage.clear();
storage["someValue"] = {"value": "go"};

expect(storage["someValue"], {"value": "go"});
expect(storage.length(), 1);
});
});


}
17 changes: 13 additions & 4 deletions test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ main() {
Storage storage = new Storage(path: "store/");

storage.start().then((_) {
test('test basic memory storage', () {
storage.clear();


test('test basic json storage', () {
storage.clear();
storage.setItem("data", {"data": "data"});

var data = storage.getItem("data");
var data = storage.getItemSync("data");
expect(data["data"], "data");
expect(storage.length(), 1);
});

storage["async"] = {"as": "go"};
storage.getItem("async").then((value) {
test('test async storage', () {
expect(value, storage["async"]);
});
});
});


}
2 changes: 1 addition & 1 deletion test/memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ main() {
test('test basic memory storage', () {
storage.setItem("data", {"data": "data"});

var data = storage.getItem("data");
var data = storage.getItemSync("data");
expect(data["data"], "data");
expect(storage.length(), 1);
});
Expand Down
1 change: 1 addition & 0 deletions test/store/someValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"value":"go"}

0 comments on commit 1548873

Please sign in to comment.