Skip to content

Commit

Permalink
Make Payload Output of TryDecode Method a byte[] Type
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanAgnihotri committed Aug 23, 2021
1 parent 9e87a9e commit 0995162
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Likewise, you can make use of MessagePack or Protocol Buffer or even System.Text
Decoding a Branca token for validation is just as simple:

```c#
if (branca.TryDecode(token, out Span<byte> payload))
if (branca.TryDecode(token, out byte[] payload))
{
// Hello, World!
string message = Encoding.UTF8.GetString(payload);
Expand All @@ -99,7 +99,7 @@ Decoding validates the token and even confirms the expiry (if you have setup som
For whatever reason, if you also want to get the time the Branca token was created, you can do the following:

```c#
if (branca.TryDecode(token, out Span<byte> payload, out uint createTime))
if (branca.TryDecode(token, out byte[] payload, out uint createTime))
{
// successful decoding
}
Expand Down Expand Up @@ -176,7 +176,7 @@ string token = branca.Encode(bytes);
```

```c#
if (branca.TryDecode(token, out Span<byte> data, out uint createTime))
if (branca.TryDecode(token, out byte[] data, out uint createTime))
{
var decodedPayload = JsonSerializer.Deserialize<Payload>(data, options);

Expand Down
10 changes: 5 additions & 5 deletions src/Branca/BrancaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public string Encode(ReadOnlySpan<byte> payload, uint createTime)
return Base62.Encode(token);
}

public bool TryDecode(string token, out Span<byte> payload)
public bool TryDecode(string token, out byte[] payload)
{
return TryDecode(token, out payload, out _);
}

public bool TryDecode(
string token,
out Span<byte> payload,
out byte[] payload,
out uint createTime)
{
ReadOnlySpan<byte> data = Base62.Decode(token);
Expand All @@ -125,7 +125,7 @@ public bool TryDecode(string token, out Span<byte> payload)

if (version.IsEmpty || version[0] != Version)
{
payload = Span<byte>.Empty;
payload = Array.Empty<byte>();
createTime = default;

return false;
Expand All @@ -138,7 +138,7 @@ public bool TryDecode(string token, out Span<byte> payload)

if (_lifetime.HasValue && _lifetime < _timer.UnixNow - creationTime)
{
payload = Span<byte>.Empty;
payload = Array.Empty<byte>();
createTime = default;

return false;
Expand Down Expand Up @@ -173,7 +173,7 @@ public bool TryDecode(string token, out Span<byte> payload)
}
catch (Exception)
{
payload = Span<byte>.Empty;
payload = Array.Empty<byte>();
createTime = default;

return false;
Expand Down
7 changes: 3 additions & 4 deletions test/Branca.Tests/BrancaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Branca.Tests
{
using System;
using System.Text;
using Xunit;
using static TestData;

Expand All @@ -22,7 +21,7 @@ public void EncodeAndDecodeValidCasesProperly(BrancaState data)
Assert.Equal(data.Token, token);

bool isValid = branca.TryDecode(
data.Token, out Span<byte> message, out uint timestamp);
data.Token, out byte[] message, out uint timestamp);

Assert.StrictEqual(data.IsValid, isValid);

Expand All @@ -38,10 +37,10 @@ public void FailToDecodeInvalidCases(BrancaState data)
BrancaService branca = BrancaFactory.Create(data.Key, data.Timestamp);

bool isValid = branca.TryDecode(
data.Token, out Span<byte> message, out uint timestamp);
data.Token, out byte[] message, out uint timestamp);

Assert.StrictEqual(data.IsValid, isValid);
Assert.True(message == Span<byte>.Empty);
Assert.True(message == Array.Empty<byte>());
Assert.True(timestamp == default);
}

Expand Down

0 comments on commit 0995162

Please sign in to comment.