aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-11-12 20:36:08 +0100
committerPatrick Spek <p.spek@tyil.nl>2020-11-12 20:36:08 +0100
commit5849c96fa6a058a8e3d79a17e0de845ab4d03fc8 (patch)
tree0538fa597f84e28335f6d7ddf2ba03f9edf0a7c9 /lib
parent95e89dee79200e1ea75b1ef976a30bf6b61e23a7 (diff)
Rewrite module
Diffstat (limited to 'lib')
-rw-r--r--lib/Hash/Merge.pm6102
-rw-r--r--lib/Hash/Merge/Augment.pm673
2 files changed, 81 insertions, 94 deletions
diff --git a/lib/Hash/Merge.pm6 b/lib/Hash/Merge.pm6
index 607e17b..7bc5bab 100644
--- a/lib/Hash/Merge.pm6
+++ b/lib/Hash/Merge.pm6
@@ -1,60 +1,76 @@
#! /usr/bin/env false
-use v6.c;
+use v6.d;
unit module Hash::Merge;
#| Merge any number of Hashes together.
-sub merge-hashes(
- *@hashes, #= Hashes to merge together
- --> Hash
+sub merge-hashes (
+ *@hashes, #= Hashes to merge together
+ --> Hash
) is export {
- my %merge-into = @hashes.shift;
+ my %merge-into = @hashes.shift;
- # Nothing to do if we only got 1 argument
- return %merge-into unless @hashes.elems;
+ # Nothing to do if we only got 1 argument
+ return %merge-into unless @hashes.elems;
- for ^@hashes.elems {
- %merge-into = merge-hash(%merge-into, @hashes.shift);
- }
+ for ^@hashes.elems {
+ %merge-into = merge-hash(%merge-into, @hashes.shift);
+ }
- %merge-into;
+ %merge-into;
}
#| Merge two hashes together.
-sub merge-hash(
- %merge-into, #= The original Hash that should be merged into.
- %merge-source, #= Another Hash to merge into the original Hash.
- Bool:D :$no-append-array = False,
- --> Hash
+sub merge-hash (
+ #| The original Hash to merge the second Hash into.
+ %first,
+
+ #| The second hash, which will be merged into the first Hash.
+ %second,
+
+ #| Boolean to set whether Associative objects should be merged on their
+ #| own. When set to False, Associative objects in %second will
+ #| overwrite those from %first.
+ Bool:D :$deep = True,
+
+ #| Boolean to set whether Positional objects should be appended. When
+ #| set to False, Positional objects in %second will overwrite those
+ #| from %first.
+ Bool:D :$positional-append = True,
+
+ --> Hash
) is export {
- for %merge-source.keys -> $key {
- if %merge-into{$key}:exists {
- given %merge-source{$key} {
- when Hash {
- merge-hash(%merge-into{$key}, %merge-source{$key}, :$no-append-array);
- }
- when Positional {
- %merge-into{$key} = $no-append-array
- ?? %merge-source{$key}
- !!
- do {
- my @a;
- @a.push: $_ for %merge-into{$key}.list;
- @a.push: $_ for %merge-source{$key}.list;
- @a;
- }
- }
- default {
- %merge-into{$key} = %merge-source{$key}
- }
- }
- } else {
- %merge-into{$key} = %merge-source{$key};
- }
- }
-
- %merge-into;
+ my %result = %first;
+
+ for %second.keys -> $key {
+ # If the key doesn't exist yet in %first, it can be inserted without worry.
+ if (%first{$key}:!exists) {
+ %result{$key} = %second{$key};
+ next;
+ }
+
+ given (%first{$key}) {
+ # Associative objects need to be merged deeply.
+ when Associative {
+ %result{$key} = $deep
+ ?? merge-hash(%first{$key}, %second{$key}, :$deep, :$positional-append)
+ !! %second{$key}
+ }
+ # Positional objects can be merged or overwritten depending on $append-array.
+ when Positional {
+ %result{$key} = $positional-append
+ ?? (|%first{$key}, |%second{$key})
+ !! %second{$key}
+ }
+ # Anything else will just overwrite.
+ default {
+ %result{$key} = %second{$key};
+ }
+ }
+ }
+
+ %result;
}
# vim: ft=perl6 ts=4 sw=4 et
diff --git a/lib/Hash/Merge/Augment.pm6 b/lib/Hash/Merge/Augment.pm6
index b256b8e..3a8cde6 100644
--- a/lib/Hash/Merge/Augment.pm6
+++ b/lib/Hash/Merge/Augment.pm6
@@ -3,61 +3,32 @@
use v6.c;
use MONKEY-TYPING;
+use Hash::Merge;
+use X::Hash::Merge::TypeObject;
+
# Don't use precompilation in order to not conflict with other MONKEY-TYPING
# modules.
no precompilation;
-augment class Hash
-{
- #| Merges a second hash into the hash the method is called on. Hash given as
- #| the argument is not modified.
- #| Traverses the full tree, replacing items in the original hash with the
- #| hash given in the argument. Does not replace positional elements by default,
- #| and instead appends the items from the supplied hash's array to the original
- #| hash's array. The object type of positionals is not retained and instead
- #| becomes an Array type.
- #| Use :no-append-array to replace arrays and positionals instead, which will
- #| also retain the original type and not convert to an Array
- multi method merge (Hash:U: %b, Bool:D :$no-append-array = False) {
- warn "Cannot merge an undefined Hash!";
- return %b;
- }
-
- multi method merge (Hash:D: %b, Bool:D :$no-append-array = False)
- {
- hashmerge self, %b, :$no-append-array;
- }
-
- sub hashmerge (%merge-into, %merge-source, Bool:D :$no-append-array)
- {
- for %merge-source.keys -> $key {
- if %merge-into{$key}:exists {
- given %merge-source{$key} {
- when Hash {
- hashmerge %merge-into{$key},
- %merge-source{$key},
- :$no-append-array;
- }
- when Positional {
- %merge-into{$key} = $no-append-array
- ?? %merge-source{$key}
- !!
- do {
- my @a;
- @a.push: $_ for %merge-into{$key}.list;
- @a.push: $_ for %merge-source{$key}.list;
- @a;
- }
- }
- # Non-positionals, so strings or Bools or whatever
- default { %merge-into{$key} = %merge-source{$key} }
- }
- } else {
- %merge-into{$key} = %merge-source{$key};
- }
- }
- %merge-into;
- }
+augment class Hash {
+ method merge (
+ Hash:D:
+
+ #| The Hash to merge into this one.
+ %hash,
+
+ #| Boolean to set whether Associative objects should be merged on their
+ #| own. When set to False, Associative objects in %second will
+ #| overwrite those from %first.
+ Bool:D :$deep = True,
+
+ #| Boolean to set whether Positional objects should be appended. When
+ #| set to False, Positional objects in %second will overwrite those
+ #| from %first.
+ Bool:D :$positional-append = True,
+ ) {
+ self = merge-hash(self, %hash, :$deep, :$positional-append);
+ }
}
# vim: ft=perl6 ts=4 sw=4 et