diff --git a/README.md b/README.md index b9bac19..a68ca59 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ dart_config =========== +## Testing integration with slack + ## Config files for the client and server. Do you have code that looks like this? @@ -149,4 +151,4 @@ a failing unit test or even a fix? StackOverflow is a great place for that. Make sure you use the [dart](http://stackoverflow.com/questions/tagged/dart) tag, and it will find -its way to me, or others that are very knowledgeable. \ No newline at end of file +its way to me, or others that are very knowledgeable. diff --git a/lib/config.dart b/lib/config.dart index 83390ab..20e3abc 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -52,7 +52,16 @@ class Config { return completer.future; } - + + /** + * Same as readConfig but synchronously + */ + Map readConfigSync() { + // load, then parse, then complete + configValues = _configParser.parseSync(_configLoader.loadConfigSync(_configPathOrUrl)); + return configValues; + } + } /** @@ -68,6 +77,10 @@ abstract class ConfigLoader { */ Future loadConfig(pathOrUrl); + /** + * Similar but synchronous + */ + String loadConfigSync(pathOrUrl); } /** @@ -77,9 +90,21 @@ abstract class ConfigLoader { * `int`, `num`, `double`, `String`, `bool`, `null`. */ abstract class ConfigParser { - + /** * Returns a Config object from the parsed config file. */ - Future> parse(String configText); + Future> parse(String configText) { + var completer = new Completer(); + + var map = parseSync(configText); + completer.complete(map); + + return completer.future; + } + + /** + * Similar but synchronously + */ + Map parseSync(String configText); } \ No newline at end of file diff --git a/lib/default_browser.dart b/lib/default_browser.dart index f2afb4c..916b01d 100644 --- a/lib/default_browser.dart +++ b/lib/default_browser.dart @@ -11,4 +11,12 @@ Future loadConfig([String filename="config.yaml"]) { new YamlConfigParser()); return config.readConfig(); +} + +Map loadConfigSync([String filename="config.yaml"]) { + var config = new Config(filename, + new ConfigHttpRequestLoader(), + new YamlConfigParser()); + + return config.readConfigSync(); } \ No newline at end of file diff --git a/lib/loaders/config_loader_httprequest.dart b/lib/loaders/config_loader_httprequest.dart index 62bf9f1..736f724 100644 --- a/lib/loaders/config_loader_httprequest.dart +++ b/lib/loaders/config_loader_httprequest.dart @@ -20,6 +20,20 @@ class ConfigHttpRequestLoader extends ConfigLoader { return completer.future; } - - + + String loadConfigSync(String path) { + String result = ""; + + HttpRequest request = new HttpRequest(); + request.open('GET', path, async : false); + request.send(); + result = request.responseText; + + if (request.statusText != "OK") { + print("Synchronous config load of $path was not possible"); + } + + return result; + } + } \ No newline at end of file diff --git a/lib/parsers/config_parser_json.dart b/lib/parsers/config_parser_json.dart index 4b6efc5..073ced7 100644 --- a/lib/parsers/config_parser_json.dart +++ b/lib/parsers/config_parser_json.dart @@ -1,18 +1,14 @@ library config_parser_json; import 'dart:async'; -import 'dart:json' as JSON; +import 'dart:convert'; import '../config.dart'; -class JsonConfigParser implements ConfigParser { - - Future parse(String configText) { - var completer = new Completer(); - - var map = JSON.parse(configText); - completer.complete(map); - - return completer.future; +class JsonConfigParser extends ConfigParser { + + Map parseSync(String configText) { + return JSON.decode(configText); } + } \ No newline at end of file diff --git a/lib/parsers/config_parser_yaml.dart b/lib/parsers/config_parser_yaml.dart index d0529ae..cf08652 100644 --- a/lib/parsers/config_parser_yaml.dart +++ b/lib/parsers/config_parser_yaml.dart @@ -5,14 +5,10 @@ import 'package:yaml/yaml.dart' as YAML; import '../config.dart'; -class YamlConfigParser implements ConfigParser { - - Future parse(String configText) { - var completer = new Completer(); - - var map = YAML.loadYaml(configText); - completer.complete(map); - - return completer.future; +class YamlConfigParser extends ConfigParser { + + Map parseSync(String configText) { + return YAML.loadYaml(configText); } + } \ No newline at end of file