safe_config 1.2.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 88

safe_config

A library to add type and name safety to YAML configuration files.

Basic Usage

safe_config is simple - it maps YAML files to Dart objects using the keys as property names. This mapping ensures that the types of your YAML values are checked at runtime and that you haven't typo'ed any YAML key names.

Consider a case where you want to configure the port and the Server header of your application. You define a subclass of ConfigurationItem with those properties:

class ApplicationConfiguration extends ConfigurationItem {
 	ApplicationConfiguration(String fileName) : 
 		super.fromFile(fileName);
	
	int port;
	String serverHeader;
}

Your YAML file should contain those two, case-sensitive keys:

port: 8000
serverHeader: booyah/1

To read your configuration file:

var config = new ApplicationConfiguration("config.yaml");
print("${config.port}"); // -> 8000
print("${config.serverHeader}"); // -> "booyah/1"

If port is not an int or is missing, you will get an exception. If serverHeader is not a String or missing, you will get an exception.

Useful Usage

You may mark properties in ConfigurationItems as optional.

class ApplicationConfiguration extends ConfigurationItem {
 	ApplicationConfiguration(String fileName) : 
 		super.fromFile(fileName);
	
	int port;
	
	@optionalConfiguration
	String serverHeader;
}

If serverHeader is omitted from your YAML when read, its value will be null and no exception is thrown.

There are two built-in ConfigurationItems, DatabaseConnectionConfiguration and APIConfiguration. These contain typical properties for common configuration values.

You may nest ConfigurationItems as deeply as you wish:

class ApplicationConfiguration extends ConfigurationItem {
 	ApplicationConfiguration(String fileName) : 
 		super.fromFile(fileName);
	
	int port;
	
	DatabaseConnectionConfiguration userDatabase;
}

For which the YAML may be:

port: 8000
userDatabase:
  databaseName: dartstuff
  host: stablekernel.com
  port: 5432

You may also use arrays and maps, for which the values can be primitive types or ConfigurationItem subclasses.

class ApplicationConfiguration extends ConfigurationItem {
 	ApplicationConfiguration(String fileName) : 
 		super.fromFile(fileName);
		
	Map<String, DatabaseConnectionConfiguration> databases;
}

The YAML here may be:

databases:
  db1:
    databaseName: dartstuff
    host: stablekernel.com
    port: 5432
  db2:
    databaseName: otherstuff
    host: somewhereoutthere.com
    port: 5432

Then, you may access it as such:

var config = new ApplicationConfig("config.yaml");

var databaseOne = config.databases["db1"];
await database.connect(databaseOne.host, 
	databaseOne.port, 
	databaseOne.databaseName);

A configuration item may have multiple YAML representations. For example, a DatabaseConnectionConfiguration can be represented as a Map<String, dynamic> of each component (username, host, etc.). It may also be represented as a connection string, e.g. "postgres://user:password@host:port/database". You may allow this behavior by overriding decode in a subclass of ConfigurationItem:

class AuthorityConfiguration extends ConfigurationItem {
  String username;
  String password;

  void decode(dynamic anyValue) {
    if (anyValue is! String) {
      throw new ConfigurationException("Expected a String for AuthorityConfiguration.";
    }

    username = anyValue.split(":").first;
    password = anyValue.split(":").last;
  }
}

This configuration item could be read in either of these two scenarios:

authority:
    username: "Bob"
    password: "Fred"

// or

authority: "Bob:Fred"

Configuration items may also be redirected to use environment variables. For platforms like Heroku, this is valuable because configuration management is done through environment variables. To reference an environment variable in a configuration file, use the '$VARIABLE' syntax as a value:

port: $PORT

When read, this configuration file would replace '$PORT' with the environment variable named 'PORT'.

See the tests for more examples.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

1.2.1

  • Throws exception when parsing if environment variable does not exist and is required.

1.2.0

  • Allow ConfigurationItem to validate their values by overriding validate (Thanks to Denis Albuquerque, @zidenis)

1.1.3

  • Ignore private variables declared in ConfigurationItem subclasses

1.1.2

  • Ignore static variables declared in ConfigurationItem subclasses

1.1.1

  • Throw exception if an unexpected key is found when reading configuration.

1.1.0

  • Enable support for reading environment variables.
  • Allow decoders for ConfigurationItem subclasses that may have multiple representations, e.g. a DatabaseConnectionConfiguration from a database connection string.

1.0.4

  • Fix issue where nested ConfigurationItems marked as optional would fail to parse.

1.0.3

  • Add ConfigurationItem.fromMap to pass a Map as the source for a ConfigurationItem.

1.0.2

  • Add library level documentation.

1.0.1

  • Add documentation generation.
  • Update readme.

1.0.0

  • Initial version.

1. Depend on it

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


dependencies:
  safe_config: "^1.2.1"

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:safe_config/safe_config.dart';
        
Version Uploaded Documentation Archive
1.2.1 Nov 28, 2017 Go to the documentation of safe_config 1.2.1 Download safe_config 1.2.1 archive
1.2.0 Jun 6, 2017 Go to the documentation of safe_config 1.2.0 Download safe_config 1.2.0 archive
1.1.3 Dec 31, 2016 Go to the documentation of safe_config 1.1.3 Download safe_config 1.1.3 archive
1.1.2 Dec 30, 2016 Go to the documentation of safe_config 1.1.2 Download safe_config 1.1.2 archive
1.1.1 Nov 10, 2016 Go to the documentation of safe_config 1.1.1 Download safe_config 1.1.1 archive
1.1.0 Sep 29, 2016 Go to the documentation of safe_config 1.1.0 Download safe_config 1.1.0 archive
1.0.4 Aug 28, 2016 Go to the documentation of safe_config 1.0.4 Download safe_config 1.0.4 archive
1.0.3 Aug 15, 2016 Go to the documentation of safe_config 1.0.3 Download safe_config 1.0.3 archive
1.0.2 Apr 22, 2016 Go to the documentation of safe_config 1.0.2 Download safe_config 1.0.2 archive
1.0.1 Apr 22, 2016 Go to the documentation of safe_config 1.0.1 Download safe_config 1.0.1 archive

All 11 versions...

Analysis

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

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

  • completed on Feb 3, 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]
76 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
88

Platforms

Detected platforms: other

Primary library: package:safe_config/safe_config.dart with components: io, mirrors.

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 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 safe_config.dart.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
yaml >=2.1.8 <3.0.0 2.1.13
Transitive dependencies
charcode 1.1.1
collection 1.14.5
path 1.5.1
source_span 1.4.0
string_scanner 1.0.2
Dev dependencies
test >=0.12.0 <0.13.0