Skip to content

Commit

Permalink
feat: formatters for types char and guid (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Jan 20, 2024
1 parent d3bb04c commit 3ed207a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
5 changes: 4 additions & 1 deletion DubUrl.Adomd/Querying/DaxValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ internal class DaxValueFormatter : BaseValueFormatter
public DaxValueFormatter()
{
With(new BooleanFormatter());
With(new DoubleQuotedValueFormatter());
With(new GuidFormatter());
With<string>(new DoubleQuotedValueFormatter());
With<char>(new DoubleQuotedValueFormatter());
With<char>(new DoubleQuotedValueFormatter());
With(new FunctionFormatter<DateOnly>("VALUE", new DaxDateFormatter()));
With(new FunctionFormatter<TimeOnly>("VALUE", new DaxTimeFormatter()));
With(new FunctionFormatter<DateTime>("VALUE", new DaxTimestampFormatter()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

namespace DubUrl.Querying.Dialects.Formatters;

public class DoubleQuotedValueFormatter : IValueFormatter<string>
public class DoubleQuotedValueFormatter : IValueFormatter<string>, IValueFormatter<char>
{
public string Format(string value)
=> $"\"{value}\"";
public string Format(char value)
=> $"\"{value}\"";
public string Format(object obj)
=> obj is string value ? Format(value) : throw new Exception();
=> obj switch
{
char c => Format(c),
string str => Format(str),
_ => throw new Exception()
};
}
22 changes: 22 additions & 0 deletions DubUrl.Core/Querying/Dialects/Formatters/GuidFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DubUrl.Querying.Dialects.Formatters;
public class GuidFormatter : IValueFormatter<Guid>
{
public string Format(Guid value)
=> $"'{value}'";

public string Format(object obj)
=> obj is Guid value
? Format(value)
: obj is string str
? Format(str)
: throw new Exception();

public virtual string Format(string value)
=> Guid.TryParse(value, out var guid) ? Format(guid) : throw new Exception();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

namespace DubUrl.Querying.Dialects.Formatters;

public class SimpleQuotedValueFormatter : IValueFormatter<string>
public class SimpleQuotedValueFormatter : IValueFormatter<string>, IValueFormatter<char>
{
public string Format(string value)
=> $"'{value}'";
public string Format(char value)
=> $"'{value}'";
public string Format(object obj)
=> obj is string value ? Format(value) : throw new Exception();
=> obj switch
{
char c => Format(c),
string str => Format(str),
_ => throw new Exception()
};
}
4 changes: 3 additions & 1 deletion DubUrl.Core/Querying/Dialects/Renderers/ValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class ValueFormatter : BaseValueFormatter
public ValueFormatter()
{
With(new BooleanFormatter());
With(new SimpleQuotedValueFormatter());
With(new GuidFormatter());
With<string>(new SimpleQuotedValueFormatter());
With<char>(new SimpleQuotedValueFormatter());
With(new PrefixFormatter<DateOnly>("DATE", new DateFormatter()));
With(new PrefixFormatter<TimeOnly>("TIME", new TimeFormatter()));
With(new PrefixFormatter<DateTime>("TIMESTAMP", new TimestampFormatter()));
Expand Down
13 changes: 12 additions & 1 deletion DubUrl.Testing/Querying/Dialects/Formatters/FormattersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,15 @@ public void PrefixFormatter_Format_Match(DateTime value, string expected)
public void SimpleQuotedValueFormatter_Format_Match(string value, string expected)
=> Assert.That(new SimpleQuotedValueFormatter().Format(value), Is.EqualTo(expected));

}
[Test]
[TestCase("36f1d158-1fa1-11ed-ba36-c8cb9e32df8e", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")]
[TestCase("36F1D158-1FA1-11ED-BA36-C8CB9e32DF8E", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")]
public void GuidFormatter_Guid_Match(Guid value, string expected)
=> Assert.That(new GuidFormatter().Format(value), Is.EqualTo($"'{value}'"));

[Test]
[TestCase("36f1d158-1fa1-11ed-ba36-c8cb9e32df8e", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")]
[TestCase("36F1D158-1FA1-11ED-BA36-C8CB9e32DF8E", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")]
public void GuidFormatter_String_Match(string value, string expected)
=> Assert.That(new GuidFormatter().Format(value), Is.EqualTo(expected));
}

0 comments on commit 3ed207a

Please sign in to comment.