FSharp.Appsettings 1.0.8

dotnet add package FSharp.Appsettings --version 1.0.8
NuGet\Install-Package FSharp.Appsettings -Version 1.0.8
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FSharp.Appsettings" Version="1.0.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FSharp.Appsettings --version 1.0.8
#r "nuget: FSharp.Appsettings, 1.0.8"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FSharp.Appsettings as a Cake Addin
#addin nuget:?package=FSharp.Appsettings&version=1.0.8

// Install FSharp.Appsettings as a Cake Tool
#tool nuget:?package=FSharp.Appsettings&version=1.0.8

FSharp.Appsettings - F#

Logo

Minimalistic environment-sensitive appsettings.json loader.

Usage

Define FSHARP_ENVIRONMENT as an environment variable. For development this will normally be done by creating a launchSettings.json file:

{
	"$schema": "https://json.schemastore.org/launchsettings.json",
	"profiles": {
		"Development": {
			"commandName": "Project",
			"environmentVariables": {
				"FSHARP_ENVIRONMENT": "Development"
			}
		}
	}
}

You can then run this code to load one or two appsettings.json files with this code:

open FSharp.Appsettings

let appsettings = Appsettings.Load ()

// Deserialize to type
open System.Text.Json

let required = appsettings.Deserialize<RequiredConfig>()

appsettings.json will always be loaded, while appsettings.{FSHARP_ENVIRONMENT}.json will be loaded if FSHARP_ENVIRONMENT is defined as an environment variable.
Properties from appsettings.json will be overwritten by the environment specific appsettings.
Arrays will be added together if a value does not already exist in the array.

Example

// appsettings.json
{
	"Env": "Root",
	"CORS": {
		"AllowedOrigins": [
			"https://localhost:3000", "https://fsharp.org"
		]
	},
	"Logging": {
		"LogLevel": {
			"Default": "Debug",
			"System": "Information",
			"Microsoft": "Information",
			"Test": "Critical"
		}
	},
	"OnlyRoot": "Root"
}
// appsettings.Development.json (FSHARP_ENVIRONMENT=Development)
{
	"Env": "Development",
	"CORS": {
		"AllowedOrigins": [
			"https://localhost:3000"
		]
	},
	"Logging": {
		"LogLevel": {
			"Default": "Debug",
			"System": "Information",
			"Microsoft": "Information"
		}
	},
	"OnlyDev": "Dev"
}
// Resulting object after Appsettings.load ()
{
	"Env": "Development",
	"CORS": {
		"AllowedOrigins": [
			"https://localhost:3000", "https://fsharp.org"
		]
	},
	"Logging": {
		"LogLevel": {
			"Default": "Debug",
			"System": "Information",
			"Microsoft": "Information",
			"Test": "Critical"
		}
	},
	"OnlyDev": "Dev",
	"OnlyRoot": "Root"
}

Note: This NuGet does not support defining secrets by defining <UserSecretsId> in the .csproj file.
Instead it detects appsettings{.FSHARP_ENVIRONMENT?}.local.json that you can (should) add to .gitignore.

Order of Priority

  1. appsettings.{FSHARP_ENVIRONMENT}.local.json
  2. appsettings.local.json
  3. appsettings.{FSHARP_ENVIRONMENT}.json
  4. appsettings.json

Building

The appsettings.json files must be copied over to output directory. This is achieved by adding the following to your .fsproj.


<ItemGroup>
	<Content Include="appsettings*.json">
		<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
	</Content>
</ItemGroup>

Merge rules

Describes what happens when a field from appsettings A encounters a field with the same name from appsettings B

  1. Value A - Value B: Value B overwrites value A
  2. Object A - Object B: Recursion
  3. Array A - Array B: Items from array A are added to array B if not already present
  4. Value A - Object B: Object B replaces value A
  5. Value A - Array B: Array B replaces value A
  6. Object A - Value B: Value B replaces Object A
  7. Object A - Array B: Array B replaces Object A
  8. Array A - Value B: Value B replaces Array A
  9. Array A - Object B: Object B replaces Array A

Committing

Important to run this before committing (assuming you have GPG key set up)

git config commit.gpgsign true
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 147 6/3/2023
1.0.7 116 5/22/2023
1.0.6 122 5/15/2023
1.0.5 125 5/4/2023
1.0.4 302 12/3/2022
1.0.3 290 12/3/2022
1.0.2 300 11/26/2022
1.0.1 382 9/21/2022
1.0.0 495 9/13/2022