magic.lambda.config

This project provides configuration settings slots for Magic. The project provides the following slots, allowing you to retrieve configuration settings from your “appsettings.json” configuration file, in addition to saving and loading your configuration file.

  • [config.get] - Returns a configuration value from your configuration file with the specified key
  • [config.section] - Returns the specified configuration section from your configuration file with the specified key
  • [config.load] - Loads your “appsettings.json” file and returns as a string
  • [config.save] - Saves your “appsettings.json” file, sanity checking it’s valid JSON before persisting your file

How to use [config.get]

This slot allows you to retrieve configuration settings. To retrieve settings you can supply a “path” such as “foo:bar” to for instance an invocation to [config.get]. This will traverse into your “foo” config setting, find its “bar” key, and return the value of your “bar” key. Below is an example of usage.

config.get:"foo:bar"

Assuming your configuration file looks like the following.

{
   "foo": {
      "bar": 42
   }
}

… afterwards the value of your [config.get] node will be the following.

config.get:42

Notice - Due to implementation details of .Net and its IConfiguration specifically, values returned will always be strings, and you’ll have to manually convert these to other types, using for instance the [convert] slot from magic.lambda. This might change in a future release though. You can also provide a default value that will be returned if no configuration value is found, such as the following illustrates.

.foo:foo
config.get:"magic:foo:non-existing-key"
   get-value:x:@.foo

Since the above “non-existing-key” doesn’t exist in your configuration file, the above Hyperlambda will return the value “foo” being the result of the invocation to the [get-value] slot. This allows you to create default values your code resorts to if the specified configuration setting doesn’t exist.