Skip to content

Commit

Permalink
0.1.2 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Sep 6, 2020
1 parent c4f5d31 commit b14de58
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 39 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## [Unreleased]
## [0.1.2] - 2020-09-06
### Changed
- When JsonPath.set() is called on a path with non-existing property, the property will be created.
Previously, no modification would be made and no errors/exceptions thrown.
- When JsonPath.set() is called on a path with non-existing index, a `RangeError` will be thrown.
Previously, no modification would be made and no errors/exceptions thrown.

## [0.1.1] - 2020-09-05
### Fixed
- Fixed example code in the readme
Expand All @@ -13,7 +20,7 @@

## [0.0.2] - 2020-09-01
### Fixed
- Last element of array was not selected (regression #1)
- Last element of array would not get selected (regression #1)

## [0.0.1] - 2020-08-03
### Added
Expand Down Expand Up @@ -53,7 +60,8 @@
### Added
- Basic design draft

[Unreleased]: https://github.com/f3ath/jessie/compare/0.1.1...HEAD
[Unreleased]: https://github.com/f3ath/jessie/compare/0.1.2...HEAD
[0.1.2]: https://github.com/f3ath/jessie/compare/0.1.1...0.1.2
[0.1.1]: https://github.com/f3ath/jessie/compare/0.1.0...0.1.1
[0.1.0]: https://github.com/f3ath/jessie/compare/0.0.2...0.1.0
[0.0.2]: https://github.com/f3ath/jessie/compare/0.0.1...0.0.2
Expand Down
3 changes: 1 addition & 2 deletions lib/src/json_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class JsonPath {
_selector.read([JsonPathMatch(json, '')]);

/// Returns a copy of [json] with all matching values replaced with [value].
dynamic set(dynamic json, dynamic value) =>
_selector.replace(json, (_) => value);
dynamic set(dynamic json, dynamic value) => _selector.set(json, (_) => value);

@override
String toString() => _selector.expression();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/selector/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class Filter with SelectorMixin implements Selector {
String expression() => '[?$name]';

@override
dynamic replace(dynamic json, Replacement replacement) =>
dynamic set(dynamic json, Replacement replacement) =>
isApplicable(json) ? replacement(json) : json;
}
4 changes: 2 additions & 2 deletions lib/src/selector/joint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class Joint with SelectorMixin implements Selector {
right.expression();

@override
dynamic replace(dynamic json, Replacement replacement) =>
left.replace(json, (_) => right.replace(_, replacement));
dynamic set(dynamic json, Replacement replacement) =>
left.set(json, (_) => right.set(_, replacement));
}
8 changes: 2 additions & 6 deletions lib/src/selector/list_union.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ class ListUnion with SelectorMixin implements Selector {
.map((key) => JsonPathMatch(list[key], path + '[$key]'));

@override
dynamic replace(dynamic json, Function(dynamic _) replacement) {
dynamic set(dynamic json, Function(dynamic _) replacement) {
if (json is List) {
final applicableKeys = keys.where((key) => json.length > key);
if (applicableKeys.isEmpty) {
return json;
}
return _replaceInList(json, applicableKeys, replacement);
return _replaceInList(json, keys, replacement);
}
return json;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/selector/list_wildcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ListWildcard with SelectorMixin implements Selector {
String expression() => '[*]';

@override
dynamic replace(dynamic json, Replacement replacement) =>
dynamic set(dynamic json, Replacement replacement) =>
(json is List) ? json.map(replacement).toList() : json;

Iterable<JsonPathMatch> _wrap(List val, String path) sync* {
Expand Down
17 changes: 5 additions & 12 deletions lib/src/selector/object_union.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@ class ObjectUnion with SelectorMixin implements Selector {
String expression() => '[${keys.map((k) => Quote(k)).join(',')}]';

@override
dynamic replace(dynamic json, Replacement replacement) {
if (json is Map) {
final patch = _makePatch(json, replacement);
if (patch.isEmpty) {
return json;
}
return {...json, ...patch};
}
dynamic set(dynamic json, Replacement replacement) {
if (json == null) return _patch(<String, dynamic>{}, replacement);
if (json is Map) return {...json, ..._patch(json, replacement)};
return json;
}

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

Map<String, dynamic> _makePatch(Map map, Replacement replacement) =>
Map.fromEntries(keys
.where(map.containsKey)
.map((key) => MapEntry(key, replacement(map[key]))));
Map _patch(Map map, Replacement replacement) =>
Map.fromEntries(keys.map((key) => MapEntry(key, replacement(map[key]))));
}
2 changes: 1 addition & 1 deletion lib/src/selector/object_wildcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ObjectWildcard with SelectorMixin implements Selector {
.map((e) => JsonPathMatch(e.value, path + '[${e.key}]'));

@override
dynamic replace(dynamic json, Replacement replacement) => (json is Map)
dynamic set(dynamic json, Replacement replacement) => (json is Map)
? json.map((key, value) => MapEntry(key, replacement(value)))
: json;
}
6 changes: 3 additions & 3 deletions lib/src/selector/recursive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Recursive with SelectorMixin implements Selector {
String expression() => '..';

@override
dynamic replace(dynamic json, Replacement replacement) {
dynamic set(dynamic json, Replacement replacement) {
if (json is Map) {
return _replaceInMap(json, replacement);
}
Expand All @@ -33,10 +33,10 @@ class Recursive with SelectorMixin implements Selector {
}

dynamic _replaceInMap(Map map, Replacement replacement) => replacement(
map.map((key, value) => MapEntry(key, replace(value, replacement))));
map.map((key, value) => MapEntry(key, set(value, replacement))));

dynamic _replaceInList(List list, Replacement replacement) =>
replacement(list.map((value) => replace(value, replacement)).toList());
replacement(list.map((value) => set(value, replacement)).toList());

Iterable<JsonPathMatch> _values(List val, String path) => val
.asMap()
Expand Down
2 changes: 1 addition & 1 deletion lib/src/selector/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class RootSelector with SelectorMixin implements Selector {
String expression() => r'$';

@override
dynamic replace(dynamic json, Replacement replacement) => replacement(json);
dynamic set(dynamic json, Replacement replacement) => replacement(json);
}
2 changes: 1 addition & 1 deletion lib/src/selector/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class Selector {
Selector then(Selector other);

/// Returns a copy of [json] with all selected values modified using [replacement] function.
dynamic replace(dynamic json, Replacement replacement);
dynamic set(dynamic json, Replacement replacement);
}

typedef Replacement = dynamic Function(dynamic value);
2 changes: 1 addition & 1 deletion lib/src/selector/slice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Slice with SelectorMixin implements Selector {
'[${first == 0 ? '' : first}:${last ?? ''}${step != 1 ? ':$step' : ''}]';

@override
dynamic replace(dynamic json, Replacement replacement) {
dynamic set(dynamic json, Replacement replacement) {
if (json is List) {
final indices = _indices(json);
if (indices.isNotEmpty) {
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.1.1+1
version: 0.1.2
description: Implementation of JSONPath expressions like "$.store.book[2].price". Can read and set values in parsed JSON objects.
homepage: "https://github.com/f3ath/jessie"

Expand Down
14 changes: 9 additions & 5 deletions test/set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ void main() {
expect(json['store']['bicycle']['color'], 'red',
reason: 'Original bike is still red');
});
test('Does not set non-existing field', () {
final foo = JsonPath(r'$.foo');
final mutated = foo.set(json, 42);
expect(mutated.containsKey('foo'), false);
expect(mutated['store'], json['store']);
test('Creates a field if it does not exist', () {
final abc = JsonPath(r'$.a.b.c');
final mutated = abc.set({'foo': 'bar'}, 'magic');
expect(mutated['foo'], 'bar');
expect(mutated['a']['b']['c'], 'magic');
});
test('Does not change non-object (scalars, arrays)', () {
expect(store.set(42, 'foo'), 42);
Expand Down Expand Up @@ -97,5 +97,9 @@ void main() {
expect(mutated['store']['book'][2], 'banana');
expect(mutated['store']['book'][3]['price'], isA<num>());
});
test('Index. Setting non-existing index throws RangeError', () {
final title = JsonPath(r'$.store.book[100].title');
expect(() => title.set(json, 'Banana'), throwsRangeError);
});
});
}

0 comments on commit b14de58

Please sign in to comment.