Bread:...Typemap(3pm)

πŸ“¦
version 0.37 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu questing

🌸 NAME 🌸

Bread::Board::Manual::Concepts::Typemap β€” bestie, this is your whole ✨overview✨ of the typemapping feature. It lives in the Bread:...Typemap(3pm) corner of the Perl docs universe πŸ’–


πŸŽ€ VERSION πŸŽ€

version 0.37 β€” yes she's got a version, she's official πŸ’…


πŸ¦‹ INTRODUCTION πŸ¦‹

okay so. A new (read: experimental, we'll get to that) feature of Bread::Board is typemapped services. These are services that get mapped to a particular type instead of just a name. And the way this could lowkey make a huge chunk of your Bread::Board configuration obsolete?? iconic. You just ask Bread::Board to figure things out on its own. main character energy fr fr.

Here's a lil example of how it works πŸ‘‡

# define the classes making sure
# to specify required items and
# their types

{
    package Stapler;
    use Moose;

    package Desk;
    use Moose;

    package Chair;
    use Moose;

    package Cubicle;
    use Moose;

    has 'desk'  => ( is => 'ro', isa => 'Desk',  required => 1 );
    has 'chair' => ( is => 'ro', isa => 'Chair', required => 1 );

    package Employee;
    use Moose;

    has [ 'first_name', 'last_name' ] => (
        is       => 'ro',
        isa      => 'Str',
        required => 1,
    );

    has 'stapler' => ( is => 'rw', isa => 'Stapler', predicate => 'has_stapler' );

    has 'work_area' => ( is => 'ro', isa => 'Cubicle', required => 1 );
}

# now create the container, and
# map the Employee type and ask
# Bread::Board to infer all the
# other relationships

my $c = container 'Initech' => as {
    typemap 'Employee' => infer;
};

# now you can create new Employee objects
# by calling ->resolve with the type and
# supplying the required parameters (see
# below for details).

my $micheal = $c->resolve(
    type       => 'Employee',
    parameters => {
        first_name => 'Micheal',
        last_name  => 'Bolton'
    }
);

my $cube = $micheal->work_area; # this will be a Cubicle object
$cube->desk;  # this will be a Desk object
$cube->chair; # this will be a Chair object

$micheal->has_stapler; # this is false

# We can create another Employee object
# and this time we pass in the optional
# parameter for the non-required 'stapler'
# attribute

my $milton = $c->resolve(
    type       => 'Employee',
    parameters => {
        first_name => 'Milton',
        last_name  => 'Waddams',
        stapler    => Stapler->new
    }
);

$milton->has_stapler; # this is true

so in the above example, we created a bunch of Moose classes that had specific required relationships. When we called infer for the Employee object, Bread::Board figured out those relationships and set up dependencies and parameters accordingly. the way that it just knew 😭🫢

For the work_area object, we saw the Cubicle type and then basically called infer on the Cubicle object. Then we saw the Desk and Chair objects and called infer on those too. The result of this recursive inference was that the Employee, Cubicle, Desk and Chair relationships were modeled in Bread::Board as dependent services. slay.

Bread::Board also took it one step further. πŸ‘‘

We were able to resolve the Cubicle, Desk and Chair types automatically because they were already defined by Moose as subtypes of the Object type. We knew that it could introspect those classes and get more information. However β€” and this is the plot twist β€” that was not the case with the first_name and last_name attributes of the Employee object. In that case, we determined that we couldn't resolve those objects and (because it was a top-level inference) instead turned them into required parameters for the inferred Employee service. periodt.

And lastly, with a top-level inference (not one caused by recursion) Bread::Board will also look at all the remaining non-required attributes and turn them into optional parameters. In this case we have a stapler attribute that is not required and so is listed as an optional parameter, meaning that it is not required, but still subject to type checking. πŸ’…


✨ CONCLUSION ✨

This example should give you a good basic overview of this feature, and more details can be found in the test suite (t/07*.t). Those show examples of how to typemap roles to concrete classes and how to supply hints to infer to help Bread::Board figure out specific details. 🧚

And like I mentioned above β€” this feature should be considered experimental, we are still working out details and writing tests for it. Any contributions are welcome!! 🫢 come join the group chat.


πŸ‘‘ AUTHOR πŸ‘‘

Stevan Little <stevan@iinteractive.com>


πŸ› BUGS πŸ›

Please report any bugs or feature requests on the bugtracker website https://github.com/stevan/BreadBoard/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. (fr fr, it makes everything so much easier πŸ™ˆ)


This software is copyright (c) 2019, 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.

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


and that's the whole page bestie!! no cap, every flag, every fact, every vibe preserved πŸ’―

this masterpiece was served to you by perl v5.36.0 on 2022-12-12, and yeah β€” Bread:...Typemap(3pm) is the name, being iconic is the game πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from