From 71b9bd7a1be137abf37f31eb0f6519ac2178f792 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Tue, 16 Apr 2024 19:08:19 -0400 Subject: [PATCH] feat: add qos normifier --- normify.go | 13 +++++++++++++ normify_test.go | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/normify.go b/normify.go index 09a8a7f..cae8bcd 100644 --- a/normify.go +++ b/normify.go @@ -195,6 +195,19 @@ func SetSessionID(sessionID string) NormifierOption { }) } +// ClampQualityOfService clamps a wrp message's qos value between 0 and 99. +func ClampQualityOfService() NormifierOption { + return optionFunc(func(m *Message) error { + if m.QualityOfService < 0 { + m.QualityOfService = 0 + } else if m.QualityOfService > 99 { + m.QualityOfService = 99 + } + + return nil + }) +} + // EnsureMetadataString ensures that the message has the given string metadata. // This will always set the metadata. func EnsureMetadataString(key, value string) NormifierOption { diff --git a/normify_test.go b/normify_test.go index 66855f8..7aa87be 100644 --- a/normify_test.go +++ b/normify_test.go @@ -88,6 +88,24 @@ func TestNormifier(t *testing.T) { want: Message{ SessionID: "session", }, + }, { + description: "ClampQualityOfService(), QualityOfService < 0", + opt: ClampQualityOfService(), + msg: Message{ + QualityOfService: -1, + }, + want: Message{ + QualityOfService: 0, + }, + }, { + description: "ClampQualityOfService(), QualityOfService > 99", + opt: ClampQualityOfService(), + msg: Message{ + QualityOfService: 100, + }, + want: Message{ + QualityOfService: 99, + }, }, { description: "EnsureMetadataString(key, value) add to empty", opt: EnsureMetadataString("key", "value"),