Skip to content

Commit

Permalink
Serialize BindingKey contents to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Sep 28, 2023
1 parent 8956a91 commit 5d50d29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Abc.Zebus.Routing;
using Abc.Zebus.Testing.Extensions;
using Newtonsoft.Json;
using NUnit.Framework;

namespace Abc.Zebus.Tests.Routing
Expand All @@ -15,5 +16,14 @@ public void should_use_special_char_for_empty_binding_key()

empty.ToString().ShouldEqual("#");
}

[Test]
public void should_serialize_parts_to_json()
{
var bindingKey = new BindingKey("foobar", "*");
var json = JsonConvert.SerializeObject(bindingKey);
json.ShouldContain("foobar");
json.ShouldContain("*");
}
}
}
10 changes: 8 additions & 2 deletions src/Abc.Zebus/Routing/BindingKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Abc.Zebus.Util.Extensions;
using JetBrains.Annotations;
using Newtonsoft.Json;
using ProtoBuf;

namespace Abc.Zebus.Routing
Expand All @@ -12,21 +13,26 @@ namespace Abc.Zebus.Routing
[ProtoContract]
public readonly struct BindingKey : IEquatable<BindingKey>
{
public static readonly BindingKey Empty = new BindingKey();
public static readonly BindingKey Empty = new();

[ProtoMember(1, IsRequired = true)]
private readonly string[]? _parts;

public BindingKey(params string[] parts)
public BindingKey(params string[]? parts)
{
if (parts == null || parts.Length == 0)
_parts = null;
else
_parts = parts;
}

[JsonProperty]
public IReadOnlyList<string> Parts => _parts ?? Array.Empty<string>();

[JsonIgnore]
public int PartCount => _parts?.Length ?? 0;

[JsonIgnore]
public bool IsEmpty => _parts == null || _parts.Length == 1 && IsSharp(0);

public bool IsSharp(int index)
Expand Down

0 comments on commit 5d50d29

Please sign in to comment.