Skip to content

Commit

Permalink
feat: parse titles and descriptions at Thing level
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 20, 2022
1 parent 199e870 commit a523560
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/src/definitions/thing_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class ThingDescription {
/// The [title] of this [ThingDescription].
late String title;

/// The [description] of this [ThingDescription].
String? description;

/// A [Map] of multi-language [titles].
final Map<String, String> titles = {};

/// A [Map] of multi-language [descriptions].
final Map<String, String> descriptions = {};

/// The JSON-LD `@context`, represented by a [List] of [ContextEntry]s.
List<ContextEntry> context = [];

Expand Down Expand Up @@ -81,6 +90,12 @@ class ThingDescription {
if (base is String) {
this.base = base;
}
final dynamic description = json["description"];
if (description is String) {
this.description = description;
}
_parseMultilangString(titles, json, "titles");
_parseMultilangString(descriptions, json, "descriptions");
final dynamic properties = json["properties"];
if (properties is Map<String, dynamic>) {
_parseProperties(properties);
Expand All @@ -103,6 +118,20 @@ class ThingDescription {
}
}

// TODO(JKRhb): Refactor
void _parseMultilangString(
Map<String, String> field, Map<String, dynamic> json, String jsonKey) {
final dynamic jsonEntries = json[jsonKey];
if (jsonEntries is Map<String, dynamic>) {
for (final entry in jsonEntries.entries) {
final dynamic value = entry.value;
if (value is String) {
field[entry.key] = value;
}
}
}
}

void _parseTitle(dynamic titleJson) {
if (titleJson is String) {
title = titleJson;
Expand Down
10 changes: 10 additions & 0 deletions test/consumed_thing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ void main() {
{
"@context": ["http://www.w3.org/ns/td"],
"title": "Test Thing",
"titles": {
"en": "Test Thing"
},
"description": "A Test Thing used for Testing.",
"descriptions": {
"en": "A Test Thing used for Testing."
},
"securityDefinitions": {
"nosec_sc": {
"scheme": "nosec"
Expand Down Expand Up @@ -81,6 +88,9 @@ void main() {
final parsedTd = ThingDescription(thingDescriptionJson);

expect(parsedTd.title, "Test Thing");
expect(parsedTd.titles, {"en": "Test Thing"});
expect(parsedTd.description, "A Test Thing used for Testing.");
expect(parsedTd.descriptions, {"en": "A Test Thing used for Testing."});

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

0 comments on commit a523560

Please sign in to comment.