From cac9a5f9e429d2461bd2830bc983eced5ef14e1a Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 07:53:05 +0200 Subject: Make .read() merge the hashes and allow an array of paths to load --- META6.json | 2 +- lib/Config.pm6 | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/META6.json b/META6.json index a3614c3..cfb38a3 100644 --- a/META6.json +++ b/META6.json @@ -6,7 +6,7 @@ "description": "Extensible library for reading and writing configuration files.", "license": "GPL-3.0", "depends": [ - + "Hash::Merge" ], "provides": { "Config": "lib/Config.pm6", diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 2fa01bf..372eb3b 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -2,6 +2,8 @@ use v6.c; +use Hash::Merge; + use Config::Exception::MissingParserException; use Config::Exception::UnknownTypeException; use Config::Exception::FileNotFoundException; @@ -109,15 +111,23 @@ class Config is export } require ::($!parser); - $!content = ::($!parser).read($path); + + $!content.merge(::($!parser).read($path)); } return True; } + multi method read(Array $paths, Str $parser = "") + { + for $paths -> $path { + self.read($path, $parser); + } + } + multi method read(Hash $hash) { - $!content = $hash; + $!content.merge($hash); } method set(Str $key, Any $value) -- cgit v1.1 From fd33799e2737e1fcce7b9bad44d0031f8dc9a2c9 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 07:53:25 +0200 Subject: Add a .clear() to clear the config --- lib/Config.pm6 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 372eb3b..d07c640 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -16,6 +16,13 @@ class Config is export has $!path; has $!parser; + method clear() + { + $!content = {}; + $!path = ""; + $!parser = ""; + } + multi method get(Str $key, Any $default = Nil) { my $index = $!content; -- cgit v1.1 From 5de74ad36feede030f0400c0231bf0eaa3c4ea3e Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:00:47 +0200 Subject: Add some additional comments --- lib/Config.pm6 | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index d07c640..97d708c 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -12,10 +12,11 @@ use Config::Parser; class Config is export { - has $!content = {}; - has $!path; - has $!parser; + has Hash $!content = {}; + has Str $!path; + has Str $!parser; + #| Clear the config. method clear() { $!content = {}; @@ -23,6 +24,8 @@ class Config is export $!parser = ""; } + #| Get a value from the config object. To get a nested + #| key, use a . to descent a level. multi method get(Str $key, Any $default = Nil) { my $index = $!content; @@ -36,6 +39,8 @@ class Config is export $index; } + #| Get a value from the config object using an array + #| to indicate the nested key to get. multi method get(@keyparts, Any $default = Nil) { my $index = $!content; @@ -49,6 +54,8 @@ class Config is export $index; } + #| Get the name of the parser module to use for the + #| given path. method get-parser(Str $path, Str $parser = "") { if ($parser ne "") { @@ -64,6 +71,7 @@ class Config is export "Config::Parser::" ~ $type; } + #| Get the type of parser required for the given path. method get-parser-type(Str $path) { given ($path) { @@ -83,6 +91,7 @@ class Config is export return Config::Type::unknown; } + #| Check wether a given key exists. method has(Str $key) { my $index = $!content; @@ -95,11 +104,20 @@ class Config is export True; } + #| Reload the configuration. Requires the configuration to + #| have been loaded from a file. multi method read() { + if ($!path eq "") { + return False; + } + return self.load($!path); } + #| Load a configuration file from the given path. Optionally + #| set a parser module name to use. If not set, Config will + #| attempt to deduce the parser to use. multi method read(Str $path, Str $parser = "") { Config::Exception::FileNotFoundException.new( @@ -119,24 +137,32 @@ class Config is export require ::($!parser); - $!content.merge(::($!parser).read($path)); + self.read(::($!parser).read($path)); } return True; } + #| Read an array of paths. Will fail on the first file that + #| fails to load for whatever reason. multi method read(Array $paths, Str $parser = "") { for $paths -> $path { self.read($path, $parser); } + + return True; } + #| Read a plain Hash into the configuration. multi method read(Hash $hash) { $!content.merge($hash); + + return True; } + #| Set a single key to a given value; method set(Str $key, Any $value) { my $index := $!content; @@ -152,6 +178,9 @@ class Config is export self; } + #| Write the current configuration to the given path. If + #| no parser is given, it tries to use the parser that + #| was used when loading the configuration. method write(Str $path, Str $parser = "") { $parser = self.get-parser($path, $parser); -- cgit v1.1 From 632f695dc8726e3c63dac004a148aac7c95b1191 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:05:14 +0200 Subject: Add a multi method has() that accepts an array param --- lib/Config.pm6 | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 97d708c..2369b6f 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -28,24 +28,16 @@ class Config is export #| key, use a . to descent a level. multi method get(Str $key, Any $default = Nil) { - my $index = $!content; - - for $key.split(".") -> $part { - return $default unless defined($index{$part}); - - $index = $index{$part}; - } - - $index; + self.get($key.split("."), $default); } #| Get a value from the config object using an array #| to indicate the nested key to get. - multi method get(@keyparts, Any $default = Nil) + multi method get(Array $keyparts, Any $default = Nil) { my $index = $!content; - for @keyparts -> $part { + for $keyparts -> $part { return $default unless defined($index{$part}); $index = $index{$part}; @@ -92,16 +84,21 @@ class Config is export } #| Check wether a given key exists. - method has(Str $key) { + multi method has(Str $key) { + self.has($key.split(".")); + } + + #| Check wether a given key exists using an array to supply + #| the nested key to check. + multi method has(Array $keyparts) + { my $index = $!content; - for $key.split(".") -> $part { + for $keyparts -> $part { return False unless defined($index{$part}); $index = $index{$part}; } - - True; } #| Reload the configuration. Requires the configuration to -- cgit v1.1 From b23ea52e22e0fcebcaf4af0d39a4e2e07e3910a9 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:07:16 +0200 Subject: Version bump before I forget --- META6.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/META6.json b/META6.json index cfb38a3..ccf33e7 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "perl": "6", "name": "Config", - "version": "1.0.0", + "version": "1.1.0", "auth": "github:scriptkitties", "description": "Extensible library for reading and writing configuration files.", "license": "GPL-3.0", -- cgit v1.1 From 348a75d03fee516bb0dc0854b20336a40dd59b74 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:09:05 +0200 Subject: Add a multi method set() that accepts an array for the key --- lib/Config.pm6 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 2369b6f..1d0d7db 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -160,11 +160,16 @@ class Config is export } #| Set a single key to a given value; - method set(Str $key, Any $value) + multi method set(Str $key, Any $value) + { + self.set($key.split("."), $value); + } + + multi method set(Array $keyparts, Any $value) { my $index := $!content; - for $key.split(".") -> $part { + for $keyparts -> $part { $index{$part} = {} unless defined($index{$part}); $index := $index{$part}; -- cgit v1.1 From 8cc5724b273133ce645589b7c7e0cacd60276360 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:10:33 +0200 Subject: Initialize path and parser with empty string --- lib/Config.pm6 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 1d0d7db..ae78ea1 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -13,8 +13,8 @@ use Config::Parser; class Config is export { has Hash $!content = {}; - has Str $!path; - has Str $!parser; + has Str $!path = ""; + has Str $!parser = ""; #| Clear the config. method clear() -- cgit v1.1 From fb014751bbcf785b4e43b9f622ab8114d496be09 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:17:33 +0200 Subject: Update test cases --- t/having.t | 2 ++ t/reading.t | 25 +++++++++++++++++++++++++ t/setting.t | 2 ++ 3 files changed, 29 insertions(+) diff --git a/t/having.t b/t/having.t index 3be2935..7c3a67e 100644 --- a/t/having.t +++ b/t/having.t @@ -19,3 +19,5 @@ $config.read({ ok $config.has("a"), "Check existence of simple key"; ok $config.has("b.c"), "Check existence of nested key"; +ok $config.has(["a"]), "Check existence of simple key using array"; +ok $config.has(["b", "c"]), "Check existence of nested key using array"; diff --git a/t/reading.t b/t/reading.t index 52859af..ec1c40b 100644 --- a/t/reading.t +++ b/t/reading.t @@ -13,3 +13,28 @@ my $config = Config.new(); throws-like { $config.read("nonexistant-config") }, Config::Exception::FileNotFoundException, "Reading nonexisting file"; throws-like { $config.read("t/test-stub") }, Config::Exception::UnknownTypeException, "Reading file of unknown type"; throws-like { $config.read("t/test-stub", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser"; + +my $hash = { + "a" => + "b" => { + "c" => "test" + } +}; + +$config.read($hash); + +is-deeply $config.get(), $hash, "Correctly sets hash"; + +$config.read({ + "b" => { + "d" => "another" + } +}) + +is-deeply $config.get(), { + "a" => + "b" => { + "c" => "test", + "d" => "another" + } +}, "Correctly merges new hash into existing config"; diff --git a/t/setting.t b/t/setting.t index d9f0854..18790cf 100644 --- a/t/setting.t +++ b/t/setting.t @@ -12,3 +12,5 @@ my $config = Config.new(); ok $config.set("a", "test").get("a") eq "test", "Setting simple key"; ok $config.set("b.c", "test").get("b.c") eq "test", "Setting nested key"; +ok $config.set(["d"], "test").get("d") eq "test", "Setting simple key using array"; +ok $config.set(["e", "f"], "test").get("e.f") eq "test", "Setting nested key using array"; -- cgit v1.1 From 070e5a9e406fee7eeeb48481a048e79fb4d531a6 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:18:45 +0200 Subject: Add a get without params to get the entire config hash --- lib/Config.pm6 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index ae78ea1..b9bc582 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -24,6 +24,12 @@ class Config is export $!parser = ""; } + #| Return the entire config hash. + multi method get() + { + return $!content; + } + #| Get a value from the config object. To get a nested #| key, use a . to descent a level. multi method get(Str $key, Any $default = Nil) -- cgit v1.1 From 37c38cd5771a3aa6eb7b203f8d38ad3cc46faac8 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:19:53 +0200 Subject: Update test plans --- t/having.t | 2 +- t/reading.t | 2 +- t/setting.t | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/t/having.t b/t/having.t index 7c3a67e..0627ed5 100644 --- a/t/having.t +++ b/t/having.t @@ -4,7 +4,7 @@ use v6.c; use Test; use lib "lib"; -plan 2; +plan 4; use Config; diff --git a/t/reading.t b/t/reading.t index ec1c40b..79d2359 100644 --- a/t/reading.t +++ b/t/reading.t @@ -4,7 +4,7 @@ use v6.c; use Test; use lib "lib"; -plan 3; +plan 5; use Config; diff --git a/t/setting.t b/t/setting.t index 18790cf..7e3a457 100644 --- a/t/setting.t +++ b/t/setting.t @@ -4,7 +4,7 @@ use v6.c; use Test; use lib "lib"; -plan 2; +plan 4; use Config; -- cgit v1.1 From 3f4b16f712aa4b5c610319c79b7e1d7d04140503 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 08:59:30 +0200 Subject: Fix the easy bugs --- lib/Config.pm6 | 30 ++++++++++++++++-------------- t/reading.t | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index b9bc582..22cd2a3 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -34,16 +34,16 @@ class Config is export #| key, use a . to descent a level. multi method get(Str $key, Any $default = Nil) { - self.get($key.split("."), $default); + self.get($key.split(".").list, $default); } - #| Get a value from the config object using an array + #| Get a value from the config object using a list #| to indicate the nested key to get. - multi method get(Array $keyparts, Any $default = Nil) + multi method get(List $keyparts, Any $default = Nil) { my $index = $!content; - for $keyparts -> $part { + for $keyparts.list -> $part { return $default unless defined($index{$part}); $index = $index{$part}; @@ -91,20 +91,22 @@ class Config is export #| Check wether a given key exists. multi method has(Str $key) { - self.has($key.split(".")); + self.has($key.split(".").list); } - #| Check wether a given key exists using an array to supply + #| Check wether a given key exists using a list to supply #| the nested key to check. - multi method has(Array $keyparts) + multi method has(List $keyparts) { my $index = $!content; - for $keyparts -> $part { + for $keyparts.list -> $part { return False unless defined($index{$part}); $index = $index{$part}; } + + defined($index); } #| Reload the configuration. Requires the configuration to @@ -146,11 +148,11 @@ class Config is export return True; } - #| Read an array of paths. Will fail on the first file that + #| Read a list of paths. Will fail on the first file that #| fails to load for whatever reason. - multi method read(Array $paths, Str $parser = "") + multi method read(List $paths, Str $parser = "") { - for $paths -> $path { + for $paths.list -> $path { self.read($path, $parser); } @@ -168,14 +170,14 @@ class Config is export #| Set a single key to a given value; multi method set(Str $key, Any $value) { - self.set($key.split("."), $value); + self.set($key.split(".").list, $value); } - multi method set(Array $keyparts, Any $value) + multi method set(List $keyparts, Any $value) { my $index := $!content; - for $keyparts -> $part { + for $keyparts.list -> $part { $index{$part} = {} unless defined($index{$part}); $index := $index{$part}; diff --git a/t/reading.t b/t/reading.t index 79d2359..1c3a120 100644 --- a/t/reading.t +++ b/t/reading.t @@ -29,7 +29,7 @@ $config.read({ "b" => { "d" => "another" } -}) +}); is-deeply $config.get(), { "a" => -- cgit v1.1 From 2fa145223277129586ae290edbd5362d6904507d Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 09:03:35 +0200 Subject: Fix thrown exception --- lib/Config.pm6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 22cd2a3..40bfa27 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -63,7 +63,7 @@ class Config is export my $type = self.get-parser-type($path); Config::Exception::UnknownTypeException.new( - type => $type + file => $path ).throw() if $type eq Config::Type::unknown; "Config::Parser::" ~ $type; -- cgit v1.1 From 7313624f98295bb80416009cf21012cec10bdc15 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 26 Apr 2017 09:48:28 +0200 Subject: I fucked up the test --- t/reading.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/reading.t b/t/reading.t index 1c3a120..e43252a 100644 --- a/t/reading.t +++ b/t/reading.t @@ -15,7 +15,7 @@ throws-like { $config.read("t/test-stub") }, Config::Exception::UnknownTypeExcep throws-like { $config.read("t/test-stub", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser"; my $hash = { - "a" => + "a" => "a", "b" => { "c" => "test" } @@ -32,7 +32,7 @@ $config.read({ }); is-deeply $config.get(), { - "a" => + "a" => "a", "b" => { "c" => "test", "d" => "another" -- cgit v1.1