使用 Type::Tiny,有没有办法对日期类型进行字符串化、缩减或取消强制转换?

Using Type::Tiny, is there a way to stringify, deflate or uncoerce a date type?

我构建了一个使用 Type::Tiny 的模块,它工作正常。

现在我必须编写一个 TO_JSON 子例程,我希望我不必为每条数据编写 deflate 方法。有没有办法在类型定义中用Type::Tiny定义一个紧缩方法? 'uncoerce' 可以这么说。我没有在文档中找到任何内容,但我可能只是没有在寻找正确的关键字。

我没有使用像 Moo 或 Moose 这样的对象框架,而是为数据使用对象。

一个简化的例子看起来像

package My::Object::Types;
# the usual stuff, strict, etc.
my $meta = __PACKAGE__->meta;
my $_Date = Type::Tiny::Class->new( class => 'Time::Piece' );
my $_Due = $meta->add_type(
  name => 'Due',
  parent => $_Date,
);

package My::Object;
# again, the usual strict, warnings, etc.
sub new {
  my ( $class, $args ) = @_;
  my $self = bless {}, $class;

  for my $attr ( keys %$args ) {
    my $type = ucfirst $attr;
    my $set_attribute = "set_$attr";

    *$set_attribute = sub {
      my ( $self, $value ) = @_;
      $value = $self->$type->assert_coerce( $value ) if $self->$type->has_coercion;
      my $invalid = $self->$type->validate( $value );
      carp $invalid && return if $invalid;
      $self->{data}{$attr} = $value;
    };

    $attr = sub { return $_[0]->{data}{$attr} };
  }

  return $self;
}

回答我自己的问题...我必须编写自己的 deflate 方法。