Skip to content

Commit

Permalink
feat: introduce toThingDescription extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 3, 2024
1 parent 68e41c9 commit 55c4913
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future<void> main(List<String> args) async {
},
};

final thingDescription = ThingDescription.fromJson(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value = await status.value();
Expand Down
2 changes: 1 addition & 1 deletion example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Future<void> main() async {
);
final wot = await servient.start();

final thingDescription = ThingDescription.fromJson(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value1 = await status.value();
Expand Down
2 changes: 1 addition & 1 deletion example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Future<void> main(List<String> args) async {
);
final wot = await servient.start();

final thingDescription = ThingDescription.fromJson(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");

Expand Down
2 changes: 1 addition & 1 deletion example/mqtt_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Future<void> main(List<String> args) async {

final wot = await servient.start();

final thingDescription = ThingDescription.fromJson(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
await consumedThing.readAndPrintProperty("status");

Expand Down
8 changes: 8 additions & 0 deletions lib/src/definitions/thing_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,11 @@ class ThingDescription {
return id ?? base?.toString() ?? title;
}
}

/// Extension for generating [ThingDescription]s from [Map]s more easily.
extension ToThingDescription on Map<String, dynamic> {
/// Tries to generate a [ThingDescription] from this [Map] object.
ThingDescription toThingDescription() {
return ThingDescription.fromJson(this);
}
}

0 comments on commit 55c4913

Please sign in to comment.