Skip to content

Commit

Permalink
feat: parse affordance title(s) and description(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 12, 2022
1 parent bd457d5 commit 9ae5ab4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
8 changes: 1 addition & 7 deletions lib/src/definitions/interaction_affordances/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import 'interaction_affordance.dart';

/// Class representing an [Action] Affordance in a Thing Description.
class Action extends InteractionAffordance {
/// The default title of this [Action].
String? title;

/// The default description of this [Action].
String? description;

/// The schema of the [input] data this [Action] accepts.
DataSchema? input;

Expand All @@ -31,6 +25,6 @@ class Action extends InteractionAffordance {

/// Creates a new [Action] from a [json] object.
Action.fromJson(Map<String, dynamic> json) : super([]) {
parseForms(json);
parseAffordanceFields(json);
}
}
8 changes: 1 addition & 7 deletions lib/src/definitions/interaction_affordances/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ import 'interaction_affordance.dart';

/// Class representing an [Event] Affordance in a Thing Description.
class Event extends InteractionAffordance {
/// The default title of this [Event].
String? title;

/// The default description of this [Event].
String? description;

/// Creates a new [Event] from a [List] of [forms].
Event(List<Form> forms) : super(forms);

/// Creates a new [Event] from a [json] object.
Event.fromJson(Map<String, dynamic> json) : super([]) {
parseForms(json);
parseAffordanceFields(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import '../form.dart';

/// Base class for Interaction Affordances (Properties, Actions, and Events).
abstract class InteractionAffordance {
/// The default [title] of this [InteractionAffordance].
String? title;

/// Multilanguage [titles] of this [InteractionAffordance].
Map<String, String>? titles;

/// The default [description] of this [InteractionAffordance].
String? description;

/// Multilanguage [descriptions] of this [InteractionAffordance].
Map<String, String>? descriptions;

/// The basic [forms] which can be used for interacting with this resource.
List<Form> forms;

Expand All @@ -21,14 +33,49 @@ abstract class InteractionAffordance {
List<Form> augmentedForms = [];

/// Parses [forms] represented by a [json] object.
void parseForms(Map<String, dynamic> json) {
void _parseForms(Map<String, dynamic> json) {
for (final formJson in json["forms"]) {
if (formJson is Map<String, dynamic>) {
forms.add(Form.fromJson(formJson));
}
}
}

Map<String, String>? _parseMultilangString(
Map<String, dynamic> json, String jsonKey) {
Map<String, String>? field;
final dynamic jsonEntries = json[jsonKey];
if (jsonEntries is Map<String, dynamic>) {
field = {};
for (final entry in jsonEntries.entries) {
final dynamic value = entry.value;
if (value is String) {
field[entry.key] = value;
}
}
}
return field;
}

/// Parses the [InteractionAffordance] contained in a [json] object.
void parseAffordanceFields(Map<String, dynamic> json) {
_parseForms(json);

final dynamic title = json["title"];
if (title is String) {
this.title = title;
}

titles = _parseMultilangString(json, "titles");

final dynamic description = json["description"];
if (description is String) {
this.description = description;
}

descriptions = _parseMultilangString(json, "descriptions");
}

/// Creates a new [InteractionAffordance]. Accepts a [List] of [forms].
InteractionAffordance(this.forms);
}
14 changes: 1 addition & 13 deletions lib/src/definitions/interaction_affordances/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class Property extends InteractionAffordance implements DataSchema {
@override
Object? defaultValue;

@override
String? description;

@override
Map<String, String>? descriptions;

@override
List<Object>? enumeration;

Expand All @@ -41,12 +35,6 @@ class Property extends InteractionAffordance implements DataSchema {
@override
bool? readOnly;

@override
String? title;

@override
Map<String, String>? titles;

@override
String? type;

Expand All @@ -61,7 +49,7 @@ class Property extends InteractionAffordance implements DataSchema {

/// Creates a new [Property] from a [json] object.
Property.fromJson(Map<String, dynamic> json) : super([]) {
parseForms(json);
parseAffordanceFields(json);
parseDataSchemaJson(this, json);
}
}

0 comments on commit 9ae5ab4

Please sign in to comment.