package Data::Query::ExprBuilder;
use strictures 1;
+use Data::Query::Constants qw(DQ_OPERATOR DQ_VALUE);
+use Scalar::Util ();
+
+use overload (
+ (map {
+ my $op = $_;
+ $op => sub {
+ Data::Query::ExprBuilder->new({
+ expr => {
+ type => DQ_OPERATOR,
+ operator => { perl => $op },
+ args => [
+ map {
+ (Scalar::Util::blessed($_)
+ && $_->isa('Data::Query::ExprBuilder'))
+ ? $_->{expr}
+ : {
+ type => DQ_VALUE,
+ subtype => { perl => 'Scalar' },
+ value => $_
+ }
+ } $_[2] ? @_[1,0] : @_[0,1]
+ ]
+ },
+ });
+ }
+ } qw(==))
+);
sub new {
bless({ %{$_[1]} }, (ref($_[0])||$_[0]));
use strictures 1;
use Test::More qw(no_plan);
use Data::Query::ExprBuilder::Identifier;
-use Data::Query::Constants qw(DQ_IDENTIFIER);
+use Data::Query::Constants qw(DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE);
-sub mk_expr {
+sub expr (&) {
+ _mk_expr($_[0]);
+}
+
+sub _mk_expr {
local $_ = Data::Query::ExprBuilder::Identifier->new({
expr => {
type => DQ_IDENTIFIER,
sub expr_is (&;@) {
my $sub = shift;
- is_deeply(mk_expr($sub), @_);
+ is_deeply(_mk_expr($sub), @_);
}
expr_is { $_->foo }
elements => [ 'foo', 'bar' ]
},
'Nested identifier ok';
+
+expr_is { $_->foo == 3 }
+ {
+ type => DQ_OPERATOR,
+ operator => { perl => '==' },
+ args => [
+ expr { $_->foo },
+ {
+ type => DQ_VALUE,
+ subtype => { perl => 'Scalar' },
+ value => 3,
+ },
+ ],
+ },
+ 'Simple equality ok';