Skip to content

Commit 1164744

Browse files
committed
Fix issue #249 with AppSettings provider
AppSettings Type Provider should get changed settings values from <app name>.exe.config
1 parent 2949a43 commit 1164744

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

src/FSharpx.TypeProviders.AppSettings/AppSettingsProvider.fs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ let internal typedAppSettings (ownerType:TypeProviderForNamespaces) (cfg:TypePro
4040
let appSettings = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None).AppSettings.Settings
4141

4242
for key in appSettings.AllKeys do
43-
let field =
43+
let name = niceName key
44+
let getValue = <@@ ConfigurationManager.AppSettings.[key] @@>
45+
let prop =
4446
match (appSettings.Item key).Value with
45-
| Int fieldValue -> ProvidedLiteralField(niceName key, typeof<int>, fieldValue)
46-
| Bool fieldValue -> ProvidedLiteralField(niceName key, typeof<bool>, fieldValue)
47-
| Double fieldValue -> ProvidedLiteralField(niceName key, typeof<float>, fieldValue)
48-
| fieldValue -> ProvidedLiteralField(niceName key, typeof<string>, fieldValue)
47+
| Int _ -> ProvidedProperty(name, typeof<int>, GetterCode = (fun _ -> <@@ Int32.Parse(%%getValue) @@>))
48+
| Bool _ -> ProvidedProperty(name, typeof<bool>, GetterCode = (fun _ -> <@@ Boolean.Parse(%%getValue) @@>))
49+
| Double _ -> ProvidedProperty(name, typeof<float>, GetterCode = (fun _ -> <@@ Double.Parse(%%getValue, Globalization.NumberStyles.Any, Globalization.CultureInfo.InvariantCulture) @@>))
50+
| _ -> ProvidedProperty(name, typeof<string>, GetterCode = (fun _ -> getValue))
4951

50-
field.AddXmlDoc (sprintf "Returns the value from %s with key %s" configFileName key)
51-
field.AddDefinitionLocation(1,1,configFileName)
52+
prop.IsStatic <- true
53+
prop.AddXmlDoc (sprintf "Returns the value from %s with key %s" configFileName key)
54+
prop.AddDefinitionLocation(1,1,configFileName)
5255

53-
typeDef.AddMember field
56+
typeDef.AddMember prop
5457

5558
typeDef
5659
with

tests/FSharpx.TypeProviders.AppSettings.Tests/Test.App.config renamed to tests/FSharpx.TypeProviders.AppSettings.Tests/App.config

File renamed without changes.

tests/FSharpx.TypeProviders.AppSettings.Tests/AppSettings.Tests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open NUnit.Framework
44
open FSharpx
55
open FsUnit
66

7-
type Settings = AppSettings<"Test.App.config">
7+
type Settings = AppSettings<"App.config">
88

99
[<Test>]
1010
let ``Can return a string from the config file``() =

tests/FSharpx.TypeProviders.AppSettings.Tests/FSharpx.TypeProviders.AppSettings.Tests.fsproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -48,6 +48,7 @@
4848
<HintPath>..\..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
4949
</Reference>
5050
<Reference Include="System" />
51+
<Reference Include="System.Configuration" />
5152
<Reference Include="System.Core" />
5253
<Reference Include="System.Numerics" />
5354
</ItemGroup>
@@ -56,9 +57,7 @@
5657
<Link>FsUnit.fs</Link>
5758
</Compile>
5859
<Compile Include="AppSettings.Tests.fs" />
59-
<Content Include="Test.App.config">
60-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
61-
</Content>
60+
<None Include="App.config" />
6261
</ItemGroup>
6362
<PropertyGroup>
6463
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>

0 commit comments

Comments
 (0)