Skip to content

Commit

Permalink
CTS update
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Dec 29, 2023
1 parent 826bb97 commit aaa0f3e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 114 deletions.
72 changes: 29 additions & 43 deletions lib/src/fun/fun_factory.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:json_path/fun_sdk.dart';
import 'package:json_path/src/expression/expression.dart';
import 'package:json_path/src/expression/nodes.dart';
import 'package:json_path/src/fun/fun.dart';
Expand Down Expand Up @@ -48,40 +49,29 @@ class FunFactory {

Expression<T> _any1<T extends Object>(String name, Expression a0) {
final f = _getFun1<T>(name);
if (f is Fun1<T, Maybe>) {
if (a0 is Expression<Maybe>) {
return a0.map(f.call);
}
if (a0 is Expression<SingularNodeList>) {
return a0.map((v) => v.asValue).map(f.call);
}
}
if (f is Fun1<T, bool>) {
if (a0 is Expression<bool>) {
return a0.map(f.call);
}
if (a0 is Expression<SingularNodeList>) {
return a0.map((v) => v.asLogical).map(f.call);
}
}
if (f is Fun1<T, NodeList>) {
if (a0 is Expression<NodeList>) {
return a0.map(f.call);
}
}
throw Exception('Fun arg mismatch');
final cast0 = cast(a0,
value: f is Fun1<T, Maybe>,
logical: f is Fun1<T, bool>,
nodes: f is Fun1<T, NodeList>);
return cast0.map(f.call);
}

Expression<T> _any2<T extends Object>(
String name, Expression a0, Expression a1) {
final f = _getFun2<T>(name);
final cast0 = cast(a0,
value: f is Fun2<T, Maybe, Object>,
logical: f is Fun2<T, bool, Object>);
final cast1 = cast(a1,
value: f is Fun2<T, Object, Maybe>,
logical: f is Fun2<T, Object, bool>);
return a0.map(cast0).merge(a1.map(cast1), f.call);
final cast0 = cast(
a0,
value: f is Fun2<T, Maybe, Object>,
logical: f is Fun2<T, bool, Object>,
nodes: f is Fun2<T, NodeList, Object>,
);
final cast1 = cast(
a1,
value: f is Fun2<T, Object, Maybe>,
logical: f is Fun2<T, Object, bool>,
nodes: f is Fun2<T, Object, NodeList>,
);
return cast0.merge(cast1, f.call);
}

Fun1<T, Object> _getFun1<T extends Object>(String name) {
Expand All @@ -96,21 +86,17 @@ class FunFactory {
throw StateError('Function "$name" of 2 arguments is not found');
}

static Object Function(Object) cast(Expression arg,
{required bool value, required bool logical}) {
static Expression cast(Expression arg,
{required bool value, required bool logical, required bool nodes}) {
if (value) {
if (arg is! Expression<Maybe> && arg is! Expression<SingularNodeList>) {
throw Exception('Arg type mismatch');
}
return _value;
if (arg is Expression<Maybe>) return arg;
if (arg is Expression<SingularNodeList>) return arg.map((v) => v.asValue);
}
if (logical) return _logical;
return _nodes;
if (logical) {
if (arg is Expression<bool>) return arg;
if (arg is Expression<NodeList>) return arg.map((v) => v.asLogical);
}
if (nodes && arg is Expression<NodeList>) return arg;
throw Exception('Arg type mismatch');
}

static Maybe _value(v) => (v is Maybe) ? v : _nodes(v).asValue;

static bool _logical(v) => (v is bool) ? v : _nodes(v).asLogical;

static NodeList _nodes(v) => v as NodeList;
}
2 changes: 1 addition & 1 deletion test/cases/cts
68 changes: 0 additions & 68 deletions test/cases/extra/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,74 +53,6 @@
"selector" : "$[?xor((@.b), (@.a))]",
"document" : [{"a": 0}, {"a": 0, "b": 0}, {"b": 0}, {}],
"result": [{"a": 0}, {"b": 0}]
},
{
"name": "functions, length, non-singular query arg",
"selector": "$[?length(@.*)<3]",
"invalid_selector": true
},
{
"name": "functions, length, arg is a function expression",
"selector": "$.values[?length(@.a)==length(value($..c))]",
"document": {
"c": "cd",
"values": [
{
"a": "ab"
},
{
"a": "d"
}
]
},
"result": [
{
"a": "ab"
}
]
}, {
"name": "functions, match, arg is a function expression",
"selector": "$.values[?match(@.a, value($..['regex']))]",
"document": {
"regex": "a.*",
"values": [
{
"a": "ab"
},
{
"a": "ba"
}
]
},
"result": [
{
"a": "ab"
}
]
},
{
"name": "functions, search, arg is a function expression",
"selector": "$.values[?search(@, value($..['regex']))]",
"document": {
"regex": "b.?b",
"values": [
"abc",
"bcd",
"bab",
"bba",
"bbab",
"b",
true,
[],
{}
]
},
"result": [
"bab",
"bba",
"bbab"
]
}

]
}
2 changes: 0 additions & 2 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ void main() {
});
});
}


0 comments on commit aaa0f3e

Please sign in to comment.