NAME
Toolforge::MixNMatch::Struct::Catalog - Mix'n'match catalog structure serialization.
SYNOPSIS
use Toolforge::MixNMatch::Struct::Catalog qw(obj2struct struct2obj);
my $struct_hr = obj2struct($obj);
my $obj = struct2obj($struct_hr);
DESCRIPTION
This conversion is between object defined in Toolforge::MixNMatch::Object::Catalog and structure serialized via JSON to Mix'n'match application.
SUBROUTINES
obj2struct
my $struct_hr = obj2struct($obj);
Convert Toolforge::MixNMatch::Object::Catalog instance to structure.
Returns reference to hash with structure.
struct2obj
my $obj = struct2obj($struct_hr);
Convert structure of time to object.
Returns Toolforge::MixNMatch::Object::Catalog instance.
ERRORS
obj2struct():
Object doesn't exist.
Object isn't 'Toolforge::MixNMatch::Object::Catalog'.
EXAMPLE1
use strict;
use warnings;
use Data::Printer;
use Toolforge::MixNMatch::Object::Catalog;
use Toolforge::MixNMatch::Struct::Catalog qw(obj2struct);
# Object.
my $obj = Toolforge::MixNMatch::Object::Catalog->new(
'count' => 10,
'type' => 'Q5',
'users' => [
Toolforge::MixNMatch::Object::User->new(
'count' => 6,
'uid' => 1,
'username' => 'Skim',
),
Toolforge::MixNMatch::Object::User->new(
'count' => 4,
'uid' => 2,
'username' => 'Foo',
),
],
'year_months' => [
Toolforge::MixNMatch::Object::YearMonth->new(
'count' => 2,
'month' => 9,
'year' => 2020,
),
Toolforge::MixNMatch::Object::YearMonth->new(
'count' => 8,
'month' => 10,
'year' => 2020,
),
],
);
# Get structure.
my $struct_hr = obj2struct($obj);
# Dump to output.
p $struct_hr;
# Output:
# \ {
# type [
# [0] {
# cnt 10,
# type "Q5"
# }
# ],
# user [
# [0] {
# cnt 6,
# uid 1,
# username "Skim"
# },
# [1] {
# cnt 4,
# uid 2,
# username "Foo"
# }
# ],
# ym [
# [0] {
# cnt 2,
# ym 202009
# },
# [1] {
# cnt 8,
# ym 202010
# }
# ]
# }
EXAMPLE2
use strict;
use warnings;
use Toolforge::MixNMatch::Struct::Catalog qw(struct2obj);
# Time structure.
my $struct_hr = {
'user' => [{
'cnt' => 6,
'uid' => 1,
'username' => 'Skim',
}, {
'cnt' => 4,
'uid' => 2,
'username' => 'Foo',
}],
'type' => [{
'cnt' => 10,
'type' => 'Q5',
}],
'ym' => [{
'cnt' => 2,
'ym' => 202009,
}, {
'cnt' => 8,
'ym' => 202010,
}],
};
# Get object.
my $obj = struct2obj($struct_hr);
# Get count.
my $count = $obj->count;
# Get type.
my $type = $obj->type;
# Get user statistics.
my $users_ar = $obj->users;
# Get year/month statistics.
my $year_months_ar = $obj->year_months;
# Print out.
print "Count: $count\n";
print "Type: $type\n";
print "Count of users: ".(scalar @{$users_ar})."\n";
print "Count of year/months: ".(scalar @{$year_months_ar})."\n";
# Output:
# Count: 10
# Type: Q5
# Count of users: 2
# Count of year/months: 2
DEPENDENCIES
Error::Pure, Exporter, Readonly, Toolforge::MixNMatch::Object::Catalog, Toolforge::MixNMatch::Struct::User, Toolforge::MixNMatch::Struct::YearMonth.
SEE ALSO
- Toolforge::MixNMatch::Struct
-
Toolforge Mix'n'match tool structures.
REPOSITORY
https://github.com/michal-josef-spacek/Toolforge-MixNMatch-Struct
AUTHOR
Michal Josef Špaček mailto:skim@cpan.org
LICENSE AND COPYRIGHT
© Michal Josef Špaček 2020
BSD 2-Clause License
VERSION
0.04