Skip to content

Commit

Permalink
Use pattern matching to extract static parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Jan 1, 2022
1 parent 8ff03b6 commit fb6e546
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
20 changes: 11 additions & 9 deletions src/FSharp.Data.LiteralProviders.DesignTime/Env.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ let addEnvOrDefault asm ns (envVars: IDictionary<string, string>) (fileVars: IDi
ProvidedStaticParameter("LoadEnvFile", typeof<bool>, true)
],
fun tyName args ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
let name = args.[0] :?> string
let variables = if args.[2] :?> bool then mergeDicts [envVars; fileVars] else envVars
let isSet, envValue = variables.TryGetValue(name)
let value = if isSet then envValue else args.[1] :?> string
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)
match args with
| [| :? string as name; :? string as defaultValue; :? bool as loadEnvFile |] ->
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
| _ -> failwithf "Invalid args: %A" args)
ty

let create asm ns baseDir =
Expand Down
31 changes: 17 additions & 14 deletions src/FSharp.Data.LiteralProviders.DesignTime/TextFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ let addFileOrDefault asm ns baseDir (ty: ProvidedTypeDefinition) =
ProvidedStaticParameter("DefaultValue", typeof<string>, "")
ProvidedStaticParameter("Encoding", typeof<string>, "") ],
fun tyName args ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
let path = Path.Combine(baseDir, args[0] :?> string)
let name = Path.GetFileName path
let exists = File.Exists(path)
ProvidedField.Literal("Exists", typeof<bool>, exists) |> ty.AddMember
if exists then
let encodings = getEncodings (args[2] :?> string)
addFileMembers ty path name encodings
else
ty.AddMembers(
[ ProvidedField.Literal("Path", typeof<string>, path)
ProvidedField.Literal("Name", typeof<string>, name)
ProvidedField.Literal("Text", typeof<string>, args[1]) ])
ty)
match args with
| [| :? string as path; :? string as defaultValue; :? string as encoding |] ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
let path = Path.Combine(baseDir, path)
let name = Path.GetFileName path
let exists = File.Exists(path)
ProvidedField.Literal("Exists", typeof<bool>, exists) |> ty.AddMember
if exists then
let encodings = getEncodings encoding
addFileMembers ty path name encodings
else
ty.AddMembers(
[ ProvidedField.Literal("Path", typeof<string>, path)
ProvidedField.Literal("Name", typeof<string>, name)
ProvidedField.Literal("Text", typeof<string>, defaultValue) ])
ty
| _ -> failwithf "Invalid args: %A" args)
ty

let create asm ns baseDir =
Expand Down

0 comments on commit fb6e546

Please sign in to comment.