Skip to content

Commit

Permalink
Merge pull request #138 from eclipse-thingweb/default-language
Browse files Browse the repository at this point in the history
feat(context.dart): add getter for default language code
  • Loading branch information
JKRhb committed May 18, 2024
2 parents 0397aad + 4dea7dc commit 4efd8fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/core/definitions/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ final class Context {
/// Used to map context extension prefixes within the `@context` to URIs.
final PrefixMapping prefixMapping;

/// Determines the default language for this `@context` if defined.
String? get defaultLanguageCode => contextEntries
.whereType<MapContextEntry>()
.where((contextEntry) => contextEntry.key == "@language")
.firstOrNull
?.value;

/// Allows for directly accessing this [Context]'s [contextEntries] by
/// [index].
ContextEntry operator [](int index) {
Expand Down
14 changes: 14 additions & 0 deletions test/core/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ void main() {
);
});

test("support determining the default language via the dedicated getter",
() {
final context1 = Context([
SingleContextEntry.fromString("https://www.w3.org/2022/wot/td/v1.1"),
const StringMapContextEntry("@language", "en"),
]);
final context2 = Context([
SingleContextEntry.fromString("https://www.w3.org/2022/wot/td/v1.1"),
]);

expect(context1.defaultLanguageCode, "en");
expect(context2.defaultLanguageCode, null);
});

test("correctly override the hashCode getter", () {
final contextEntry1 =
SingleContextEntry.fromString("https://www.w3.org/2022/wot/td/v1.1");
Expand Down

0 comments on commit 4efd8fe

Please sign in to comment.