我有一个自定义DateTime类型,从字符串到DateTime定义了强制,如下所示:
package Library;
use Type::Library -base, -declare => qw(DateTime);
use DateTime::Format::ISO8601;
class_type DateTime, { class => 'DateTime' };
coerce DateTime, from Str, via { DateTime::Format::ISO8601->parse_datetime($_) };
我想在Dict中使用该DateTime类型,如下所示:
package MyObj;
use Moo;
$constraint = declare MyType, as Dict[ name => Str, date => DateTime ];
has 'whatsis' => ( is => 'ro', isa => $constraint );
然后将其称为:
use MyObj;
my $obj = MyObj->new( whatsis => { name => 'Something', date => '2016-01-01' } );
我已尝试将coerce => 1
添加到whatsis
的声明中,但这没有用。
如何创建从Dict继承的自定义类型并运行在成员类型上定义的类型强制?
答案 0 :(得分:2)
来自https://metacpan.org/pod/distribution/Type-Tiny/lib/Type/Tiny/Manual/Coercions.pod
的大量帮助Library.pm:
package MyObj;
use Moo;
use Library -all;
has 'whatsis' => ( is => 'ro', isa => MyType, coerce => 1 );
MyObj.pm:
#!perl
use MyObj;
my $obj = MyObj->new( whatsis => { name => 'Something', date => '2016-01-01' } );
use Data::Dumper; print Dumper $obj;
tester.pl:
var query = from user in UserManager.Users
join ur in _userRoleRepository.GetAll() on user.Id equals ur.UserId into urJoined
from ur in urJoined.DefaultIfEmpty()
join up in _userPermissionRepository.GetAll() on new {UserId = user.Id, Name = input.Permission} equals
new {up.UserId, up.Name} into upJoined
from up in upJoined.DefaultIfEmpty()
join rp in _rolePermissionRepository.GetAll() on new {RoleId = ur.RoleId, Name = input.Permission}
equals new {rp.RoleId, rp.Name} into rpJoined
from rp in rpJoined.DefaultIfEmpty()
where (up != null && up.IsGranted) || (up == null && rp != null)
group user by user
into userGrouped
select userGrouped.Key;
var users = await query.ToListAsync();