Remove these from here, its now a separate dist on cpan
This commit is contained in:
parent
e83386b4e1
commit
6a66ab275b
2 changed files with 0 additions and 158 deletions
|
@ -1,71 +0,0 @@
|
|||
package Object::PadX::Role::AutoJSON;
|
||||
|
||||
use v5.38;
|
||||
|
||||
use Object::Pad ':experimental(custom_field_attr mop)';
|
||||
use Object::Pad::MOP::FieldAttr;
|
||||
use Object::Pad::MOP::Field;
|
||||
use Object::Pad::MOP::Class;
|
||||
use Syntax::Operator::Equ;
|
||||
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONExclude", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
# Set a new name when going to JSON
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONKey", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
# Allow this to get sent as null, rather than leaving it off
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONNull", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
# Force boolean or num or str
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONBool", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONNum", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
Object::Pad::MOP::FieldAttr->register( "JSONStr", permit_hintkey => 'Object/PadX/Role/AutoJSON' );
|
||||
|
||||
|
||||
|
||||
use Data::Dumper;
|
||||
|
||||
sub import { $^H{'Object/PadX/Role/AutoJSON'}=1;}
|
||||
|
||||
role AutoJSON {
|
||||
use Carp qw/croak/;
|
||||
use experimental 'for_list';
|
||||
|
||||
method TO_JSON() {
|
||||
my $class = __CLASS__;
|
||||
my $classmeta = Object::Pad::MOP::Class->for_class($class);
|
||||
my @metafields = $classmeta->fields;
|
||||
|
||||
my %json_out = ();
|
||||
|
||||
for my $metafield (@metafields) {
|
||||
my $field_name = $metafield->name;
|
||||
my $sigil = $metafield->sigil;
|
||||
|
||||
my $has_exclude = $metafield->has_attribute("JSONExclude");
|
||||
|
||||
next if $has_exclude;
|
||||
|
||||
next if $sigil ne '$'; # Don't try to handle anything but scalars
|
||||
|
||||
my $has_null = $metafield->has_attribute("JSONNull");
|
||||
|
||||
my $value = $metafield->value($self);
|
||||
next unless (defined $value || $has_null);
|
||||
|
||||
my $key = $field_name =~ s/^\$//r;
|
||||
$key = $metafield->get_attribute_value("JSONKey") if $metafield->has_attribute("JSONKey");
|
||||
|
||||
if ($metafield->has_attribute('JSONBool')) {
|
||||
$value = !!$value ? \1 : \0;
|
||||
} elsif ($metafield->has_attribute('JSONNum')) {
|
||||
# Force numification
|
||||
$value = 0+$value;
|
||||
} elsif ($metafield->has_attribute('JSONStr')) {
|
||||
# Force stringification
|
||||
$value = "".$value;
|
||||
}
|
||||
|
||||
$json_out{$key} = $value;
|
||||
}
|
||||
|
||||
return \%json_out;
|
||||
}
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
package Object::PadX::Role::AutoMarshal;
|
||||
|
||||
use v5.38;
|
||||
|
||||
use Object::Pad ':experimental(custom_field_attr mop)';
|
||||
use Object::Pad::MOP::FieldAttr;
|
||||
use Object::Pad::MOP::Field;
|
||||
use Object::Pad::MOP::Class;
|
||||
use Syntax::Operator::Equ;
|
||||
|
||||
Object::Pad::MOP::FieldAttr->register( "MarshalTo", permit_hintkey => 'Object/PadX/Role/AutoMarshal' );
|
||||
|
||||
use Data::Dumper;
|
||||
|
||||
sub import { $^H{'Object/PadX/Role/AutoMarshal'}=1;}
|
||||
|
||||
role AutoMarshal {
|
||||
use Carp qw/croak/;
|
||||
use experimental 'for_list';
|
||||
|
||||
ADJUST {
|
||||
my $class = __CLASS__;
|
||||
|
||||
my $classmeta = Object::Pad::MOP::Class->for_class($class);
|
||||
my @metafields = $classmeta->fields;
|
||||
|
||||
for my $metafield (@metafields) {
|
||||
my $field_name = $metafield->name;
|
||||
my $sigil = $metafield->sigil;
|
||||
|
||||
my $has_attr = $metafield->has_attribute("MarshalTo");
|
||||
|
||||
# one of ours!
|
||||
if ($metafield->has_attribute("MarshalTo")) {
|
||||
my $value = $metafield->value($self);
|
||||
my $newvalue;
|
||||
|
||||
my $newclass = $metafield->get_attribute_value("MarshalTo");
|
||||
|
||||
if ($sigil equ '$') {
|
||||
# TODO more advanced parser?
|
||||
# :KeyValidator?
|
||||
if ($newclass =~ /^\[(.*?)\]$/) {
|
||||
$newclass = $1;
|
||||
|
||||
my @list = map {$newclass->new($_->%*)} $value->@*;
|
||||
|
||||
$newvalue = \@list;
|
||||
|
||||
$metafield->value($self) = $newvalue;
|
||||
} elsif ($newclass =~ /^\{(.*?)\}$/) {
|
||||
$newclass = $1;
|
||||
|
||||
my %hash = ();
|
||||
for my ($k, $v) ($value->%*) {
|
||||
$hash{$k} = $newclass->new($v->%*);
|
||||
}
|
||||
my $newvalue = \%hash;
|
||||
|
||||
$metafield->value($self) = $newvalue;
|
||||
} else {
|
||||
$newvalue = $newclass->new($value->%*);
|
||||
|
||||
$metafield->value($self) = $newvalue;
|
||||
}
|
||||
} elsif ($sigil equ '%') {
|
||||
my %hash = ();
|
||||
for my ($k, $v) ($value->%*) {
|
||||
$hash{$k} = $newclass->new($v->%*);
|
||||
}
|
||||
my $newvalue = \%hash;
|
||||
|
||||
$metafield->value($self) = $newvalue;
|
||||
} elsif ($sigil equ '@') {
|
||||
$newclass = $1;
|
||||
|
||||
my @list = map {$newclass->new($_->%*)} $value->@*;
|
||||
my $newvalue = \@list;
|
||||
|
||||
$metafield->value($self) = $newvalue;
|
||||
} else {
|
||||
croak "Unable to handle field $class"."->$sigil$field_name";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue