From 89f7f0a8ee1206daafc00f2bff3c34e5a905cf51 Mon Sep 17 00:00:00 2001 From: Matt Thornton Date: Wed, 7 Jul 2021 15:56:20 +0100 Subject: [PATCH] Add a decoder for System.Uri. (#8) --- .../Decode.fs | 25 +++++++++------- .../DecodeTests.fs | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Symbolica.Extensions.Configuration.FSharp/Decode.fs b/src/Symbolica.Extensions.Configuration.FSharp/Decode.fs index 151cbb1..fad9896 100644 --- a/src/Symbolica.Extensions.Configuration.FSharp/Decode.fs +++ b/src/Symbolica.Extensions.Configuration.FSharp/Decode.fs @@ -1,5 +1,7 @@ namespace Symbolica.Extensions.Configuration.FSharp +open System + /// /// A function that takes a value and returns a which evaluates to Success if the /// value can be decoded otherwise Failure. @@ -35,31 +37,34 @@ module Decode = Failure [ $"Could not decode '{value}' at path '{section |> path}' as type '{typeof<'parsed>}'." ]) /// A for values. - let bool = ofParser System.Boolean.TryParse + let bool = ofParser Boolean.TryParse /// A for values. - let char = ofParser System.Char.TryParse + let char = ofParser Char.TryParse /// A for values. - let dateTime = ofParser System.DateTime.TryParse + let dateTime = ofParser DateTime.TryParse /// A for values. - let float = ofParser System.Double.TryParse + let float = ofParser Double.TryParse /// A for values. - let int16 = ofParser System.Int16.TryParse + let int16 = ofParser Int16.TryParse /// A for values. - let int = ofParser System.Int32.TryParse + let int = ofParser Int32.TryParse /// A for values. - let int64 = ofParser System.Int64.TryParse + let int64 = ofParser Int64.TryParse /// A for values. - let uint16 = ofParser System.UInt16.TryParse + let uint16 = ofParser UInt16.TryParse /// A for values. - let uint = ofParser System.UInt32.TryParse + let uint = ofParser UInt32.TryParse /// A for values. - let uint64 = ofParser System.UInt64.TryParse + let uint64 = ofParser UInt64.TryParse + + /// A for values of the specified . + let uri kind = ofParser (fun s -> Uri.TryCreate(s, kind)) diff --git a/tests/Symbolica.Extensions.Configuration.FSharp.Tests/DecodeTests.fs b/tests/Symbolica.Extensions.Configuration.FSharp.Tests/DecodeTests.fs index fb2482d..018dff2 100644 --- a/tests/Symbolica.Extensions.Configuration.FSharp.Tests/DecodeTests.fs +++ b/tests/Symbolica.Extensions.Configuration.FSharp.Tests/DecodeTests.fs @@ -3,6 +3,7 @@ namespace Symbolica.Extensions.Configuration.FSharp open FsCheck open FsCheck.Xunit open Swensen.Unquote +open global.Xunit module Decode = module Bool = @@ -192,3 +193,31 @@ module Decode = |> Binder.eval (SectionStub.Empty path) = Failure( [ $"Could not decode 'string' at path '{path}' as type 'System.UInt64'." ] ) @> + + module Uri = + [] + let ``should be Success value if can be converted to absolute uri`` (HostName host) = + let value = System.Uri($"https://{host}") + test + <@ value + |> string + |> Decode.uri System.UriKind.Absolute + |> Binder.eval (SectionStub.Empty "") = Success(value) @> + + [] + let ``should be Success value if can be converted to relative uri`` = + let value = System.Uri("/relative/uri", System.UriKind.Relative) + test + <@ value + |> string + |> Decode.uri System.UriKind.Relative + |> Binder.eval (SectionStub.Empty "") = Success(value) @> + + [] + let ``should be Failure if string can not be converted to uri`` path = + test + <@ "string" + |> Decode.uri System.UriKind.Absolute + |> Binder.eval (SectionStub.Empty path) = Failure( + [ $"Could not decode 'string' at path '{path}' as type 'System.Uri'." ] + ) @>