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 30, 2023
1 parent cb999bc commit c4a6160
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
31 changes: 12 additions & 19 deletions lib/src/core/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,12 @@ class ConsumedThing implements scripting_api.ConsumedThing {
final form = clientAndForm.form;
final client = clientAndForm.client;

final Content content;

if (input == null) {
content = Content(form.contentType, const Stream<List<int>>.empty());
} else {
switch (input) {
case DataSchemaValueInput():
content = servient.contentSerdes.valueToContent(
input.dataSchemaValue,
property,
form.contentType,
);
case StreamInput():
content = Content(form.contentType, input.byteStream);
}
}
final content = Content.fromInteractionInput(
input,
form.contentType,
servient.contentSerdes,
property,
);

await client.writeResource(form, content);
}
Expand All @@ -211,7 +201,6 @@ class ConsumedThing implements scripting_api.ConsumedThing {
}) async {
// TODO(JKRhb): Refactor
final action = thingDescription.actions[actionName];
final dataSchemaValue = DataSchemaValue.tryParse(input);

if (action == null) {
throw ArgumentError(
Expand All @@ -232,8 +221,12 @@ class ConsumedThing implements scripting_api.ConsumedThing {
final form = clientAndForm.form;
final client = clientAndForm.client;

final content = servient.contentSerdes
.valueToContent(dataSchemaValue, action.input, form.contentType);
final content = Content.fromInteractionInput(
input,
form.contentType,
servient.contentSerdes,
action.input,
);

final output = await client.invokeResource(form, content);

Expand Down
33 changes: 33 additions & 0 deletions lib/src/core/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,45 @@ import 'dart:typed_data';

import 'package:typed_data/typed_data.dart';

import '../definitions/data_schema.dart';
import '../scripting_api/interaction_input.dart';
import 'content_serdes.dart';

/// This class contains binary input or output data and indicates the media
/// type this data is encoded in.
class Content {
/// Creates a new [Content] object from a media [type] and a [body].
Content(this.type, this.body);

/// Creates a new [Content] object from an [interactionInput].
///
/// If the [interactionInput] is not a [StreamInput], it will be converted to
/// a [Stream] by the referenced [contentSerdes] if it supports the specified
/// [contentType].
/// In this case, the optional [dataSchema] will be used for validation before
/// the conversion.
factory Content.fromInteractionInput(
InteractionInput? interactionInput,
String contentType,
ContentSerdes contentSerdes,
DataSchema? dataSchema,
) {
if (interactionInput == null) {
return Content(contentType, const Stream.empty());
}

switch (interactionInput) {
case DataSchemaValueInput():
return contentSerdes.valueToContent(
interactionInput.dataSchemaValue,
dataSchema,
contentType,
);
case StreamInput():
return Content(contentType, interactionInput.byteStream);
}
}

/// The media type corresponding with this [Content] object.
///
/// Examples would be `application/json` or `application/cbor`.
Expand Down

0 comments on commit c4a6160

Please sign in to comment.