Skip to content

Commit

Permalink
feat!: improve deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 2, 2024
1 parent 237a259 commit 5b0f322
Show file tree
Hide file tree
Showing 36 changed files with 693 additions and 1,149 deletions.
2 changes: 1 addition & 1 deletion lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension CoapFormExtension on AugmentedForm {
return BlockSize.fromDecodedValue(value);
// ignore: avoid_catching_errors
} on ArgumentError {
throw ValidationException(
throw FormatException(
"Encountered invalid blocksize $value in CoAP form",
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/binding_mqtt/mqtt_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extension MqttFormExtension on AugmentedForm {

// TODO: This validation should maybe already happen earlier.
if (qosValue != null) {
throw ValidationException(
throw FormatException(
"Encountered unknown QoS value $qosValue. "
"in form with href $href of Thing Description with Identifier "
"$tdIdentifier.",
Expand Down
1 change: 0 additions & 1 deletion lib/src/core/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ export "definitions/security/psk_security_scheme.dart";
export "definitions/security/security_scheme.dart";
export "definitions/thing_description.dart";
export "definitions/thing_model.dart";
export "definitions/validation/thing_description_schema.dart";
10 changes: 4 additions & 6 deletions lib/src/core/definitions/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import "package:collection/collection.dart";
import "package:curie/curie.dart";
import "package:meta/meta.dart";

import "../exceptions.dart";

const _tdVersion10ContextUrl = "https://www.w3.org/2019/wot/td/v1";
const _tdVersion11ContextUrl = "https://www.w3.org/2022/wot/td/v1.1";

Expand All @@ -30,14 +28,14 @@ final class Context {
final firstContextEntry = contextEntries.firstOrNull;

if (firstContextEntry is! SingleContextEntry) {
throw const ValidationException("Missing TD context URL.");
throw const FormatException("Missing TD context URL.");
}

final firstContextValue = firstContextEntry.value;

if (![_tdVersion10ContextUrl, _tdVersion11ContextUrl]
.contains(firstContextValue)) {
throw ValidationException(
throw FormatException(
"Encountered invalid TD context URL $firstContextEntry",
);
}
Expand Down Expand Up @@ -142,12 +140,12 @@ final class SingleContextEntry extends ContextEntry {
/// Creates a new [SingleContextEntry] from a [string] that represents a URI.
///
/// If the [string] should not be a valid URI, this factory constructor will
/// throw a [ValidationException].
/// throw a [FormatException].
factory SingleContextEntry.fromString(String string) {
final parsedUri = Uri.tryParse(string);

if (parsedUri == null) {
throw ValidationException("Encountered invalid URI $string");
throw FormatException("Encountered invalid URI $string");
}

return SingleContextEntry(parsedUri);
Expand Down
11 changes: 8 additions & 3 deletions lib/src/core/definitions/data_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ class DataSchema {
Set<String>? parsedFields,
]) {
parsedFields = parsedFields ?? {};
final atType = json.parseArrayField<String>("@type", parsedFields);
final atType =
json.parseArrayField<String>("@type", parsedFields: parsedFields);
final title = json.parseField<String>("title", parsedFields);
final titles = json.parseMapField<String>("titles", parsedFields);
final description = json.parseField<String>("description", parsedFields);
final descriptions =
json.parseMapField<String>("descriptions", parsedFields);
final constant = json.parseField<Object>("constant", parsedFields);
final defaultValue = json.parseField<Object>("default", parsedFields);
final enumeration = json.parseField<List<Object>>("enum", parsedFields);
final enumeration = json.parseArrayField<Object?>(
"enum",
parsedFields: parsedFields,
minimalSize: 1,
);
final readOnly = json.parseField<bool>("readOnly", parsedFields);
final writeOnly = json.parseField<bool>("writeOnly", parsedFields);
final format = json.parseField<String>("format", parsedFields);
Expand Down Expand Up @@ -166,7 +171,7 @@ class DataSchema {
final List<DataSchema>? oneOf;

/// Restricted set of values provided as a [List].
final List<Object>? enumeration;
final List<Object?>? enumeration;

/// Indicates if a value is read only.
final bool? readOnly;
Expand Down
Loading

0 comments on commit 5b0f322

Please sign in to comment.