Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created April 10, 2024 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredgrott/800b2afd56ddd219638b4978f63ee220 to your computer and use it in GitHub Desktop.
Save fredgrott/800b2afd56ddd219638b4978f63ee220 to your computer and use it in GitHub Desktop.
resolver json
// Copyright 2024 Fredrick Allan Hrott. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Modified from original by codable BSD 2 clause
// license 2019.
import 'package:json_helpers/json_helpers/keyed_archive.dart';
class ReferenceResolver {
ReferenceResolver(this.document);
final KeyedArchive document;
/// resolves a reference of the form '#/yyy/xxx'
/// To the value stored in a document
///
/// e.g.
/// if [ref] == '#/definitions/child' then we would
/// return a [KeyedArchive] with the child named Sally.
///
/// ```
/// {
/// "definitions": {
/// "child": {"name": "Sally"}
/// },
/// "root": {
/// "name": "Bob",
/// "child": {"\$ref": "#/definitions/child"}
///}
/// ```
KeyedArchive? resolve(Uri ref) {
final folded = ref.pathSegments.fold<KeyedArchive?>(document, (KeyedArchive? objectPtr, pathSegment) {
if (objectPtr != null) {
return objectPtr[pathSegment] as KeyedArchive?;
} else {
return null;
}
});
return folded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment