Skip to content

Commit

Permalink
v3: null safety (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Dec 12, 2020
1 parent 70471b3 commit 4f4603a
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 43 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ubuntu-latest

container:
image: google/dart:latest
image: google/dart:beta

steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: pub get
run: dart pub get
- name: Format
run: dartfmt --dry-run --set-exit-if-changed lib test example
- name: Analyzer
run: dartanalyzer --fatal-infos --fatal-warnings lib test example
run: dart analyze --fatal-infos --fatal-warnings
- name: Tests
run: pub run test_coverage --no-badge --print-test-output --min-coverage 100
run: dart run test_coverage --no-badge --print-test-output --min-coverage 100
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [Unreleased]
### Changed
- Prepared for null safety

## [0.2.0] - 2020-09-07
### Added
- Ability to create arrays and set adjacent indices
Expand Down Expand Up @@ -83,4 +86,4 @@ Previously, no modification would be made and no errors/exceptions thrown.
[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
[0.0.0+dev.2]: https://github.com/f3ath/jessie/compare/0.0.0+dev.1...0.0.0+dev.2
[0.0.0+dev.1]: https://github.com/f3ath/jessie/compare/0.0.0+dev.0...0.0.0+dev.1
[0.0.0+dev.1]: https://github.com/f3ath/jessie/compare/0.0.0+dev.0...0.0.0+dev.1
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ include: package:pedantic/analysis_options.yaml
linter:
rules:
- sort_constructors_first
- sort_unnamed_constructors_first
- sort_unnamed_constructors_first
2 changes: 1 addition & 1 deletion lib/src/ast/tokenize.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Iterable<String> tokenize(String expr) =>
_tokens.allMatches(expr).map((match) => match.group(0));
_tokens.allMatches(expr).map((match) => match.group(0).toString());

final _tokens = RegExp([
r'\$', // root
Expand Down
7 changes: 4 additions & 3 deletions lib/src/json_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ class JsonPath {
/// in the [expression].
///
/// Throws [FormatException] if the [expression] can not be parsed.
factory JsonPath(String expression, {Map<String, Predicate> filter}) {
factory JsonPath(String expression,
{Map<String, Predicate> filter = const {}}) {
if (expression.isEmpty) throw FormatException('Empty expression');
ParsingState state = Ready(RootSelector());
AST(tokenize(expression)).nodes.forEach((node) {
state = state.process(node, filter ?? {});
state = state.process(node, filter);
});
return JsonPath._(state.selector);
}
Expand All @@ -33,7 +34,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.set(json, (_) => value);
dynamic set(json, value) => _selector.set(json, (_) => value);

@override
String toString() => _selector.expression();
Expand Down
15 changes: 8 additions & 7 deletions lib/src/parsing_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class ParsingState {

/// Ready to process the next node
class Ready implements ParsingState {
Ready(this.selector);
const Ready(this.selector);

@override
final Selector selector;
Expand Down Expand Up @@ -63,10 +63,11 @@ class Ready implements ParsingState {

Filter _filter(List<Node> nodes, Map<String, Predicate> filters) {
final name = nodes[1].value;
if (!filters.containsKey(name)) {
final filter = filters[name];
if (filter == null) {
throw FormatException('Filter not found: "${name}"');
}
return Filter(name, filters[name]);
return Filter(name, filter);
}

bool _isFilter(List<Node> nodes) => nodes.first.value == '?';
Expand All @@ -83,9 +84,9 @@ class Ready implements ParsingState {
}

Slice _slice(List<Node> nodes) {
int first;
int last;
int step;
int? first;
int? last;
int? step;
var colons = 0;
nodes.forEach((node) {
if (node.value == ':') {
Expand All @@ -107,7 +108,7 @@ class Ready implements ParsingState {
}

class AwaitingField implements ParsingState {
AwaitingField(this.selector);
const AwaitingField(this.selector);

@override
final Selector selector;
Expand Down
File renamed without changes.
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 set(dynamic json, Replacement replacement) =>
dynamic set(json, Replacement replacement) =>
isApplicable(json) ? replacement(json) : json;
}
2 changes: 1 addition & 1 deletion 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 set(dynamic json, Replacement replacement) =>
dynamic set(json, Replacement replacement) =>
left.set(json, (_) => right.set(_, replacement));
}
4 changes: 2 additions & 2 deletions lib/src/selector/list_union.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class ListUnion with SelectorMixin implements Selector {

@override
Iterable<JsonPathMatch> read(Iterable<JsonPathMatch> matches) => matches
.map((r) => (r.value is List) ? _map(r.value, r.path) : [])
.map((r) => (r.value is List) ? _map(r.value, r.path) : <JsonPathMatch>[])
.expand((_) => _);

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

@override
dynamic set(dynamic json, Replacement replacement) {
dynamic set(json, Replacement replacement) {
json ??= [];
if (json is List) {
json = [...json];
Expand Down
4 changes: 2 additions & 2 deletions lib/src/selector/list_wildcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ListWildcard with SelectorMixin implements Selector {
String expression() => '[*]';

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

Iterable<JsonPathMatch> _wrap(List val, String path) sync* {
static Iterable<JsonPathMatch> _wrap(List val, String path) sync* {
for (var i = 0; i < val.length; i++) {
yield JsonPathMatch(val[i], path + '[$i]');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/selector/object_union.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:json_path/src/json_path_match.dart';
import 'package:json_path/src/selector/quote.dart';
import 'package:json_path/src/quote.dart';
import 'package:json_path/src/selector/selector.dart';
import 'package:json_path/src/selector/selector_mixin.dart';

Expand All @@ -18,7 +18,7 @@ class ObjectUnion with SelectorMixin implements Selector {
String expression() => '[${_keys.map((k) => Quote(k)).join(',')}]';

@override
dynamic set(dynamic json, Replacement replacement) {
dynamic set(json, Replacement replacement) {
if (json == null) return _patch(<String, dynamic>{}, replacement);
if (json is Map) return {...json, ..._patch(json, replacement)};
return json;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/selector/object_wildcard.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:json_path/src/json_path_match.dart';
import 'package:json_path/src/selector/quote.dart';
import 'package:json_path/src/quote.dart';
import 'package:json_path/src/selector/selector.dart';
import 'package:json_path/src/selector/selector_mixin.dart';

Expand All @@ -24,7 +24,7 @@ class ObjectWildcard with SelectorMixin implements Selector {
.map((e) => JsonPathMatch(e.value, path + '[${e.key}]'));

@override
dynamic set(dynamic json, Replacement replacement) => (json is Map)
dynamic set(json, Replacement replacement) => (json is Map)
? json.map((key, value) => MapEntry(key, replacement(value)))
: json;
}
4 changes: 2 additions & 2 deletions lib/src/selector/recursive.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:json_path/src/json_path_match.dart';
import 'package:json_path/src/selector/quote.dart';
import 'package:json_path/src/quote.dart';
import 'package:json_path/src/selector/selector.dart';
import 'package:json_path/src/selector/selector_mixin.dart';

Expand All @@ -12,7 +12,7 @@ class Recursive with SelectorMixin implements Selector {
String expression() => '..';

@override
dynamic set(dynamic json, Replacement replacement) {
dynamic set(json, Replacement replacement) {
if (json is Map) {
return _replaceInMap(json, replacement);
}
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 set(dynamic json, Replacement replacement) => replacement(json);
dynamic set(json, Replacement replacement) => replacement(json);
}
4 changes: 2 additions & 2 deletions 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 set(dynamic json, Replacement replacement);
dynamic set(json, Replacement replacement);
}

typedef Replacement = dynamic Function(dynamic value);
typedef Replacement<V, T> = V Function(T value);
10 changes: 5 additions & 5 deletions lib/src/selector/slice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:json_path/src/selector/selector.dart';
import 'package:json_path/src/selector/selector_mixin.dart';

class Slice with SelectorMixin implements Selector {
Slice({int first, this.last, int step})
Slice({int? first, this.last, int? step})
: first = first ?? 0,
step = step ?? 1;

Expand All @@ -18,7 +18,7 @@ class Slice with SelectorMixin implements Selector {

final int first;

final int last;
final int? last;

final int step;

Expand All @@ -33,7 +33,7 @@ class Slice with SelectorMixin implements Selector {
'[${first == 0 ? '' : first}:${last ?? ''}${step != 1 ? ':$step' : ''}]';

@override
dynamic set(dynamic json, Replacement replacement) {
dynamic set(json, Replacement replacement) {
if (json is List) {
final indices = _indices(json);
if (indices.isNotEmpty) {
Expand All @@ -57,7 +57,7 @@ class Slice with SelectorMixin implements Selector {

int _actualLast(int len) {
if (last == null) return len;
if (last < 0) return min(len, len + last);
return min(len, last);
if (last! < 0) return min(len, len + last!);
return min(len, last!);
}
}
13 changes: 7 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: json_path
version: 0.2.0
version: 0.3.0-nullsafety
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"

environment:
sdk: '>=2.12.0-29 <3.0.0'

dev_dependencies:
pedantic: ^1.9.0
test: ^1.9.0
test_coverage: ^0.4.3
pedantic: ^1.10.0-nullsafety
test: ^1.16.0-nullsafety
test_coverage: ^0.5.0

environment:
sdk: ">=2.8.0 <3.0.0"

0 comments on commit 4f4603a

Please sign in to comment.