-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
446 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
/// JSONPath for Dart | ||
library json_path; | ||
|
||
export 'package:json_path/json_pointer.dart'; | ||
export 'package:json_path/src/filter_not_found.dart'; | ||
export 'package:json_path/src/json_path.dart'; | ||
export 'package:json_path/src/json_path_match.dart'; | ||
export 'package:json_path/src/matching_context.dart'; | ||
export 'package:json_path/src/path/filter_not_found.dart'; | ||
export 'package:json_path/src/path/json_path.dart'; | ||
export 'package:json_path/src/path/json_path_match.dart'; | ||
export 'package:json_path/src/path/matching_context.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
/// JSON Pointer (RFC 6901). | ||
class JsonPointer { | ||
/// Creates a pointer to the root element | ||
const JsonPointer() : value = ''; | ||
library json_pointer; | ||
|
||
JsonPointer._(this.value); | ||
|
||
/// The string value of the pointer | ||
final String value; | ||
|
||
/// Returns a new instance of [JsonPointer] | ||
/// with the [segment] appended at the end. | ||
JsonPointer append(String segment) => JsonPointer._( | ||
value + '/' + segment.replaceAll('~', '~0').replaceAll('/', '~1')); | ||
|
||
@override | ||
String toString() => value; | ||
} | ||
export 'package:json_path/src/pointer/json_pointer.dart'; | ||
export 'package:json_path/src/pointer/reference.dart'; | ||
export 'package:json_path/src/pointer/invalid_route.dart'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:json_path/src/path/grammar.dart'; | ||
import 'package:json_path/src/path/selector/selector.dart'; | ||
import 'package:petitparser/core.dart'; | ||
|
||
Parser<Selector> buildParser() { | ||
return jsonPath; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/json_path_match.dart → lib/src/path/json_path_match.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
lib/src/match_factory.dart → lib/src/path/match_factory.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/matching_context.dart → lib/src/path/matching_context.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
lib/src/selector/array_index.dart → lib/src/path/selector/array_index.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/src/selector/array_slice.dart → lib/src/path/selector/array_slice.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/src/selector/field.dart → lib/src/path/selector/field.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/src/selector/named_filter.dart → lib/src/path/selector/named_filter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/src/selector/recursion.dart → lib/src/path/selector/recursion.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:json_path/src/path/json_path_match.dart'; | ||
|
||
abstract class Selector { | ||
/// Applies this filter to the [match] | ||
Iterable<JsonPathMatch> read(JsonPathMatch match); | ||
} |
4 changes: 2 additions & 2 deletions
4
lib/src/selector/sequence.dart → lib/src/path/selector/sequence.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/src/selector/union.dart → lib/src/path/selector/union.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/src/selector/wildcard.dart → lib/src/path/selector/wildcard.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// Thrown when the referenced value is not found in the document. | ||
class InvalidRoute implements Exception { | ||
const InvalidRoute(this.pointer); | ||
|
||
final String pointer; | ||
|
||
@override | ||
String toString() => 'No value is referenced by $pointer'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import 'package:json_path/src/pointer/invalid_route.dart'; | ||
import 'package:json_path/src/pointer/reference.dart'; | ||
|
||
/// A JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901). | ||
class JsonPointer { | ||
const JsonPointer(); | ||
|
||
/// An empty JSON Pointer | ||
static const empty = JsonPointer(); | ||
|
||
/// Parses a new JSON Pointer from a string expression. | ||
/// This method returns a non-writable [JsonPointer] for an empty expression | ||
/// and [WritableJsonPointer] for a non-empty expression. | ||
static JsonPointer parse(String expression) { | ||
if (expression.isEmpty) return empty; | ||
return parseWritable(expression); | ||
} | ||
|
||
/// Parses a new writable JSON Pointer from a non-empty string expression. | ||
/// Throws a [FormatException] if the expression has invalid format. | ||
static WritableJsonPointer parseWritable(String expression) { | ||
final errors = _errors(expression); | ||
if (errors.isNotEmpty) throw FormatException(errors.join(' ')); | ||
return buildWritable(expression.split('/').skip(1).map(_unescape)); | ||
} | ||
|
||
/// Builds a new writable JSON Pointer from a non-empty iterable of segments. | ||
/// Throws a [ArgumentError] if the iterable is empty. | ||
static WritableJsonPointer buildWritable(Iterable<String> segments) { | ||
if (segments.isEmpty) throw ArgumentError('Empty segments'); | ||
return segments.skip(1).fold(empty.append(segments.first), | ||
(previousValue, element) => previousValue.append(element)); | ||
} | ||
|
||
/// Returns errors found in the [expression]. | ||
/// The expression is valid if no errors are returned. | ||
static Iterable<String> _errors(String expression) sync* { | ||
if (!expression.startsWith('/')) { | ||
yield 'Expression MUST start with "/".'; | ||
} | ||
if (_danglingTilda.hasMatch(expression)) { | ||
yield 'Tilda("~") MUST be followed by "0" or "1".'; | ||
} | ||
} | ||
|
||
static String _unescape(String s) => | ||
s.replaceAll('~1', '/').replaceAll('~0', '~'); | ||
|
||
static final _danglingTilda = RegExp(r'~[^01]|~$'); | ||
|
||
/// Reads the referenced value from the [document]. | ||
/// If no value is referenced, tries to return the result of [orElse]. | ||
/// Otherwise throws [InvalidRoute]. | ||
dynamic read(document, {dynamic Function()? orElse}) => document; | ||
|
||
/// Returns a new writable JSON Pointer by appending a new [reference] at the end. | ||
WritableJsonPointer append(String reference) => | ||
WritableJsonPointer(JsonPointerReference.parse(reference), parent: this); | ||
|
||
@override | ||
String toString() => ''; | ||
} | ||
|
||
/// A writable JSON Pointer. | ||
class WritableJsonPointer extends JsonPointer { | ||
/// Creates a new instance of [WritableJsonPointer] from the [reference]. | ||
/// If [parent] is passed, the [reference] will be appended to it. | ||
const WritableJsonPointer(this.reference, | ||
{this.parent = const JsonPointer()}); | ||
|
||
/// The rightmost reference in the pointer | ||
final JsonPointerReference reference; | ||
|
||
/// The parent pointer | ||
final JsonPointer parent; | ||
|
||
@override | ||
dynamic read(dynamic document, {dynamic Function()? orElse}) { | ||
try { | ||
return reference.read(parent.read(document)); | ||
} on InvalidRoute catch (e) { | ||
if (orElse != null) return orElse(); | ||
throw _wrapped(e); | ||
} | ||
} | ||
|
||
/// Replaces the referenced value in the [document] with a [newValue]. | ||
/// When a non-existing [Map] (JSON Object) key is referred, it will be added to the map. | ||
/// When a new index in a [List] (JSON Array) is referred, it will be appended to the list. | ||
/// Otherwise throws [InvalidRoute]. | ||
void write(dynamic document, dynamic newValue) { | ||
try { | ||
reference.write(parent.read(document), newValue); | ||
} on InvalidRoute catch (e) { | ||
throw _wrapped(e); | ||
} | ||
} | ||
|
||
@override | ||
String toString() => '$parent$reference'; | ||
|
||
InvalidRoute _wrapped(InvalidRoute e) => InvalidRoute( | ||
'No value is referenced by $this. Failed reference: ${e.pointer}'); | ||
} |
Oops, something went wrong.