From 52620cf7c573084c85cfd170fbacbd67e105b3b8 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Fri, 29 Dec 2023 03:20:01 +0100 Subject: [PATCH] fixup! feat: improve DataSchemaValue handling --- lib/src/core/content_serdes.dart | 13 +++++++++---- lib/src/core/servient.dart | 5 +++-- lib/src/core/thing_discovery.dart | 9 +++++---- lib/src/core/wot.dart | 7 +++---- test/core/content_serdes_test.dart | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/src/core/content_serdes.dart b/lib/src/core/content_serdes.dart index 58127822..534c850d 100644 --- a/lib/src/core/content_serdes.dart +++ b/lib/src/core/content_serdes.dart @@ -139,7 +139,10 @@ class ContentSerdes { return _codecs[parsedMediaType]; } - void _validateValue(DataSchemaValue dataSchemaValue, DataSchema? dataSchema) { + void _validateValue( + DataSchemaValue dataSchemaValue, + DataSchema? dataSchema, + ) { // TODO(JKRhb): The process of validating values according to a dataschema // needs to be reworked. const filteredKeys = ['uriVariables']; @@ -164,7 +167,7 @@ class ContentSerdes { /// conversion. If the[mediaType] is not supported, the method will try to /// try to treat it as a UTF-8 string. Content valueToContent( - DataSchemaValue? value, + DataSchemaValue? value, DataSchema? dataSchema, String? mediaType, ) { @@ -178,6 +181,7 @@ class ContentSerdes { final mimeType = parsedMediaType.mimeType; final parameters = parsedMediaType.parameters; + // TODO(JKRhb): Reevaluate usage of TextCodec here final codec = _getCodecFromMediaType(mimeType) ?? TextCodec(); final bytes = codec.valueToBytes(value, dataSchema, parameters); @@ -189,7 +193,7 @@ class ContentSerdes { /// A [dataSchema] can be passed for validating the result. If the media type /// specified in the [content] is not supported, the method willl try to /// convert its body to an UTF-8 string. - Future contentToValue( + Future?> contentToValue( Content content, DataSchema? dataSchema, ) async { @@ -198,6 +202,7 @@ class ContentSerdes { final parameters = parsedMediaType.parameters; final bytes = await content.toByteList(); + // TODO(JKRhb): Reevaluate usage of TextCodec here final codec = _getCodecFromMediaType(mimeType) ?? TextCodec(); final value = codec.bytesToValue(bytes, dataSchema, parameters); @@ -205,6 +210,6 @@ class ContentSerdes { if (value != null) { _validateValue(value, dataSchema); } - return value?.value; + return value; } } diff --git a/lib/src/core/servient.dart b/lib/src/core/servient.dart index bec82db4..d6604f80 100644 --- a/lib/src/core/servient.dart +++ b/lib/src/core/servient.dart @@ -8,6 +8,7 @@ import 'package:uuid/uuid.dart'; import '../definitions/interaction_affordances/interaction_affordance.dart'; import '../definitions/thing_description.dart'; +import '../scripting_api/data_schema_value.dart'; import 'consumed_thing.dart'; import 'content_serdes.dart'; import 'credentials/callbacks.dart'; @@ -234,12 +235,12 @@ class Servient { final value = await contentSerdes.contentToValue(content, null); - if (value is! Map) { + if (value is! DataSchemaValue>) { throw DiscoveryException( 'Could not parse Thing Description obtained from $url', ); } - return ThingDescription.fromJson(value); + return ThingDescription.fromJson(value.value); } } diff --git a/lib/src/core/thing_discovery.dart b/lib/src/core/thing_discovery.dart index e513be38..6eaca6be 100644 --- a/lib/src/core/thing_discovery.dart +++ b/lib/src/core/thing_discovery.dart @@ -13,6 +13,7 @@ import 'package:multicast_dns/multicast_dns.dart'; import '../../core.dart'; import '../../scripting_api.dart' as scripting_api; import '../definitions/thing_description.dart'; +import '../scripting_api/data_schema_value.dart'; import '../scripting_api/discovery/discovery_method.dart'; import 'content.dart'; @@ -106,13 +107,13 @@ class ThingDiscovery extends Stream DiscoveryContent content, ) async { final value = await _servient.contentSerdes.contentToValue(content, null); - if (value is! Map) { + if (value is! DataSchemaValue>) { throw DiscoveryException( 'Could not parse Thing Description obtained from ${content.sourceUri}', ); } - return ThingDescription.fromJson(value); + return ThingDescription.fromJson(value.value); } Stream _discoverDirectly(Uri uri) async* { @@ -126,8 +127,8 @@ class ThingDiscovery extends Stream Future?> _getCoreWebLinks(Content content) async { final value = await _servient.contentSerdes.contentToValue(content, null); - if (value is String) { - return CoapLinkFormat.parse(value).toList(); + if (value is DataSchemaValue) { + return CoapLinkFormat.parse(value.value).toList(); } return null; diff --git a/lib/src/core/wot.dart b/lib/src/core/wot.dart index cbdb1b1a..3953be85 100644 --- a/lib/src/core/wot.dart +++ b/lib/src/core/wot.dart @@ -8,7 +8,6 @@ import 'dart:async'; import '../../scripting_api.dart' as scripting_api; import '../definitions/thing_description.dart'; -import '../scripting_api/discovery/discovery_method.dart'; import 'consumed_thing.dart'; import 'exposed_thing.dart'; import 'servient.dart'; @@ -89,7 +88,7 @@ class WoT implements scripting_api.WoT { ThingDiscovery discover( Uri url, { scripting_api.ThingFilter? thingFilter, - DiscoveryMethod method = DiscoveryMethod.direct, + scripting_api.DiscoveryMethod method = scripting_api.DiscoveryMethod.direct, }) { return ThingDiscovery(url, thingFilter, _servient, method: method); } @@ -118,7 +117,7 @@ class WoT implements scripting_api.WoT { await consumedDirectoryThing.readProperty('things'); final rawThingDescriptions = await interactionOutput.value(); - if (rawThingDescriptions is! List) { + if (rawThingDescriptions is! scripting_api.DataSchemaValue>) { throw DiscoveryException( 'Expected an array of Thing Descriptions but received an ' 'invalid output instead.', @@ -126,7 +125,7 @@ class WoT implements scripting_api.WoT { } final thingDescriptionStream = Stream.fromIterable( - rawThingDescriptions.whereType>(), + rawThingDescriptions.value.whereType>(), ).toThingDescriptionStream(); return ThingDiscoveryProcess(thingDescriptionStream, filter); diff --git a/test/core/content_serdes_test.dart b/test/core/content_serdes_test.dart index 78a267e2..6cb12974 100644 --- a/test/core/content_serdes_test.dart +++ b/test/core/content_serdes_test.dart @@ -29,7 +29,7 @@ void main() { expect( await contentSerdes.contentToValue(testContent1, successfulSchema), - 42, + 42.asDataSchemaValue(), ); final testContent2 = _getTestContent('42');