Poz/Types/object.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 147 CC 10 SCAR 32.6
CC
10
Cov
0%
safe_parse
72.0
new
6.0
parse
6.0
CRAP 26.0
1 package Poz::Types::object;
2 use 5.032;
3 use strict;
4 use warnings;
5 use Carp ();
6 use Try::Tiny;
7 use parent 'Poz::Types';
8  
9 sub new {
10 my ($class, $struct) = @_;
11 my $self = bless {
12 __struct__ => {},
13 __as__ => undef,
14 __is__ => undef,
15 }, $class;
16 for my $key (keys %$struct) {
17 my $v = $struct->{$key};
18 if ($v->isa('Poz::Types')) {
19 $self->{__struct__}{$key} = $v;
20 }
21 }
22 return $self;
23 }
24  
25 sub as {
26 my ($self, $typename) = @_;
27 $self->{__as__} = $typename;
28 return $self;
29 }
30  
31 sub is {
32 my ($self, $typename) = @_;
33 $self->{__is__} = $typename;
34 return $self;
35 }
36  
37 sub constructor {
38 my ($self) = @_;
39 my $caller_class = caller();
40 no strict 'refs';
41 *{"$caller_class\::new"} = sub {
42 my ($class, %args) = @_;
43 return $self
44 ->as($caller_class)
45 ->parse({%args});
46 }
47 }
48  
49 sub parse {
50 my ($self, $data) = @_;
51 my ($valid, $errors) = $self->safe_parse($data);
52 if ($errors) {
53 my $error_message = _errors_to_string($errors);
54 Carp::croak($error_message);
55 }
56 return $valid;
57 }
58  
59 sub safe_parse {
60 Carp::croak "Must handle error" unless wantarray;
61  
62 my ($self, $data) = @_;
63 my @errors = ();
64 my $valid = {};
65 if (!_is_hashref_or_object($data)) {
66 push @errors, {
67 key => '(root)',
68 error => "Invalid data: is not hashref"
69 };
70 } else {
71 if ($self->{__is__}) {
72 my $is = $self->{__is__};
73 if (!$data->isa($is)) {
74 push @errors, {
75 key => '(root)',
76 error => "Invalid data: is not $is"
77 };
78 }
79 }
80 for my $key (sort keys %{$self->{__struct__}}) {
81 my $v = $self->{__struct__}{$key};
82 my $val = $data->{$key};
83
84 # if the value is not defined, try to transform it
85 if (!defined $val && scalar(@{$v->{transform}}) > 0) {
86 for my $transformer (@{$v->{transform}}) {
87 $val = $transformer->($v, $val);
88 }
89 }
90  
91 try {
92 my $_parsed = $v->parse($val);
93 $valid->{$key} = $_parsed;
94 } catch {
95 my $error_message = $_;
96 $error_message =~ s/ at .+ line [0-9]+\.\n//;
97 push @errors, {
98 key => $key,
99 error => $error_message,
100 };
101 };
102 }
103 }
104 if (scalar(@errors) > 0) {
105 return (undef, [@errors])
106 }
107 my $classname = $self->{__as__} || $self->{__is__};
108 $valid = bless $valid, $classname if $classname;
109 return ($valid, undef);
110 }
111  
112 sub _is_hashref_or_object {
113 my $data = shift;
114 return defined $data && (ref($data) eq 'HASH' || $data->isa('HASH'));
115 }
116  
117 sub _errors_to_string {
118 my $errors = shift;
119 my @error_strings = ();
120 for my $error (@$errors) {
121 push @error_strings, sprintf("%s on key `%s`", $error->{error}, $error->{key});
122 }
123 return join(", and ", @error_strings);
124 }
125  
126 1;
127  
128 =head1 NAME
129  
130 Poz::Types::object - A module for handling structured data with type validation
131  
132 =head1 SYNOPSIS
133  
134 use Poz qw/z/;
135  
136 # Schema for a person, cast to Some::Class when valid
137 my $object = z->object({
138 name => z->string,
139 age => z->number,
140 })->as('Some::Class');
141 my $data = {
142 name => 'John Doe',
143 age => 30,
144 };
145 my $parsed_data = $object->parse($data); # isa Some::Class
146  
147 # Schema for a person, validate that the data is an instance of Some::Class
148 my $another_object = z->object({
149 name => z->string,
150 age => z->number,
151 })->is('Another::Class');
152 my $other = bless {
153 name => 'John Doe',
154 age => 30,
155 }, 'Another::Class';
156 my $someone = bless {
157 name => 'Jane Doe',
158 age => 25,
159 }, 'Some::Class';
160 my $parsed_data = $another_object->parse($other); # isa Another::Class
161 my $someone_else = $another_object->parse($someone); # throws an exception, because not an instance of Another::Class
162  
163 # or use Poz as your class constructor
164 {
165 package My::Class;
166 use Poz qw/z/;
167 z->object({
168 name => z->string,
169 age => z->number,
170 })->constructor;
171 }
172 my $instance = My::Class->new(
173 name => 'Alice',
174 age => 20,
175 );
176  
177  
178 =head1 DESCRIPTION
179  
180 Poz::Types::object is a module for handling structured data with type validation. It allows you to define a structure with specific types and validate data against this structure.
181  
182 =head1 METHODS
183  
184 =head2 as
185  
186 $object->as($typename);
187  
188 Sets the class name to bless the parsed data into. The C<$typename> parameter should be a string representing the class name.
189  
190 =head2 is
191  
192 $object->is($typename);
193  
194 Validates that the parsed data is an instance of the given class. The C<$typename> parameter should be a string representing the class name.
195  
196 =head2 constructor
197  
198 $object->constructor;
199  
200 Creates a constructor method into your class.
201  
202 =head2 parse
203  
204 my $parsed_data = $object->parse($data);
205  
206 Parses and validates the given data against the structure. If the data is valid, it returns the parsed data. If the data is invalid, it throws an exception with the validation errors.
207  
208 =head2 safe_parse
209  
210 my ($valid, $errors) = $object->safe_parse($data);
211  
212 Parses and validates the given data against the structure. If the data is valid, it returns the parsed data and undef for errors. If the data is invalid, it returns undef for valid data and an array reference of errors.
213  
214 =head1 LICENSE
215  
216 Copyright (C) ytnobody.
217  
218 This library is free software; you can redistribute it and/or modify
219 it under the same terms as Perl itself.
220  
221 =head1 AUTHOR
222  
223 ytnobody E<lt>ytnobody@gmail.comE<gt>
224  
225 =cut