Skip to content

Commit

Permalink
[#14] Add EnsureExists parameter to TextFile and Env
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed May 21, 2022
1 parent fb6e546 commit d8d0b08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/FSharp.Data.LiteralProviders.DesignTime/Env.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ let addEnvOrDefault asm ns (envVars: IDictionary<string, string>) (fileVars: IDi
ProvidedStaticParameter("Name", typeof<string>)
ProvidedStaticParameter("DefaultValue", typeof<string>, "")
ProvidedStaticParameter("LoadEnvFile", typeof<bool>, true)
ProvidedStaticParameter("EnsureExists", typeof<bool>, false)
],
fun tyName args ->
match args with
| [| :? string as name; :? string as defaultValue; :? bool as loadEnvFile |] ->
| [| :? string as name; :? string as defaultValue; :? bool as loadEnvFile; :? bool as ensureExists |] ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
let variables = if loadEnvFile then mergeDicts [envVars; fileVars] else envVars
let isSet, envValue = variables.TryGetValue(name)
let value = if isSet then envValue else defaultValue
ProvidedField.Literal("Name", typeof<string>, name) |> ty.AddMember
ProvidedField.Literal("Value", typeof<string>, value) |> ty.AddMember
ProvidedField.Literal("IsSet", typeof<bool>, isSet) |> ty.AddMember
ty
if ensureExists && not isSet then
failwithf "Environment variable does not exist: %s" name
else
let value = if isSet then envValue else defaultValue
ProvidedField.Literal("Name", typeof<string>, name) |> ty.AddMember
ProvidedField.Literal("Value", typeof<string>, value) |> ty.AddMember
ProvidedField.Literal("IsSet", typeof<bool>, isSet) |> ty.AddMember
ty
| _ -> failwithf "Invalid args: %A" args)
ty

Expand Down
7 changes: 5 additions & 2 deletions src/FSharp.Data.LiteralProviders.DesignTime/TextFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ let addFileOrDefault asm ns baseDir (ty: ProvidedTypeDefinition) =
ty.DefineStaticParameters(
[ ProvidedStaticParameter("Path", typeof<string>)
ProvidedStaticParameter("DefaultValue", typeof<string>, "")
ProvidedStaticParameter("Encoding", typeof<string>, "") ],
ProvidedStaticParameter("Encoding", typeof<string>, "")
ProvidedStaticParameter("EnsureExists", typeof<bool>, false) ],
fun tyName args ->
match args with
| [| :? string as path; :? string as defaultValue; :? string as encoding |] ->
| [| :? string as path; :? string as defaultValue; :? string as encoding; :? bool as ensureExists |] ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
let path = Path.Combine(baseDir, path)
let name = Path.GetFileName path
Expand All @@ -93,6 +94,8 @@ let addFileOrDefault asm ns baseDir (ty: ProvidedTypeDefinition) =
if exists then
let encodings = getEncodings encoding
addFileMembers ty path name encodings
elif ensureExists then
failwithf "File does not exist: %s" path
else
ty.AddMembers(
[ ProvidedField.Literal("Path", typeof<string>, path)
Expand Down
3 changes: 3 additions & 0 deletions tests/FSharp.Data.LiteralProviders.Tests/Env.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ let ``Garbage variable with default is not set`` () =
[<Test>]
let ``Garbage variable with default is default`` () =
Assert.AreEqual("some default value", ``Garbage with default``.Value)

// Uncomment below to test EnsureExists
// type EnsureExists = Env<"doesntexist", EnsureExists = true>
3 changes: 3 additions & 0 deletions tests/FSharp.Data.LiteralProviders.Tests/TextFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ let ``Regression #12 - Strip BOM`` () =
Assert.AreEqual("Test content", WithBom.Text)
Assert.IsFalse(WithoutBom.HasBom)
Assert.AreEqual("Test content", WithoutBom.Text)

// Uncomment below to test EnsureExists
// type EnsureExists = TextFile<"doesntexist.txt", EnsureExists = true>

0 comments on commit d8d0b08

Please sign in to comment.