Created
April 10, 2024 19:07
-
-
Save fredgrott/800b2afd56ddd219638b4978f63ee220 to your computer and use it in GitHub Desktop.
resolver json
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
// 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