<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use strict;
use warnings;

package CPAN::Meta::Merge;

our $VERSION = '2.150010';

use Carp qw/croak/;
use Scalar::Util qw/blessed/;
use CPAN::Meta::Converter 2.141170;

sub _is_identical {
  my ($left, $right) = @_;
  return
    (not defined $left and not defined $right)
    # if either of these are references, we compare the serialized value
    || (defined $left and defined $right and $left eq $right);
}

sub _identical {
  my ($left, $right, $path) = @_;
  croak sprintf "Can't merge attribute %s: '%s' does not equal '%s'", join('.', @{$path}), $left, $right
    unless _is_identical($left, $right);
  return $left;
}

sub _merge {
  my ($current, $next, $mergers, $path) = @_;
  for my $key (keys %{$next}) {
    if (not exists $current-&gt;{$key}) {
      $current-&gt;{$key} = $next-&gt;{$key};
    }
    elsif (my $merger = $mergers-&gt;{$key}) {
      $current-&gt;{$key} = $merger-&gt;($current-&gt;{$key}, $next-&gt;{$key}, [ @{$path}, $key ]);
    }
    elsif ($merger = $mergers-&gt;{':default'}) {
      $current-&gt;{$key} = $merger-&gt;($current-&gt;{$key}, $next-&gt;{$key}, [ @{$path}, $key ]);
    }
    else {
      croak sprintf "Can't merge unknown attribute '%s'", join '.', @{$path}, $key;
    }
  }
  return $current;
}

sub _uniq {
  my %seen = ();
  return grep { not $seen{$_}++ } @_;
}

sub _set_addition {
  my ($left, $right) = @_;
  return [ +_uniq(@{$left}, @{$right}) ];
}

sub _uniq_map {
  my ($left, $right, $path) = @_;
  for my $key (keys %{$right}) {
    if (not exists $left-&gt;{$key}) {
      $left-&gt;{$key} = $right-&gt;{$key};
    }
    # identical strings or references are merged identically
    elsif (_is_identical($left-&gt;{$key}, $right-&gt;{$key})) {
      1; # do nothing - keep left
    }
    elsif (ref $left-&gt;{$key} eq 'HASH' and ref $right-&gt;{$key} eq 'HASH') {
      $left-&gt;{$key} = _uniq_map($left-&gt;{$key}, $right-&gt;{$key}, [ @{$path}, $key ]);
    }
    else {
      croak 'Duplication of element ' . join '.', @{$path}, $key;
    }
  }
  return $left;
}

sub _improvise {
  my ($left, $right, $path) = @_;
  my ($name) = reverse @{$path};
  if ($name =~ /^x_/) {
    if (ref($left) eq 'ARRAY') {
      return _set_addition($left, $right, $path);
    }
    elsif (ref($left) eq 'HASH') {
      return _uniq_map($left, $right, $path);
    }
    else {
      return _identical($left, $right, $path);
    }
  }
  croak sprintf "Can't merge '%s'", join '.', @{$path};
}

sub _optional_features {
  my ($left, $right, $path) = @_;

  for my $key (keys %{$right}) {
    if (not exists $left-&gt;{$key}) {
      $left-&gt;{$key} = $right-&gt;{$key};
    }
    else {
      for my $subkey (keys %{ $right-&gt;{$key} }) {
        next if $subkey eq 'prereqs';
        if (not exists $left-&gt;{$key}{$subkey}) {
          $left-&gt;{$key}{$subkey} = $right-&gt;{$key}{$subkey};
        }
        else {
          Carp::croak "Cannot merge two optional_features named '$key' with different '$subkey' values"
            if do { no warnings 'uninitialized'; $left-&gt;{$key}{$subkey} ne $right-&gt;{$key}{$subkey} };
        }
      }

      require CPAN::Meta::Prereqs;
      $left-&gt;{$key}{prereqs} =
        CPAN::Meta::Prereqs-&gt;new($left-&gt;{$key}{prereqs})
          -&gt;with_merged_prereqs(CPAN::Meta::Prereqs-&gt;new($right-&gt;{$key}{prereqs}))
          -&gt;as_string_hash;
    }
  }
  return $left;
}


my %default = (
  abstract       =&gt; \&amp;_identical,
  author         =&gt; \&amp;_set_addition,
  dynamic_config =&gt; sub {
    my ($left, $right) = @_;
    return $left || $right;
  },
  generated_by =&gt; sub {
    my ($left, $right) = @_;
    return join ', ', _uniq(split(/, /, $left), split(/, /, $right));
  },
  license     =&gt; \&amp;_set_addition,
  'meta-spec' =&gt; {
    version =&gt; \&amp;_identical,
    url     =&gt; \&amp;_identical
  },
  name              =&gt; \&amp;_identical,
  release_status    =&gt; \&amp;_identical,
  version           =&gt; \&amp;_identical,
  description       =&gt; \&amp;_identical,
  keywords          =&gt; \&amp;_set_addition,
  no_index          =&gt; { map { ($_ =&gt; \&amp;_set_addition) } qw/file directory package namespace/ },
  optional_features =&gt; \&amp;_optional_features,
  prereqs           =&gt; sub {
    require CPAN::Meta::Prereqs;
    my ($left, $right) = map { CPAN::Meta::Prereqs-&gt;new($_) } @_[0,1];
    return $left-&gt;with_merged_prereqs($right)-&gt;as_string_hash;
  },
  provides  =&gt; \&amp;_uniq_map,
  resources =&gt; {
    license    =&gt; \&amp;_set_addition,
    homepage   =&gt; \&amp;_identical,
    bugtracker =&gt; \&amp;_uniq_map,
    repository =&gt; \&amp;_uniq_map,
    ':default' =&gt; \&amp;_improvise,
  },
  ':default' =&gt; \&amp;_improvise,
);

sub new {
  my ($class, %arguments) = @_;
  croak 'default version required' if not exists $arguments{default_version};
  my %mapping = %default;
  my %extra = %{ $arguments{extra_mappings} || {} };
  for my $key (keys %extra) {
    if (ref($mapping{$key}) eq 'HASH') {
      $mapping{$key} = { %{ $mapping{$key} }, %{ $extra{$key} } };
    }
    else {
      $mapping{$key} = $extra{$key};
    }
  }
  return bless {
    default_version =&gt; $arguments{default_version},
    mapping =&gt; _coerce_mapping(\%mapping, []),
  }, $class;
}

my %coderef_for = (
  set_addition =&gt; \&amp;_set_addition,
  uniq_map     =&gt; \&amp;_uniq_map,
  identical    =&gt; \&amp;_identical,
  improvise    =&gt; \&amp;_improvise,
  improvize    =&gt; \&amp;_improvise, # [sic] for backwards compatibility
);

sub _coerce_mapping {
  my ($orig, $map_path) = @_;
  my %ret;
  for my $key (keys %{$orig}) {
    my $value = $orig-&gt;{$key};
    if (ref($orig-&gt;{$key}) eq 'CODE') {
      $ret{$key} = $value;
    }
    elsif (ref($value) eq 'HASH') {
      my $mapping = _coerce_mapping($value, [ @{$map_path}, $key ]);
      $ret{$key} = sub {
        my ($left, $right, $path) = @_;
        return _merge($left, $right, $mapping, [ @{$path} ]);
      };
    }
    elsif ($coderef_for{$value}) {
      $ret{$key} = $coderef_for{$value};
    }
    else {
      croak "Don't know what to do with " . join '.', @{$map_path}, $key;
    }
  }
  return \%ret;
}

sub merge {
  my ($self, @items) = @_;
  my $current = {};
  for my $next (@items) {
    if ( blessed($next) &amp;&amp; $next-&gt;isa('CPAN::Meta') ) {
      $next = $next-&gt;as_struct;
    }
    elsif ( ref($next) eq 'HASH' ) {
      my $cmc = CPAN::Meta::Converter-&gt;new(
        $next, default_version =&gt; $self-&gt;{default_version}
      );
      $next = $cmc-&gt;upgrade_fragment;
    }
    else {
      croak "Don't know how to merge '$next'";
    }
    $current = _merge($current, $next, $self-&gt;{mapping}, []);
  }
  return $current;
}

1;

# ABSTRACT: Merging CPAN Meta fragments


# vim: ts=2 sts=2 sw=2 et :

__END__

=pod

=encoding UTF-8

=head1 NAME

CPAN::Meta::Merge - Merging CPAN Meta fragments

=head1 VERSION

version 2.150010

=head1 SYNOPSIS

 my $merger = CPAN::Meta::Merge-&gt;new(default_version =&gt; "2");
 my $meta = $merger-&gt;merge($base, @additional);

=head1 DESCRIPTION

=head1 METHODS

=head2 new

This creates a CPAN::Meta::Merge object. It takes one mandatory named
argument, C&lt;version&gt;, declaring the version of the meta-spec that must be
used for the merge. It can optionally take an C&lt;extra_mappings&gt; argument
that allows one to add additional merging functions for specific elements.

The C&lt;extra_mappings&gt; arguments takes a hash ref with the same type of
structure as described in L&lt;CPAN::Meta::Spec&gt;, except with its values as
one of the L&lt;defined merge strategies|/"MERGE STRATEGIES"&gt; or a code ref
to a merging function.

  my $merger = CPAN::Meta::Merge-&gt;new(
      default_version =&gt; '2',
      extra_mappings =&gt; {
          'optional_features' =&gt; \&amp;custom_merge_function,
          'x_custom' =&gt; 'set_addition',
          'x_meta_meta' =&gt; {
              name =&gt; 'identical',
              tags =&gt; 'set_addition',
          }
      }
  );

=head2 merge(@fragments)

Merge all C&lt;@fragments&gt; together. It will accept both CPAN::Meta objects and
(possibly incomplete) hashrefs of metadata.

=head1 MERGE STRATEGIES

C&lt;merge&gt; uses various strategies to combine different elements of the CPAN::Meta objects.  The following strategies can be used with the extra_mappings argument of C&lt;new&gt;:

=over

=item identical

The elements must be identical

=item set_addition

The union of two array refs

  [ a, b ] U [ a, c]  = [ a, b, c ]

=item uniq_map

Key value pairs from the right hash are merged to the left hash.  Key
collisions are only allowed if their values are the same.  This merge
function will recurse into nested hash refs following the same merge
rules.

=item improvise

This merge strategy will try to pick the appropriate predefined strategy
based on what element type.  Array refs will try to use the
C&lt;set_addition&gt; strategy,  Hash refs will try to use the C&lt;uniq_map&gt;
strategy, and everything else will try the C&lt;identical&gt; strategy.

=back

=head1 AUTHORS

=over 4

=item *

David Golden &lt;dagolden@cpan.org&gt;

=item *

Ricardo Signes &lt;rjbs@cpan.org&gt;

=item *

Adam Kennedy &lt;adamk@cpan.org&gt;

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by David Golden, Ricardo Signes, Adam Kennedy and Contributors.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
</pre></body></html>