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

Added a synchronous way of loading configs #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -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.
its way to me, or others that are very knowledgeable.
31 changes: 28 additions & 3 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

/**
Expand All @@ -68,6 +77,10 @@ abstract class ConfigLoader {
*/
Future<String> loadConfig(pathOrUrl);

/**
* Similar but synchronous
*/
String loadConfigSync(pathOrUrl);
}

/**
Expand All @@ -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<Map<String,Object>> parse(String configText);
Future<Map<String,Object>> parse(String configText) {
var completer = new Completer<Map>();

var map = parseSync(configText);
completer.complete(map);

return completer.future;
}

/**
* Similar but synchronously
*/
Map<String,Object> parseSync(String configText);
}
8 changes: 8 additions & 0 deletions lib/default_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ Future<Map> 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();
}
18 changes: 16 additions & 2 deletions lib/loaders/config_loader_httprequest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
16 changes: 6 additions & 10 deletions lib/parsers/config_parser_json.dart
Original file line number Diff line number Diff line change
@@ -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<Map> parse(String configText) {
var completer = new Completer<Map>();

var map = JSON.parse(configText);
completer.complete(map);

return completer.future;
class JsonConfigParser extends ConfigParser {

Map parseSync(String configText) {
return JSON.decode(configText);
}

}
14 changes: 5 additions & 9 deletions lib/parsers/config_parser_yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import 'package:yaml/yaml.dart' as YAML;

import '../config.dart';

class YamlConfigParser implements ConfigParser {

Future<Map> parse(String configText) {
var completer = new Completer<Map>();

var map = YAML.loadYaml(configText);
completer.complete(map);

return completer.future;
class YamlConfigParser extends ConfigParser {

Map parseSync(String configText) {
return YAML.loadYaml(configText);
}

}