Skip to content

Commit

Permalink
Add a decoder for System.Uri. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Choc13 authored Jul 7, 2021
1 parent 4daf14f commit 89f7f0a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Symbolica.Extensions.Configuration.FSharp/Decode.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Symbolica.Extensions.Configuration.FSharp

open System

/// <summary>
/// A function that takes a value and returns a <see cref="Binder"/> which evaluates to <c>Success</c> if the
/// value can be decoded otherwise <c>Failure</c>.
Expand Down Expand Up @@ -35,31 +37,34 @@ module Decode =
Failure [ $"Could not decode '{value}' at path '{section |> path}' as type '{typeof<'parsed>}'." ])

/// <summary>A <see cref="Decoder" /> for <see cref="System.Boolean" /> values.</summary>
let bool = ofParser System.Boolean.TryParse
let bool = ofParser Boolean.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Char" /> values.</summary>
let char = ofParser System.Char.TryParse
let char = ofParser Char.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.DateTime" /> values.</summary>
let dateTime = ofParser System.DateTime.TryParse
let dateTime = ofParser DateTime.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Double" /> values.</summary>
let float = ofParser System.Double.TryParse
let float = ofParser Double.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Int16" /> values.</summary>
let int16 = ofParser System.Int16.TryParse
let int16 = ofParser Int16.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Int32" /> values.</summary>
let int = ofParser System.Int32.TryParse
let int = ofParser Int32.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Int64" /> values.</summary>
let int64 = ofParser System.Int64.TryParse
let int64 = ofParser Int64.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.UInt16" /> values.</summary>
let uint16 = ofParser System.UInt16.TryParse
let uint16 = ofParser UInt16.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.UInt32" /> values.</summary>
let uint = ofParser System.UInt32.TryParse
let uint = ofParser UInt32.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.UInt64" /> values.</summary>
let uint64 = ofParser System.UInt64.TryParse
let uint64 = ofParser UInt64.TryParse

/// <summary>A <see cref="Decoder" /> for <see cref="System.Uri" /> values of the specified <see cref="System.UriKind" />.</summary>
let uri kind = ofParser (fun s -> Uri.TryCreate(s, kind))
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Symbolica.Extensions.Configuration.FSharp
open FsCheck
open FsCheck.Xunit
open Swensen.Unquote
open global.Xunit

module Decode =
module Bool =
Expand Down Expand Up @@ -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 =
[<Property>]
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) @>

[<Fact>]
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) @>

[<Property>]
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'." ]
) @>

0 comments on commit 89f7f0a

Please sign in to comment.