Browse Source

Merge pull request #886 from PMExtra/feature-userproperties

GetUserProperty extension.
release/3.x.x
Christian 4 years ago
committed by GitHub
parent
commit
3e096bc353
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 0 deletions
  1. +1
    -0
      Build/MQTTnet.nuspec
  2. +32
    -0
      Source/MQTTnet/Extensions/UserPropertyExtension.cs
  3. +1
    -0
      Source/MQTTnet/MQTTnet.csproj
  4. +33
    -0
      Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs

+ 1
- 0
Build/MQTTnet.nuspec View File

@@ -22,6 +22,7 @@
* [Server] Added interceptor for unsubscriptions.
* [MQTTnet.Server] Added interceptor for unsubscriptions.
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1
* [Core] Added MqttApplicationMessage.GetUserProperty() convenience method (thanks to @PMExtra).
* [Client] Support WithConnectionUri to configure client (thanks to @PMExtra).
* [Server] Removed exceptions when user properties are set with MQTT protocol version 3.1
</releaseNotes>


+ 32
- 0
Source/MQTTnet/Extensions/UserPropertyExtension.cs View File

@@ -0,0 +1,32 @@
using System;
using System.ComponentModel;
using System.Linq;

namespace MQTTnet.Extensions
{
public static class UserPropertyExtension
{
public static string GetUserProperty(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
{
if (message == null) throw new ArgumentNullException(nameof(message));
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));

return message.UserProperties?.SingleOrDefault(up => up.Name.Equals(propertyName, comparisonType))?.Value;
}

public static T GetUserProperty<T>(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
{
var value = GetUserProperty(message, propertyName, comparisonType);

var typeDescriptor = TypeDescriptor.GetConverter(typeof(T));
try
{
return (T) typeDescriptor.ConvertFromString(value);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Cannot convert value({value ?? "null"}) of UserProperty({propertyName}) to {typeof(T).FullName}.", ex);
}
}
}
}

+ 1
- 0
Source/MQTTnet/MQTTnet.csproj View File

@@ -42,6 +42,7 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
<PackageReference Include="System.Net.Security" Version="4.3.2" />
<PackageReference Include="System.Net.WebSockets" Version="4.3.0" />
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.2" />


+ 33
- 0
Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Extensions;
using MQTTnet.Packets;

namespace MQTTnet.Tests
{
[TestClass]
public class MqttApplicationMessage_Tests
{
[TestMethod]
public void GetUserProperty_Test()
{
var message = new MqttApplicationMessage
{
UserProperties = new List<MqttUserProperty>
{
new MqttUserProperty("foo", "bar"),
new MqttUserProperty("value", "1011"),
new MqttUserProperty("CASE", "insensitive")
}
};

Assert.AreEqual("bar", message.GetUserProperty("foo"));
Assert.AreEqual(1011, message.GetUserProperty<int>("value"));
Assert.AreEqual("insensitive", message.GetUserProperty("case"));
Assert.AreEqual(null, message.GetUserProperty("nonExists"));
Assert.AreEqual(null, message.GetUserProperty<int?>("nonExists"));
Assert.ThrowsException<InvalidOperationException>(() => message.GetUserProperty<int>("nonExists"));
}
}
}

Loading…
Cancel
Save