Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Aug 2, 2020
1 parent 407d372 commit 74ee6c9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
29 changes: 11 additions & 18 deletions lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@ class Node {
Node(this.value);

/// Builds the AST from the list of tokens
static Node build(List<String> tokens) {
final root = Node(r'$');
if (tokens.isEmpty) {
return root;
}
final reversed = [...tokens.reversed];
if (reversed.last == r'$') {
reversed.removeLast();
}
final stack = <Node>[root];
while (reversed.isNotEmpty) {
final token = reversed.removeLast();
static List<Node> list(Iterable<String> tokens) {
const root = r'$';
final stack = <Node>[Node(root)];
tokens.skipWhile((token) => token == root).forEach((token) {
if (token == '[') {
stack.add(Node(token));
continue;
return;
}
if (token == ']' || token == ')') {
final search = token == ']' ? '[' : '(';
final closing = token == ']' ? '[' : '(';
final children = <Node>[];
while (stack.last.value != search) {
while (stack.last.value != closing) {
children.add(stack.removeLast());
}
final brackets = stack.removeLast();
brackets.children.addAll(children.reversed);
stack.last.children.add(brackets);
continue;
return;
}
stack.last.children.add(Node(token));
}
return stack.last;
});

return stack.last.children;
}

final String value;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/json_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import 'package:json_path/src/tokenize.dart';
class JsonPath {
/// Creates an instance from string
factory JsonPath(String expression) {
if (expression.isEmpty) throw FormatException('Empty expression');
State state = Ready(Root());
for (final node in Node.build(tokenize(expression)).children) {
for (final node in Node.list(tokenize(expression))) {
state = state.process(node);
}
return JsonPath._(state.selector);
Expand Down
16 changes: 16 additions & 0 deletions lib/src/selector/union.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:json_path/src/result.dart';
import 'package:json_path/src/selector/selector.dart';

class ArrayUnion extends Selector {
ArrayUnion(this.keys);

final List<int> keys;
@override
Iterable<Result> call(Iterable<Result> results) {
throw UnimplementedError();
}

@override
String get expression => '[${keys.join(',')}]';

}
4 changes: 4 additions & 0 deletions lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class Ready implements State {
int step;
var colons = 0;
nodes.map((_) => _.value).forEach((val) {
// if (nodes.length > 2 && nodes[1].value == ',') {
// // union. take the odd ones
// nodes.map((e) => e.value).where((element) => element != ',')
// }
if (val == ':') {
colons++;
return;
Expand Down
7 changes: 2 additions & 5 deletions lib/src/tokenize.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// Parses a JSONPath expression into a list of tokens
List<String> tokenize(String expr) {
final tokens = <String>[];
Iterable<String> tokenize(String expr) sync* {
var pos = 0;
var insideQuotedString = false;
while (pos < expr.length) {
Expand Down Expand Up @@ -41,12 +40,10 @@ List<String> tokenize(String expr) {
pos += 1;
}
if (insideQuotedString) throw FormatException('Unmatched quote');
tokens.add(token);
yield token;
}
return tokens;
}


const _singles = [
r'$',
'[',
Expand Down
19 changes: 11 additions & 8 deletions test/json_path_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import 'package:test/test.dart';
void main() {
final json = jsonDecode(File('test/store.json').readAsStringSync());
group('Basic expressions', () {
test('Empty', () {
final empty = JsonPath('');
expect(empty.toString(), r'$');
expect(empty.select(json).single.value, json);
expect(empty.select(json).single.path, r'$');
});

test('Only root', () {
final root = JsonPath(r'$');
expect(root.toString(), r'$');
Expand Down Expand Up @@ -49,6 +42,9 @@ void main() {
expect(() => JsonPath(r"$['hello"), throwsFormatException);
expect(() => JsonPath(r"$['hello\"), throwsFormatException);
});
test('Empty', () {
expect(() => JsonPath(''), throwsFormatException);
});
});

group('Slices', () {
Expand Down Expand Up @@ -164,7 +160,6 @@ void main() {
expect(slice.select(abc).last.value, 'f');
expect(slice.select(abc).last.path, r'$[5]');
});

});

group('Uncommon brackets', () {
Expand All @@ -177,6 +172,14 @@ void main() {
});
});

// group('Union', () {
// test('Array', () {
//// final abc = 'abcdefg'.split('');
// final union = JsonPath(r'$[2,3,5]');
// expect(union.toString(), r'$[2,3,5]');
// });
// });

group('Wildcards', () {
test('All in root', () {
final allInRoot = JsonPath(r'$.*');
Expand Down

0 comments on commit 74ee6c9

Please sign in to comment.