Skip to content

Commit

Permalink
fixup! feat: improve DataSchemaValue handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 29, 2023
1 parent ce35154 commit 52620cf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
13 changes: 9 additions & 4 deletions lib/src/core/content_serdes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ class ContentSerdes {
return _codecs[parsedMediaType];
}

void _validateValue(DataSchemaValue dataSchemaValue, DataSchema? dataSchema) {
void _validateValue(
DataSchemaValue<Object?> dataSchemaValue,
DataSchema? dataSchema,
) {
// TODO(JKRhb): The process of validating values according to a dataschema
// needs to be reworked.
const filteredKeys = ['uriVariables'];
Expand All @@ -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<Object?>? value,
DataSchema? dataSchema,
String? mediaType,
) {
Expand All @@ -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);
Expand All @@ -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<Object?> contentToValue(
Future<DataSchemaValue<Object?>?> contentToValue(
Content content,
DataSchema? dataSchema,
) async {
Expand All @@ -198,13 +202,14 @@ 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);

if (value != null) {
_validateValue(value, dataSchema);
}
return value?.value;
return value;
}
}
5 changes: 3 additions & 2 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -234,12 +235,12 @@ class Servient {

final value = await contentSerdes.contentToValue(content, null);

if (value is! Map<String, Object?>) {
if (value is! DataSchemaValue<Map<String, Object?>>) {
throw DiscoveryException(
'Could not parse Thing Description obtained from $url',
);
}

return ThingDescription.fromJson(value);
return ThingDescription.fromJson(value.value);
}
}
9 changes: 5 additions & 4 deletions lib/src/core/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -106,13 +107,13 @@ class ThingDiscovery extends Stream<ThingDescription>
DiscoveryContent content,
) async {
final value = await _servient.contentSerdes.contentToValue(content, null);
if (value is! Map<String, Object?>) {
if (value is! DataSchemaValue<Map<String, Object?>>) {
throw DiscoveryException(
'Could not parse Thing Description obtained from ${content.sourceUri}',
);
}

return ThingDescription.fromJson(value);
return ThingDescription.fromJson(value.value);
}

Stream<ThingDescription> _discoverDirectly(Uri uri) async* {
Expand All @@ -126,8 +127,8 @@ class ThingDiscovery extends Stream<ThingDescription>
Future<List<CoapWebLink>?> _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<String>) {
return CoapLinkFormat.parse(value.value).toList();
}

return null;
Expand Down
7 changes: 3 additions & 4 deletions lib/src/core/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -118,15 +117,15 @@ class WoT implements scripting_api.WoT {
await consumedDirectoryThing.readProperty('things');
final rawThingDescriptions = await interactionOutput.value();

if (rawThingDescriptions is! List<Object?>) {
if (rawThingDescriptions is! scripting_api.DataSchemaValue<List<Object?>>) {
throw DiscoveryException(
'Expected an array of Thing Descriptions but received an '
'invalid output instead.',
);
}

final thingDescriptionStream = Stream.fromIterable(
rawThingDescriptions.whereType<Map<String, Object?>>(),
rawThingDescriptions.value.whereType<Map<String, Object?>>(),
).toThingDescriptionStream();

return ThingDiscoveryProcess(thingDescriptionStream, filter);
Expand Down
2 changes: 1 addition & 1 deletion test/core/content_serdes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {

expect(
await contentSerdes.contentToValue(testContent1, successfulSchema),
42,
42.asDataSchemaValue(),
);

final testContent2 = _getTestContent('42');
Expand Down

0 comments on commit 52620cf

Please sign in to comment.