From 15488733f24a2fa90a179e789c5a54074a16f253 Mon Sep 17 00:00:00 2001 From: joris hermans Date: Fri, 4 Jul 2014 13:04:17 +0200 Subject: [PATCH] Operators and Async --- CHANGELOG.md | 6 +++++ lib/server/abstract_storage.dart | 11 ++++++++- lib/server/json_storage.dart | 42 +++++++++++++++++++++++++++----- lib/server/memory_storage.dart | 8 +++++- pubspec.yaml | 2 +- readme.md | 11 ++++++++- test/json_access_test.dart | 19 +++++++++++++++ test/json_test.dart | 17 ++++++++++--- test/memory_test.dart | 2 +- test/store/someValue.json | 1 + 10 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 test/json_access_test.dart create mode 100644 test/store/someValue.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 20e749f..7b55955 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/server/abstract_storage.dart b/lib/server/abstract_storage.dart index d26599d..f3878bb 100644 --- a/lib/server/abstract_storage.dart +++ b/lib/server/abstract_storage.dart @@ -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; } \ No newline at end of file diff --git a/lib/server/json_storage.dart b/lib/server/json_storage.dart index 1ee9094..d9de928 100644 --- a/lib/server/json_storage.dart +++ b/lib/server/json_storage.dart @@ -6,6 +6,8 @@ class JsonStorage extends Storage { List keys = new List(); + Map map; + JsonStorage (String dir) : super._() { pathToStore = Platform.script.resolve(dir).toFilePath(); _completer = new Completer(); @@ -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()); @@ -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()); @@ -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) { @@ -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() { @@ -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(); diff --git a/lib/server/memory_storage.dart b/lib/server/memory_storage.dart index cec2c80..4e1c4a9 100644 --- a/lib/server/memory_storage.dart +++ b/lib/server/memory_storage.dart @@ -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; } diff --git a/pubspec.yaml b/pubspec.yaml index 4835193..7081ca9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cargo -version: 0.1.0 +version: 0.2.0 author: Joris Hermans description: A key value, storage library for dart homepage: https://github.com/jorishermans/cargo diff --git a/readme.md b/readme.md index e27b0c6..4439844 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/json_access_test.dart b/test/json_access_test.dart new file mode 100644 index 0000000..2f1beaf --- /dev/null +++ b/test/json_access_test.dart @@ -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); + }); + }); + + +} \ No newline at end of file diff --git a/test/json_test.dart b/test/json_test.dart index 0ee55cd..c1d0128 100644 --- a/test/json_test.dart +++ b/test/json_test.dart @@ -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"]); + }); + }); }); + + } \ No newline at end of file diff --git a/test/memory_test.dart b/test/memory_test.dart index 5b41c10..d0a3d05 100644 --- a/test/memory_test.dart +++ b/test/memory_test.dart @@ -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); }); diff --git a/test/store/someValue.json b/test/store/someValue.json new file mode 100644 index 0000000..805ce61 --- /dev/null +++ b/test/store/someValue.json @@ -0,0 +1 @@ +{"value":"go"} \ No newline at end of file