Skip to content

Commit

Permalink
added sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dora-gt committed Mar 4, 2018
1 parent c8435ff commit 95eb159
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Org.Interledger.Encoding.Asn.Codecs
{
public abstract class AsnPrintableStringBasedObjectCodecBase<T> : AsnCharStringBasedObjectCodecBase<T>
{
public AsnPrintableStringBasedObjectCodecBase(AsnSizeConstraint sizeConstraint) : base(sizeConstraint, System.Text.Encoding.GetEncoding("US_ASCII"))
public AsnPrintableStringBasedObjectCodecBase(AsnSizeConstraint sizeConstraint) : base(sizeConstraint, System.Text.Encoding.GetEncoding("us-ascii"))
{
Regex regex = new Regex(this.GetRegex());
this.Validator = (string charString) => regex.IsMatch(charString);
}

private string GetRegex()
{
return "[\\p{Alnum}'()+,-.?:/= ]+";
return "[a-zA-Z0-9'()+,-.?:/= ]+";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.IO;
using Xunit;

using Org.Interledger.Encoding.Asn.Codecs;

namespace Test.Org.Interledger.Encoding.Asn
{
public class TestInterledgerCodecs
{
[Fact]
public void TestTimestamp()
{
DateTime dateTime = DateTime.Now;

AsnTimestampCodec codec = new AsnTimestampCodec();
codec.Encode(dateTime);
Console.WriteLine(string.Format("encoded: {0}", codec.CharString));

DateTime decodedDateTime = codec.Decode();
Console.WriteLine(string.Format("decoded: {0}", decodedDateTime));
}
}
}
27 changes: 27 additions & 0 deletions cs-ilp-core-codecs-test/cs-ilp-core-codecs-test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Test\" />
<Folder Include="Test\Org\" />
<Folder Include="Test\Org\Interledger\" />
<Folder Include="Test\Org\Interledger\Encoding\" />
<Folder Include="Test\Org\Interledger\Encoding\Asn\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cs-ilp-codec-framework\cs-ilp-codec-framework.csproj" />
<ProjectReference Include="..\cs-ilp-core\cs-ilp-core.csproj" />
<ProjectReference Include="..\cs-ilp-core-codecs\cs-ilp-core-codecs.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Org.Interledger.Encoding.Asn.Codecs
{
public class AsnTimestampCodec : AsnPrintableStringBasedObjectCodecBase<DateTime>
{
private string DateTimeFormatter { get { return "yyyyMMddHHmmssfff"; } }

public AsnTimestampCodec() : base(new AsnSizeConstraint(17))
{
Regex regex = new Regex(this.GetRegex());
this.Validator = (string charString) => regex.IsMatch(charString);
}

public override DateTime Decode()
{
DateTime dateTime;
DateTime.TryParseExact(this.CharString, this.DateTimeFormatter, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out dateTime);
if (dateTime != null)
{
return dateTime;
}
else
{
throw new FormatException(string.Format(
"Interledger timestamps only support values in the format 'YYYYMMDDHHMMSSfff', value {0} is invalid.", this.CharString
));
}
}

public override void Encode(DateTime value)
{
this.CharString = value.ToString(this.DateTimeFormatter);
}

public override void Accept(IAsnObjectCodecVisitor visitor)
{
throw new NotImplementedException();
}

private string GetRegex()
{
return "[0-9]{17}";
}
}
}
6 changes: 6 additions & 0 deletions cs-ilp-core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cs-ilp-core-codecs", "cs-il
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cs-ilp-codec-framework-test", "cs-ilp-codec-framework-test\cs-ilp-codec-framework-test.csproj", "{55F3DD56-143F-4294-A9F0-3E4B36D0873F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cs-ilp-core-codecs-test", "cs-ilp-core-codecs-test\cs-ilp-core-codecs-test.csproj", "{2DC8332E-F866-492A-9713-E130E2110D31}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,6 +39,10 @@ Global
{55F3DD56-143F-4294-A9F0-3E4B36D0873F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55F3DD56-143F-4294-A9F0-3E4B36D0873F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55F3DD56-143F-4294-A9F0-3E4B36D0873F}.Release|Any CPU.Build.0 = Release|Any CPU
{2DC8332E-F866-492A-9713-E130E2110D31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DC8332E-F866-492A-9713-E130E2110D31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DC8332E-F866-492A-9713-E130E2110D31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DC8332E-F866-492A-9713-E130E2110D31}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down

0 comments on commit 95eb159

Please sign in to comment.