Skip to content

Commit

Permalink
Edm.Date-in-Url for #347
Browse files Browse the repository at this point in the history
See issue #347
  • Loading branch information
Luke Dean authored and LianwMS committed Nov 12, 2015
1 parent c4dc2b9 commit 6222a1b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Microsoft.OData.Core/UriParser/Parsers/LiteralParser.cs
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.OData.Core.UriParser.Parsers
using System.Globalization;
using System.Text;
using System.Xml;
using Edm.Library;
using Microsoft.Spatial;
using XmlConstants = ExpressionConstants;

Expand All @@ -37,6 +38,7 @@ internal abstract class LiteralParser
{ typeof(byte[]), new BinaryPrimitiveParser() },
{ typeof(String), new StringPrimitiveParser() },
{ typeof(Decimal), new DecimalPrimitiveParser() },
{ typeof(Date), new DatePrimitiveParser() },

// Types without single-quotes or type markers
{ typeof(Boolean), DelegatingPrimitiveParser<bool>.WithoutMarkup(XmlConvert.ToBoolean) },
Expand Down Expand Up @@ -626,5 +628,52 @@ internal override bool TryRemoveFormatting(ref string text)
return UriPrimitiveTypeParser.TryRemoveQuotes(ref text);
}
}

/// <summary>
/// Parser specific to the Edm.Date type.
/// </summary>
private sealed class DatePrimitiveParser : PrimitiveParser
{
/// <summary>
/// Initializes a new instance of the <see cref="DatePrimitiveParser"/> class.
/// </summary>
public DatePrimitiveParser() : base(typeof(Date))
{
}

/// <summary>
/// Tries to convert the given text into this parser's expected type. Conversion only, formatting should already have been removed.
/// </summary>
/// <param name="text">The text to convert.</param>
/// <param name="targetValue">The target value.</param>
/// <returns>
/// Whether or not conversion was successful.
/// </returns>
internal override bool TryConvert(string text, out object targetValue)
{
try
{
var date = XmlConvert.ToDateTimeOffset(text);
targetValue = new Date(date.Year, date.Month, date.Day);
}
catch (FormatException)
{
targetValue = null;
return false;
}
catch (ArgumentOutOfRangeException)
{
targetValue = null;
return false;
}
catch (ArgumentNullException)
{
targetValue = null;
return false;
}

return true;
}
}
}
}
Expand Up @@ -4,6 +4,8 @@
// </copyright>
//---------------------------------------------------------------------

using Microsoft.OData.Edm.Library;

namespace Microsoft.Test.OData.Query.TDD.Tests.Syntactic
{
using System;
Expand Down Expand Up @@ -47,5 +49,14 @@ public void TryParseLiteralWithDurationLiteralForKeyAsSegmentUrlConventionsShoul
parser.TryParseLiteral(typeof(TimeSpan), "P1D", out output).Should().BeTrue();
output.ShouldBeEquivalentTo(new TimeSpan(1, 0, 0, 0));
}

[TestMethod]
public void TryParseLiteralWithDateForKeyAsSegmentUrlConventionsShouldReturnValidDate()
{
var parser = LiteralParser.ForKeys(true /*keyAsSegment*/);
object output;
parser.TryParseLiteral(typeof(Date), "2015-09-28", out output).Should().BeTrue();
output.ShouldBeEquivalentTo(new Date(2015, 09, 28));
}
}
}

0 comments on commit 6222a1b

Please sign in to comment.