p-hatan/ mirror

pub repository

mirror.p-hatan.com

toml

0.1.1

Parser and encoder for TOML v0.3.1 (Tom's Obvious, Minimal Language) configuration files.

Download archive

Readme

toml.dart

This package provides an implementation of a TOML (Tom's Obvious, Minimal Language) parser and encoder for dart. It can be used with the dart_config library which allows you to dynamically load configuration files.

It currently supports version v0.3.1 of the TOML specification.

Installation

To get started add toml as a dependency to your pubspec.yaml and run the pub get command.

dependencies:
  toml: ">=0.1.0 <0.2.0"

Usage

With dart_config

If you want to use dart_config and your code is running in the browser add the following import directive:

import 'package:toml/browser.dart';

For command line and server side applications write:

import 'package:toml/server.dart';

Both imports define a loadConfig function which takes an optional path to the configuration file (defaults to 'config.toml') and returns a Future of the parsed document as an unmodifiable Map. They use the default ConfigHttpRequestLoader and ConfigFilesystemLoader, respectively.

loadConfig().then((Map document) {
  // ...
})

If you want to use a custom ConfigLoader add the following import instead:

import 'package:toml/config.dart';

There you will find the TomlConfigParser which implements the ConfigParser interface and can be used in conjunction with dart_configs Config class and your loader:

var config = new Config(filename,
  new MyConfigLoader(),
  new TomlConfigParser()
);
config.readConfig().then((Map document) {
  // ...
});

Without dart_config

If you don't want to use dart_config at all, add:

import 'package:toml/toml.dart';

This library contains the actual TomlParser class whose parse method takes a String and returns a Result object. The results value property holds an unmodifiable Map of the parsed document.

var toml = '''
  # ...
''';
var parser = new TomlParser();
var document = parser.parse(toml).value;

Encode

This package includes a TOML encoder. To use it simply import:

import 'package:toml/encoder.dart';

The library provides a TomlEncoder class whose encode method takes a Map and returns a TOML encoded String. All values of the map must be natively representable by TOML or implement the TomlEncodable interface.

var document = {
  // ...
};
var encoder = new TomlEncoder();
var toml = encoder.encode(document);

Clases which implement the TomlEncodable interface define a toToml method whose return value can be represented by TOML in turn.

Data Structure

TOML documents and tables are represented through nested UnmodifiableMapView objects whose keys are Strings and values dynamic read-only representations of the corresponding TOML value or sub-table. The contents of a table declared by:

[a.b.c]
key = 'value'

may be accessed using []:

var table = document['a']['b']['c']; // ok
var value = table['key'];

The following, however, is invalid:

var table = document['a.b.c']; // error
table['key'] = value; // error

All kinds of arrays including arrays of tables are stored as UnmodifiableListView objects. Though the encoder accepts any Iterable. The items of the list represent either a value or a table. Given a document:

[[items]]
name = 'A'

[[items]]
name = 'B'

[[items]]
name = 'C'

One might iterate over the items of the list:

document['items'].forEach((Map item) { # ok
  print(item.name);
});

But it is not allowed to add, remove or modify its entries:

document['items'].add({ # error
  'name': 'D'
});
document['items'][0] = { # error
  'name': 'E'
};

All string variants produce regular dart Strings. These are therefore all eqivalent:

str1 = "Hello World!"
str2 = 'Hello World!'
str3 = """
  Hello \
  World!\
"""
str4 = '''Hello World!'''

Integers are of type int and floating point numbers are represented as doubles. When compiled to JavaScript these two types are not distinct. Thus a float without decimal places might accidentally be encoded as an integer. This behavior would lead to the generation of invalid numeric arrays. The TomlEncoder addresses this issue by analyzing the contents of numeric arrays first. If any of its items cannot be represented as an integer all items will be encoded as floats instead. Encoding the following map:

var document = {
  'array': [1, 2.0, 3.141]
};

would throw an MixedArrayTypesError in the vm but yields this document when compiled to JavaScript:

array = [1.0, 2.0, 3.141]

Boolean values are obviously of type bool.

Datetime values are UTC DateTime objects.

Examples

Check out the scripts located in the '/examples' directory.

License

toml.dart is licensed under the MIT license agreement. See the LICENSE file for details.

Changelog

Changelog

0.1.1 / 2015-01-26

  • Fixed links and markdown.

0.1.0 / 2015-01-23

  • Initial version, implements v0.3.1 of the TOML spec.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add toml
pubspec.yaml
name: toml
version: 0.1.1
author: Justin Andresen <jan.dresen.95@gmail.com>
description: Parser and encoder for TOML v0.3.1 (Tom's Obvious, Minimal Language) configuration files.
homepage: https://github.com/just95/toml.dart
dependencies:
  dart_config: '>=0.5.0 <0.6.0'
  petitparser: '>=1.3.3 <2.0.0'
dev_dependencies:
  browser: any
  unittest: '>=0.11.5 <0.12.0'

Version history

VersionPublishedArchiveSHA-256
0.1.02015-01-23T14:48:22.639310ZDownloadfe943e7b640fdc8ade39d14110a99b51188ab41f89ce574aefe79da178745f75
0.1.12015-01-26T13:47:23.835010ZDownload0fbeebbe90be744bf1ae35871c8f6b640fb82b87cad92dd5064ade53c4b0f201
0.2.02015-02-12T21:52:07.949870ZDownload794b87401f2ca24e6564279bb636e833cdb9cdfe4a1fbabc0fff7ba1c9318e06
0.3.02015-04-08T12:59:06.990840ZDownload5d51223fa88af735bd5ce33bed30f005d57371a2c8800580f74d5ddea19bc5e6
0.4.02015-06-05T14:57:17.183ZDownload2679b939dc96886bd04e2a1a4be602bec634ceaa7e7d93cf2f2d2cb2f21f56f1
0.5.02016-09-24T20:51:54.767ZDownloadb503ca42ecb7cf198f9387546b2b7eab75a2ffc617631c7ccc9e4b85c1be77e9
0.5.12017-06-19T09:16:08.248223ZDownload003443c1ddca744af80bd1434393110d31e2e5cb29da321a2a1f5025fa06a362
0.6.02020-05-22T14:19:44.977152ZDownload1a883934f58fd75335774b5e1dd9b50ad519e1945b1141555dfd7f68d2bcfb52
0.6.12020-11-19T17:51:57.723072ZDownload8a7fe4155bf9df605ec14db22e8d5f9ba6b92f661f27270f6ddf06e664f61a4f
0.7.02021-01-17T01:27:50.546631ZDownloadd866f9e860393ebf08b37aa3a7d4203cc5f88ee0e09d43dcb174a7c48b8c1a3d
0.8.02021-01-29T20:37:29.598384ZDownloadf1f1d0d049e6ca20ee08164504bca7b5da7f8c5e2ea7060f2968106799637181
0.9.02021-03-07T09:40:55.824203ZDownloadc4b52a2fdbc5e909647d96212faa4e667f11744ab1e7b9a2ff4c0993277fd499
0.9.12021-03-07T14:35:46.955647ZDownload2a96184e183ca3fe26b0a605955d0b37c6ad1ca732f765e9570c0a16607a62f5
0.10.02021-03-07T16:20:49.018649ZDownload02575f98805450710672aac1c7afd3af561d8e762815ae5e5821cc899db911f9
0.11.02021-04-18T22:21:43.724006ZDownload585a8c04b3e85d6a2512217e9d76b4e9e8c0dd0f3054a4f6ccbed69260539e29
0.12.02021-09-07T11:11:50.949884ZDownload00172a9c2d8211953a90ef71e888d22aeabe4a6e3f69c6a5aeabf77851c88526
0.13.02022-05-30T00:23:44.817391ZDownload06ce5b8d97c37b5c09bbee32f0619b135cbe495e44ea76131f61237f830e20f1
0.13.12022-05-31T15:33:42.145627ZDownload69756bc12eccf279b72217a87310d217efc4b3752f722e890f672801f19ac485
0.14.02023-03-05T01:43:21.795252ZDownload157c5dca5160fced243f3ce984117f729c788bb5e475504f3dbcda881accee44
0.15.02023-12-07T00:15:11.761933ZDownload9968de24e45b632bf1a654fe1ac7b6fe5261c349243df83fd262397799c45a2d
0.16.02024-07-07T18:43:21.444176ZDownloadd968d149c8bd06dc14e09ea3a140f90a3f2ba71949e7a91df4a46f3107400e71
0.17.02025-10-21T22:43:46.595624ZDownload35cd2a1351c14bd213f130f8efcbd3e0c18181bff0c8ca7a08f6822a2bede786
0.18.02025-12-30T17:02:45.113288ZDownload35a35f782228656a2af31e8c73d1353cc4ef3d683fd68af1111b44631879c05e

Dependencies

dart_config "\u003e=0.5.0 \u003c0.6.0"

petitparser "\u003e=1.3.3 \u003c2.0.0"