dart_config 0.5.0

  • README.md
  • Installing
  • Versions
  • 64

dart_config

Config files for the client and server.

Do you have code that looks like this?

var myServerUrl = "http://localhost/foo"; // local server
//var myServerUrl = "http://awesomeapp.com/foo"; // real server

Do you wish you could load settings from a config file, like this yaml below:

# local server config.yaml
myServerUrl: http://localhost/foo

or

# real server config.yaml
myServerUrl: http://awesomeapp.com/foo

Now you can, with the same syntax whether you're running Dart in the browser, or running Dart on the server.

Instructions

1. Add a dependency to pubspec.yaml for dart_config:

dependencies
  dart_config: any
  

2. If you are running in the browser, add the following code to your app to load config information from the server:

import 'package:dart_config/default_browser.dart';

main() {
  loadConfig().then(
    (Map config) {
     print(config["myServerUrl"]);
     // continue with your app here!
    }, 
    onError: (error) => print(error));
}

2. If you are running in the server, add the following code to your app to load config information from the filesystem:

import 'package:dart_config/default_server.dart';

main() {
  loadConfig().then(
    (Map config) {
     print(config["myServerUrl"]);
     // continue with your app here!
    }, 
    onError: (error) => print(error));
}

What just happened?

Both the client and server code looks for a file called config.yaml, loads it from the current folder (or current browser path), parses it, and returns it to your app as a map of key/value pairs (the values may also be maps themselves).

Take a look at test/test_default_browser.dart and test/test_default_server.dart, and the associated config.yaml files.

Want more control?

Of course you want more control! The loadConfig() function actually takes an optional filename parameter, which defaults to config.yaml - but you can provide a different filename / path if you want.

Want even more control?

Of course you do. The default_browser and default_server libraries are just opinionated implementations. Behind the scenes, they are calling code that looks similar to this:

var config = new Config(filename,
  new ConfigFilesystemLoader(),
  new YamlConfigParser());

return config.readConfig(); // returns a Future<Map>

The Config class has a single constructor, which takes three parameters. The first parameter is the location of the config file. The second is an implementation of ConfigLoader, and the third is an implementation of ConfigParser (this is a typical dependency injection pattern)

The dart_config library comes (at the moment) with two ConfigLoader implementations and two ConfigParser implementations:

ConfigLoaders:

  • ConfigFilesystemLoader
  • ConfigHttpRequestLoader

ConfigParsers:

  • YamlConfigParser
  • JsonConfigParser

You can use any combination of loader and parser. At its core, the config system needs to know how to load a config file and how to parse a config file. It really is no more complex than that.

Take a look at the three files: tests.dart (a VM independant suite of tests), and the test_browser.dart and test_server.dart, which run the same set of tests found in tests.dart both in the browser and on the server. The only differences are ConfigLoader implementation, and the path to the config files.

Want to load a config file that looks like "x"?

No problem - simply implement your own version of ConfigLoader - there's only one method: Future<String> loadConfig(pathOrUrl) - just return the contents of the config file in the Future<String>, and it will get passed into whatever parser you specify.

Want to parse a config file of type "y"?

No problem again - simply implement your own version of ConfogParser - there's only one method: Future<Map<String,Object>> parse(String configText) - just parse the String contents that the ConfigLoader has loaded into a Map, and return it in a Future.

Written your own config loader / parser?

Great! - Please share :) - create your own pub packaged containing loaders and/ or parsers, or clone this library and issue pull requests.

Some ideas (that I might work on if someone else doesn't get there first):

  • TOML parser
  • Key=value pair parser
  • Server-side HttpClient configLoader - load config files from a remote url

Found a bug?

Raise an issue on github and I'll take a look, or even better, create a pull request with a failing unit test or even a fix?

Got a question?

StackOverflow is a great place for that. Make sure you use the dart tag, and it will find its way to me, or others that are very knowledgeable.

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_config: "^0.5.0"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_config/config.dart';

import 'package:dart_config/default_browser.dart';

import 'package:dart_config/default_server.dart';

import 'package:dart_config/loaders/config_loader_filesystem.dart';

import 'package:dart_config/loaders/config_loader_httprequest.dart';

import 'package:dart_config/parsers/config_parser_json.dart';

import 'package:dart_config/parsers/config_parser_yaml.dart';
        
Version Uploaded Documentation Archive
0.5.0 Jun 13, 2013 Go to the documentation of dart_config 0.5.0 Download dart_config 0.5.0 archive
0.3.0+3 May 20, 2013 Go to the documentation of dart_config 0.3.0+3 Download dart_config 0.3.0+3 archive
0.3.0+2 Mar 27, 2013 Go to the documentation of dart_config 0.3.0+2 Download dart_config 0.3.0+2 archive
0.3.0+1 Mar 16, 2013 Go to the documentation of dart_config 0.3.0+1 Download dart_config 0.3.0+1 archive
0.3.0 Feb 24, 2013 Go to the documentation of dart_config 0.3.0 Download dart_config 0.3.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 16, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
88 / 100
Health:
Code health derived from static analysis. [more]
67 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
64

Platforms

Detected platforms: unsure

Error(s) prevent platform classification.

Suggestions

  • Fix lib/parsers/config_parser_json.dart.

    Strong-mode analysis of lib/parsers/config_parser_json.dart failed with the following error:

    line: 4 col: 8
    Target of URI doesn't exist: 'dart:json'.

  • Fix lib/parsers/config_parser_yaml.dart.

    Strong-mode analysis of lib/parsers/config_parser_yaml.dart failed with the following error:

    line: 10 col: 3
    Invalid override. The type of 'YamlConfigParser.parse' ('(String) → Future<Map<dynamic, dynamic>>') isn't a subtype of 'ConfigParser.parse' ('(String) → Future<Map<String, Object>>').

  • Fix further 2 Dart files.

    Similar analysis of the following files failed:

    • lib/loaders/config_loader_httprequest.dart
    • lib/loaders/config_loader_filesystem.dart
  • Fix platform conflicts.

    Make sure none of the libraries use mutually exclusive dependendencies.

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 3 error(s) and 0 warning(s).

  • Use constrained dependencies.

    The pubspec.yaml contains 2 dependencies without version constraints. Specify version ranges for the following dependencies: unittest, yaml.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • The description is too long.

    Search engines will display only the first part of the description. Try to keep it under 180 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dart_config.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
unittest any 0.11.7 0.12.4+1
yaml >=0.4.2 2.1.13
Transitive dependencies
charcode 1.1.1
collection 1.14.5
path 1.5.1
source_span 1.4.0
stack_trace 1.9.2
string_scanner 1.0.2
Dev dependencies
browser any