Skip to content

Commit

Permalink
Rename CoapResource to CoapResourceMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Aug 16, 2017
1 parent a82d49a commit 4165523
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 55 deletions.
1 change: 0 additions & 1 deletion CoAPNet.Tests/CoAPNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoAP.Net\CoAPNet.csproj" />
<ProjectReference Include="..\CoAPNet\CoAPNet.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions CoAPNet.Tests/CoreLinkFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public class TestCoreLinkFormat
public void ParseSimpleLinkFormat()
{
// Arrange
var expected = new List<CoapResource>
var expected = new List<CoapResourceMetadata>
{
new CoapResource("/sensor/temp")
new CoapResourceMetadata("/sensor/temp")
{
InterfaceDescription = { "sensor" }
},
new CoapResource("/sensor/light")
new CoapResourceMetadata("/sensor/light")
{
InterfaceDescription = { "sensor" }
}
Expand All @@ -42,9 +42,9 @@ public void ParseSimpleLinkFormat()
public void ParseExtensiveLinkFormat()
{
// Arrange
var expected = new List<CoapResource>
var expected = new List<CoapResourceMetadata>
{
new CoapResource("/sensor/temp")
new CoapResourceMetadata("/sensor/temp")
{
InterfaceDescription = {"sensor", "read"},
ResourceTypes = { "temperature-c", "temperature-f" },
Expand All @@ -60,11 +60,11 @@ public void ParseExtensiveLinkFormat()
Options.ContentFormatType.ApplicationJson
}
},
new CoapResource("http://stupid.schema.io/temperature.json")
new CoapResourceMetadata("http://stupid.schema.io/temperature.json")
{
Anchor = "/sensor/temp"
},
new CoapResource("/firmware/v2.1")
new CoapResourceMetadata("/firmware/v2.1")
{
ResourceTypes = { "firmware" },
SuggestedContentTypes = { Options.ContentFormatType.ApplicationOctetStream },
Expand Down
39 changes: 19 additions & 20 deletions CoAPNet/CoapResource.cs → CoAPNet/CoapResourceMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using CoAPNet.Options;

namespace CoAPNet
{
public class CoapResource
public class CoapResourceMetadata
{
public string URIReference { get; }
public Uri UriReference { get; }

public IList<string> Rel { get; } = new List<string>();

Expand Down Expand Up @@ -34,23 +35,21 @@ public class CoapResource

public Dictionary<string, string> Extentions = new Dictionary<string, string>();

public CoapResource(string uri)
{
URIReference = uri;
}
public CoapResourceMetadata(string uri)
: this(new Uri(uri, UriKind.RelativeOrAbsolute)) { }

private bool nullableSequenceEquals<T>(ICollection<T> a, ICollection<T> b) {
return (a == null && b == null) || (a != null && b != null && a.SequenceEqual(b));
public CoapResourceMetadata(Uri uri)
{
UriReference = uri;
}

public override bool Equals(object obj)
{
var other = obj as CoapResource;
var other = obj as CoapResourceMetadata;

if (other == null)
return base.Equals(obj);

if (URIReference != other.URIReference)
return false;
if (UriReference != other.UriReference)
return false;
if (Anchor != other.Anchor)
return false;
Expand All @@ -64,26 +63,26 @@ public override bool Equals(object obj)
return false;
if (Type != other.Type)
return false;
if (!nullableSequenceEquals(Rel, other.Rel))
if (!Rel.SequenceEqual(other.Rel))
return false;
if (!nullableSequenceEquals(Rev, other.Rev))
if (!Rev.SequenceEqual(other.Rev))
return false;
if (!nullableSequenceEquals(ResourceTypes, other.ResourceTypes))
if (!ResourceTypes.SequenceEqual(other.ResourceTypes))
return false;
if (!nullableSequenceEquals(InterfaceDescription, other.InterfaceDescription))
if (!InterfaceDescription.SequenceEqual(other.InterfaceDescription))
return false;
if (!nullableSequenceEquals(SuggestedContentTypes, other.SuggestedContentTypes))
if (!SuggestedContentTypes.SequenceEqual(other.SuggestedContentTypes))
return false;
if (MaxSize != other.MaxSize)
return false;
if (!Enumerable.SequenceEqual<KeyValuePair<string, string>>(Extentions, other.Extentions))
if (!Extentions.SequenceEqual(other.Extentions))
return false;
return true;
}

public override int GetHashCode()
{
return URIReference.GetHashCode();
return UriReference.GetHashCode();
}
}
}
54 changes: 27 additions & 27 deletions CoAPNet/CoreLinkFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public static class CoreLinkFormat
{
private enum FormatState { LinkValue, LinkParam }

public static List<CoapResource> Parse(string message)
public static List<CoapResourceMetadata> Parse(string message)
{
var state = FormatState.LinkValue;
var mPos = 0;

var result = new List<CoapResource>();
CoapResource currentResource = null;
var result = new List<CoapResourceMetadata>();
CoapResourceMetadata currentResourceMetadata = null;
try
{
var run = true;
Expand All @@ -30,13 +30,13 @@ public static List<CoapResource> Parse(string message)
if (message[mPos++] != '<')
throw new ArgumentException($"Expected link-value '<' at pos {mPos}");
mSeek = message.IndexOf('>', mPos);
if (currentResource != null)
result.Add(currentResource);
currentResource = new CoapResource(message.Substring(mPos, mSeek - mPos));
if (currentResourceMetadata != null)
result.Add(currentResourceMetadata);
currentResourceMetadata = new CoapResourceMetadata(message.Substring(mPos, mSeek - mPos));
mPos = mSeek + 1;
break;
case FormatState.LinkParam:
if (currentResource == null)
if (currentResourceMetadata == null)
throw new InvalidOperationException();

mSeek = message.IndexOf('=', mPos);
Expand All @@ -53,35 +53,35 @@ public static List<CoapResource> Parse(string message)
case "if":
value = value.Substring(1, value.Length - 2);
foreach (var s in value.Split(' '))
currentResource.InterfaceDescription.Add(s);
currentResourceMetadata.InterfaceDescription.Add(s);
break;
case "rt":
value = value.Substring(1, value.Length - 2);
foreach (var s in value.Split(' '))
currentResource.ResourceTypes.Add(s);
currentResourceMetadata.ResourceTypes.Add(s);
break;
case "rev":
if (currentResource.Rev.Count == 0)
if (currentResourceMetadata.Rev.Count == 0)
{
value = value.Substring(1, value.Length - 2);
foreach (var s in value.Split(' '))
currentResource.Rev.Add(s);
currentResourceMetadata.Rev.Add(s);
}
break;
case "rel":
if (currentResource.Rel.Count == 0)
if (currentResourceMetadata.Rel.Count == 0)
{
value = value.Substring(1, value.Length - 2);
foreach (var s in value.Split(' '))
currentResource.Rel.Add(s);
currentResourceMetadata.Rel.Add(s);
}
break;
case "anchor":
currentResource.Anchor = value.Substring(1, value.Length - 2);
currentResourceMetadata.Anchor = value.Substring(1, value.Length - 2);
break;
case "hreflang":
// Much easier to let libraries offload language formatting stuff
currentResource.HrefLang = new System.Globalization.CultureInfo(value).Name.ToLower();
currentResourceMetadata.HrefLang = new System.Globalization.CultureInfo(value).Name.ToLower();
break;
case "media":
if(value[0] == '"')
Expand All @@ -90,14 +90,14 @@ public static List<CoapResource> Parse(string message)
throw new ArgumentException($"Expected MediaDesc DQUOTE '\"' at pos {mSeek}");
value = value.Substring(1, value.Length - 2);
}
currentResource.Media = value;
currentResourceMetadata.Media = value;
break;
case "title":
if (value[0] != '"' )
throw new ArgumentException($"Expected QuotedString DQUOTE '\"' at pos {mPos}");
if (value[value.Length - 1] != '"')
throw new ArgumentException($"Expected QuotedString DQUOTE '\"' at pos {mSeek}");
currentResource.Title = value.Substring(1, value.Length - 2);
currentResourceMetadata.Title = value.Substring(1, value.Length - 2);
break;
case "title*":
// Todo: No idea what to do here...?
Expand All @@ -108,7 +108,7 @@ public static List<CoapResource> Parse(string message)
//System.Diagnostics.Debug.WriteLine("title* = {3}\n\tCharset: {0}\n\tLanguage: {1}\n\tValue: {2}",
// charset, lang, value, Uri.UnescapeDataString(value));

currentResource.TitleExt = Uri.UnescapeDataString(value);
currentResourceMetadata.TitleExt = Uri.UnescapeDataString(value);
break;
case "type":
if (value[0] == '"')
Expand All @@ -117,18 +117,18 @@ public static List<CoapResource> Parse(string message)
throw new ArgumentException($"Expected Type DQUOTE '\"' at pos {mSeek}");
value = value.Substring(1, value.Length - 2);
}
currentResource.Type = value;
currentResourceMetadata.Type = value;
break;
case "sz":
if (value[0] == '0' && value.Length != 1)
throw new ArgumentException($"cardinal may not start with '0' unless at pos {mSeek}");
if(!ulong.TryParse(value, out var maxSize))
throw new ArgumentException($"Could not parse cardinal at pos {mSeek}");
currentResource.MaxSize = maxSize;
currentResourceMetadata.MaxSize = maxSize;
break;
case "ct":
int ct = 0;
currentResource.SuggestedContentTypes.Clear();
currentResourceMetadata.SuggestedContentTypes.Clear();

if (value[0] == '"')
{
Expand All @@ -140,7 +140,7 @@ public static List<CoapResource> Parse(string message)
.Split(' ')
.Select(s => (Options.ContentFormatType) int.Parse(s)))
{
currentResource.SuggestedContentTypes.Add(contentFormatType);
currentResourceMetadata.SuggestedContentTypes.Add(contentFormatType);
}
}
else
Expand All @@ -150,23 +150,23 @@ public static List<CoapResource> Parse(string message)
if (!int.TryParse(value, out ct))
throw new ArgumentException($"Could not parse cardinal at pos {mSeek}");

currentResource.SuggestedContentTypes.Add(ct);
currentResourceMetadata.SuggestedContentTypes.Add(ct);
}
break;
default:
if (value.Length == 1)
{
if (!char.IsLetterOrDigit(value[0]) && !new [] { '!', '#', '$', '%', '&', '\'', '(', ')', '*', '+', '-', '.', '/', ':', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~' }.Contains(value[0]))
throw new ArgumentException($"PToken contains invalid character '{value[0]}' at pos {mSeek}");
currentResource.Extentions.Add(param, value);
currentResourceMetadata.Extentions.Add(param, value);
}
else
{
if (value[0] != '"')
throw new ArgumentException($"Expected QuotedString DQUOTE '\"' at pos {mPos}");
if (value[value.Length - 1] != '"')
throw new ArgumentException($"Expected QuotedString DQUOTE '\"' at pos {mSeek}");
currentResource.Extentions.Add(param, value.Substring(1, value.Length - 2));
currentResourceMetadata.Extentions.Add(param, value.Substring(1, value.Length - 2));
}
break;
}
Expand All @@ -192,8 +192,8 @@ public static List<CoapResource> Parse(string message)
// Moving on...
}

if (currentResource != null)
result.Add(currentResource);
if (currentResourceMetadata != null)
result.Add(currentResourceMetadata);

return result;
}
Expand Down

0 comments on commit 4165523

Please sign in to comment.