Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
Added dict interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobconley committed Sep 19, 2019
1 parent 4860c17 commit d8b9a34
Show file tree
Hide file tree
Showing 321 changed files with 21,431 additions and 39 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ project.*
.vscode

tests/tmp.hack
tests/test.toml

vendor/
composer.lock
tests/test.toml
1 change: 1 addition & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored_paths = [ 'vendor/bin/.+' ]
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This small package provides an interface for reading [TOML](https://github.com/t
Usage
====

All TOML decoders return `dict<string, nonnull>`. Canonically, a TOML document is an unordered list of key/value pairs.
All TOML decoders return `dict<string, nonnull>`. Canonically, a TOML document is an unordered list of key/value pairs.

For convenience, two methods are added to the global namespace -

Expand All @@ -19,6 +19,24 @@ For convenience, two methods are added to the global namespace -
A stream object can also be decoded directly if needed by using `(new toml\Decoder())->DecodeStream(...)`.


DictionaryAccess object
-----

`dict<string,nonnull>` was chosen as the return type instead of `array` in light of the hhvm team's [previous stance on the matter](https://hhvm.com/blog/10649/improving-arrays-in-hack) and the fact that, since they broke PHP backwards compatibility, they likely won't continue to support a loose-typed array.

This also makes sense for security as TOML is largely designed to be a configuration format and the developer using this package should know at all times what the keys and data types are that they're reading.

For type safety and convenience, the `DictionaryAccess` object is provided. It wraps around the `dict<string, nonnull>` object, providing the following methods:

- `->exists(string $key) : bool` - Returns true if the given key has a value
- `->get(string $key) : nonnull` - Accesses the wrapped dictionary straight-up; equivalent to using the array access operator (`[$name]`)

- `->int(string $key) : int`, `->bool(string $key) : bool`, etc - Returns the value at $key as the appropriate type, and throws an exception if the value is unset or the wrong type
- `->intlist(string $key) : vec<int>`, etc - Returns a vec corresponding to the given key. Like the above, but with a vec
- `->dict(string $key) : DictionaryAccess` - Get a child dictionary like this one
- `->dictlist(string $key) : vec<DictionaryAccess>`


Testing
===

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"name": "jacobconley/hack-toml",
"description": "A TOML library for the Hack language",

"require": {
"hhvm/hsl": "^4.5",
"hhvm/hhvm-autoload": "^2.0"
},
"require-dev": {
"hhvm/hacktest": "^1.6",
"facebook/fbexpect": "^2.6"
}
}
325 changes: 325 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d8b9a34

Please sign in to comment.