Skip to content

Commit

Permalink
Carael/handle nullable application properties (#49)
Browse files Browse the repository at this point in the history
* Handle nullable application properties - defaults type to string if null 
Fixes #44
  • Loading branch information
Carael authored Mar 8, 2024
1 parent 27e6f38 commit 01897b0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Core/ServiceBus.Contracts/Types/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public record Message(
string? Subject,
string Body,
MessageSystemProperties SystemProperties,
IReadOnlyDictionary<string, object>? ApplicationProperties);
IReadOnlyDictionary<string, object?>? ApplicationProperties);
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ namespace CrossBusExplorer.ServiceBus.Extensions
{
public static class ApplicationPropertyTypeExtensions
{
public static ApplicationPropertyType GetApplicationPropertyType(this object value) =>
public static ApplicationPropertyType GetApplicationPropertyType(this object? value) =>
value switch
{
null => ApplicationPropertyType.String,
string _ => ApplicationPropertyType.String,
bool _ => ApplicationPropertyType.Bool,
byte _ => ApplicationPropertyType.Byte,
Expand All @@ -24,16 +25,21 @@ public static ApplicationPropertyType GetApplicationPropertyType(this object val
Guid _ => ApplicationPropertyType.Guid,
DateTime _ => ApplicationPropertyType.DateTime,
DateTimeOffset _ => ApplicationPropertyType.DateTimeOffset,
Stream _ => ApplicationPropertyType.Stream,
Uri _ => ApplicationPropertyType.Uri,
TimeSpan _ => ApplicationPropertyType.TimeSpan,
_ => throw new ArgumentOutOfRangeException($"Type {value.GetType()} is not supported")
_ => throw new ArgumentOutOfRangeException(
$"Type {value?.GetType()} is not supported")
};

public static object GetApplicationPropertyValue(
this string value,
public static object? GetApplicationPropertyValue(
this string? value,
ApplicationPropertyType type)
{
if (value == null)
{
return value;
}

switch (type)
{
case ApplicationPropertyType.String:
Expand Down Expand Up @@ -84,8 +90,6 @@ public static object GetApplicationPropertyValue(
return DateTime.Parse(value);
case ApplicationPropertyType.DateTimeOffset:
return DateTimeOffset.Parse(value);
case ApplicationPropertyType.Stream:
throw new InvalidOperationException("Cannot parse Stream from string.");
case ApplicationPropertyType.Uri:
return new Uri(value);
case ApplicationPropertyType.TimeSpan:
Expand Down
2 changes: 1 addition & 1 deletion src/Ui/Website.Host/electron.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"appId": "com.crossbusexplorer.app",
"productName": "Cross Bus Explorer",
"copyright": "Copyright © 2024",
"buildVersion": "0.4.5",
"buildVersion": "0.4.6",
"compression": "maximum",
"directories": {
"output": "../../../bin/Desktop"
Expand Down
2 changes: 1 addition & 1 deletion src/Ui/Website/Mappings/MessageMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static MessageDetailsModel ToSendMessageModel(this Message message) =>
new KeyValueTypePair
{
Key = p.Key,
Value = p.Value.ToString() ?? string.Empty,
Value = p.Value?.ToString() ?? null,
Type = p.Value.GetApplicationPropertyType()
}).ToList() ?? new List<KeyValueTypePair>())
};
Expand Down
4 changes: 2 additions & 2 deletions src/Ui/Website/Models/KeyValueTypePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public string Key
}
}

private string _value;
private string? _value;

public string Value
public string? Value
{
get => _value;
set
Expand Down

0 comments on commit 01897b0

Please sign in to comment.