Skip to content

Commit

Permalink
dev.6
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Aug 2, 2020
1 parent 1d8446a commit ee6eb41
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 49 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## [Unreleased]
## [0.0.0+dev.6] - 2020-08-01
### Added
- Unions

## [0.0.0+dev.5] - 2020-07-31
### Added
- Slice expression
Expand All @@ -25,7 +29,8 @@
### Added
- Basic design draft

[Unreleased]: https://github.com/f3ath/jessie/compare/0.0.0+dev.5...HEAD
[Unreleased]: https://github.com/f3ath/jessie/compare/0.0.0+dev.6...HEAD
[0.0.0+dev.6]: https://github.com/f3ath/jessie/compare/0.0.0+dev.5...0.0.0+dev.6
[0.0.0+dev.5]: https://github.com/f3ath/jessie/compare/0.0.0+dev.4...0.0.0+dev.5
[0.0.0+dev.4]: https://github.com/f3ath/jessie/compare/0.0.0+dev.3...0.0.0+dev.4
[0.0.0+dev.3]: https://github.com/f3ath/jessie/compare/0.0.0+dev.2...0.0.0+dev.3
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
- [x] Wildcard (`$.store.*`)
- [x] Square-bracket field notation (`['foo']`, `$['"some" \'special\' [chars]']`)
- [x] Slice (`articles[1:10:2]`)
- [ ] Union (`book[0, 1]`, `book[author, title, price]`)
- [ ] Basic filtering (`book[?(@.price - 1)]`)
- [ ] Expressions?
- [x] Union (`book[0, 1]`, `book[author, title, price]`)
- [ ] Basic filtering (`book[?(@.price)]`)
- [ ] Expressions (`book[?(@.price * @.discount < 10)]`)


[JSONPath]: https://goessner.net/articles/JsonPath/
20 changes: 20 additions & 0 deletions lib/src/selector/list_union.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:json_path/src/result.dart';
import 'package:json_path/src/selector/selector.dart';

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

final List<int> keys;

@override
Iterable<Result> call(Iterable<Result> results) => results
.map((r) => (r.value is List) ? mapList(r.value, r.path) : [])
.expand((_) => _);

Iterable<Result> mapList(List list, String path) => keys
.where((key) => key < list.length)
.map((key) => Result(list[key], path + '[$key]'));

@override
String get expression => '[${keys.join(',')}]';
}
21 changes: 21 additions & 0 deletions lib/src/selector/object_union.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:json_path/src/quote.dart';
import 'package:json_path/src/result.dart';
import 'package:json_path/src/selector/selector.dart';

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

final List<String> keys;

@override
Iterable<Result> call(Iterable<Result> results) => results
.map((r) => (r.value is Map) ? map(r.value, r.path) : [])
.expand((_) => _);

Iterable<Result> map(Map map, String path) => keys
.where(map.containsKey)
.map((key) => Result(map[key], path + '[${Quote(key)}]'));

@override
String get expression => '[${keys.map((k) => Quote(k)).join(',')}]';
}
16 changes: 0 additions & 16 deletions lib/src/selector/union.dart

This file was deleted.

52 changes: 31 additions & 21 deletions lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:json_path/src/selector/all_in_array.dart';
import 'package:json_path/src/selector/all_values.dart';
import 'package:json_path/src/selector/field.dart';
import 'package:json_path/src/selector/index.dart';
import 'package:json_path/src/selector/list_union.dart';
import 'package:json_path/src/selector/object_union.dart';
import 'package:json_path/src/selector/recursive.dart';
import 'package:json_path/src/selector/selector.dart';
import 'package:json_path/src/selector/slice.dart';
Expand Down Expand Up @@ -45,27 +47,35 @@ class Ready implements State {
return multiValueBrackets(nodes);
}

Slice multiValueBrackets(List<Node> nodes) {
int first;
int last;
int step;
var colons = 0;
nodes.forEach((node) {
if (node.value == ':') {
colons++;
return;
}
if (colons == 0) {
first = node.intValue;
return;
}
if (colons == 1) {
last = node.intValue;
return;
}
step = node.intValue;
});
return Slice(first: first, last: last, step: step);
Selector multiValueBrackets(List<Node> nodes) {
if (nodes.any((node) => node.value == ':')) {
int first;
int last;
int step;
var colons = 0;
nodes.forEach((node) {
if (node.value == ':') {
colons++;
return;
}
if (colons == 0) {
first = node.intValue;
return;
}
if (colons == 1) {
last = node.intValue;
return;
}
step = node.intValue;
});
return Slice(first: first, last: last, step: step);
}
final filtered = nodes.where((_) => _.value != ',');
if (nodes.first.isNumber) {
return ListUnion(filtered.map((_) => _.intValue).toList());
}
return ObjectUnion(
filtered.map((_) => _.isQuoted ? _.unquoted : _.value).toList());
}

Selector singleValueBrackets(Node node) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/tokenize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const _singles = [
'.',
'*',
':',
',',
];

const _doubles = [
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_path
version: 0.0.0+dev.5
version: 0.0.0+dev.6
description: JSONPath for Dart. JSONPath is XPath for JSON. It is a path in a JSON document.
homepage: "https://github.com/f3ath/jessie"

Expand Down
33 changes: 26 additions & 7 deletions test/json_path_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,32 @@ 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('Union', () {
test('List', () {
final abc = 'abcdefg'.split('');
final union = JsonPath(r'$[2,3,100,5]');
expect(union.toString(), r'$[2,3,100,5]');
expect(union.select(abc).length, 3);
expect(union.select(abc).first.value, 'c');
expect(union.select(abc).first.path, r'$[2]');
expect(union.select(abc).last.value, 'f');
expect(union.select(abc).last.path, r'$[5]');
});
test('Object', () {
final abc = {
'a': 'A',
'b': 'B',
'c': 'C',
};
final union = JsonPath(r"$['a','x',c]");
expect(union.toString(), r"$['a','x','c']");
expect(union.select(abc).length, 2);
expect(union.select(abc).first.value, 'A');
expect(union.select(abc).first.path, r"$['a']");
expect(union.select(abc).last.value, 'C');
expect(union.select(abc).last.path, r"$['c']");
});
});

group('Wildcards', () {
test('All in root', () {
Expand Down

0 comments on commit ee6eb41

Please sign in to comment.