Skip to content

Commit 8e012d7

Browse files
authored
Merge pull request #98 from eclipse-thingweb/coapsubprotocol-parse
feat(binding_coap): add tryParse method to CoapSubprotocol
2 parents 30a9cf8 + a4a7626 commit 8e012d7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/src/binding_coap/coap_definitions.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ enum CoapRequestMethod {
5555
enum CoapSubprotocol {
5656
/// Subprotocol for observing CoAP resources.
5757
observe,
58+
;
59+
60+
/// Tries to match the given [subprotocol] string to one of the known
61+
/// [CoapSubprotocol.values].
62+
static CoapSubprotocol? tryParse(String subprotocol) {
63+
if (subprotocol == "cov:observe") {
64+
return CoapSubprotocol.observe;
65+
}
66+
67+
return null;
68+
}
5869
}

test/binding_coap/coap_definitions_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import "package:dart_wot/src/binding_coap/coap_extensions.dart";
1111
import "package:test/test.dart";
1212

1313
void main() {
14-
group("CoAP definitions", () {
15-
test("should deserialize CoAP Forms", () async {
14+
group("CoAP definitions should", () {
15+
test("deserialize CoAP Forms", () async {
1616
const thingDescriptionJson = {
1717
"@context": [
1818
"https://www.w3.org/2022/wot/td/v1.1",
@@ -88,4 +88,12 @@ void main() {
8888
);
8989
});
9090
});
91+
92+
test("parse CoAP subprotocols", () async {
93+
final observeSubprotocol = CoapSubprotocol.tryParse("cov:observe");
94+
expect(observeSubprotocol == CoapSubprotocol.observe, isTrue);
95+
96+
final unknownSubprotocol = CoapSubprotocol.tryParse("foobar");
97+
expect(unknownSubprotocol, isNull);
98+
});
9199
}

0 commit comments

Comments
 (0)