Skip to content

Commit a523560

Browse files
committed
feat: parse titles and descriptions at Thing level
1 parent 199e870 commit a523560

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/src/definitions/thing_description.dart

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class ThingDescription {
2626
/// The [title] of this [ThingDescription].
2727
late String title;
2828

29+
/// The [description] of this [ThingDescription].
30+
String? description;
31+
32+
/// A [Map] of multi-language [titles].
33+
final Map<String, String> titles = {};
34+
35+
/// A [Map] of multi-language [descriptions].
36+
final Map<String, String> descriptions = {};
37+
2938
/// The JSON-LD `@context`, represented by a [List] of [ContextEntry]s.
3039
List<ContextEntry> context = [];
3140

@@ -81,6 +90,12 @@ class ThingDescription {
8190
if (base is String) {
8291
this.base = base;
8392
}
93+
final dynamic description = json["description"];
94+
if (description is String) {
95+
this.description = description;
96+
}
97+
_parseMultilangString(titles, json, "titles");
98+
_parseMultilangString(descriptions, json, "descriptions");
8499
final dynamic properties = json["properties"];
85100
if (properties is Map<String, dynamic>) {
86101
_parseProperties(properties);
@@ -103,6 +118,20 @@ class ThingDescription {
103118
}
104119
}
105120

121+
// TODO(JKRhb): Refactor
122+
void _parseMultilangString(
123+
Map<String, String> field, Map<String, dynamic> json, String jsonKey) {
124+
final dynamic jsonEntries = json[jsonKey];
125+
if (jsonEntries is Map<String, dynamic>) {
126+
for (final entry in jsonEntries.entries) {
127+
final dynamic value = entry.value;
128+
if (value is String) {
129+
field[entry.key] = value;
130+
}
131+
}
132+
}
133+
}
134+
106135
void _parseTitle(dynamic titleJson) {
107136
if (titleJson is String) {
108137
title = titleJson;

test/consumed_thing_test.dart

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ void main() {
1818
{
1919
"@context": ["http://www.w3.org/ns/td"],
2020
"title": "Test Thing",
21+
"titles": {
22+
"en": "Test Thing"
23+
},
24+
"description": "A Test Thing used for Testing.",
25+
"descriptions": {
26+
"en": "A Test Thing used for Testing."
27+
},
2128
"securityDefinitions": {
2229
"nosec_sc": {
2330
"scheme": "nosec"
@@ -81,6 +88,9 @@ void main() {
8188
final parsedTd = ThingDescription(thingDescriptionJson);
8289

8390
expect(parsedTd.title, "Test Thing");
91+
expect(parsedTd.titles, {"en": "Test Thing"});
92+
expect(parsedTd.description, "A Test Thing used for Testing.");
93+
expect(parsedTd.descriptions, {"en": "A Test Thing used for Testing."});
8494

8595
final statusProperty = parsedTd.properties["status"];
8696
expect(statusProperty!.title, "Status");

0 commit comments

Comments
 (0)