Skip to content

Commit

Permalink
fix: fix parsing of multiple context values
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 23, 2023
1 parent 0b83b53 commit ab46394
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/src/definitions/context_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,38 @@ class ContextEntry {
/// Creates a new [ContextEntry].
const ContextEntry(this.value, this.key);

/// Parses a single `@context` entry from a given [json] value.
/// Parses a [List] of `@context` entries from a given [json] value.
///
/// @context extensions are added to the provided [prefixMapping].
/// If the given entry is the [firstEntry], it will be set in the
/// `@context` extensions are added to the provided [prefixMapping].
/// If a given entry is the [firstEntry], it will be set in the
/// [prefixMapping] accordingly.
factory ContextEntry.fromJson(
static List<ContextEntry> fromJson(
dynamic json,
PrefixMapping prefixMapping, {
required bool firstEntry,
}) {
// TODO: Refactor
if (json is String) {
if (firstEntry && _validTdContextValues.contains(json)) {
prefixMapping.defaultPrefixValue = json;
}
return ContextEntry(json, null);
return [ContextEntry(json, null)];
}

if (json is Map<String, dynamic>) {
final contextEntries = <ContextEntry>[];
for (final contextEntry in json.entries) {
final key = contextEntry.key;
final value = contextEntry.value;
if (value is String) {
if (!key.startsWith('@') && Uri.tryParse(value) != null) {
prefixMapping.addPrefix(key, value);
}
return ContextEntry(value, key);
contextEntries.add(ContextEntry(value, key));
}
}

return contextEntries;
}

throw ValidationException(
Expand All @@ -68,15 +72,13 @@ class ContextEntry {
var firstEntry = true;

if (json is String) {
return [
ContextEntry.fromJson(json, prefixMapping, firstEntry: firstEntry)
];
return ContextEntry.fromJson(json, prefixMapping, firstEntry: firstEntry);
}

if (json is List<dynamic>) {
final List<ContextEntry> result = [];
for (final contextEntry in json) {
result.add(
result.addAll(
ContextEntry.fromJson(
contextEntry,
prefixMapping,
Expand Down

0 comments on commit ab46394

Please sign in to comment.