### TOML Example with Server Data Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md This TOML snippet defines server configurations, including IP addresses and ports, which will be used in the transformation example. ```toml [servers.alpha] ip = "192.168.1.1" ports = [8080, 8081] [servers.beta] ip = "192.168.1.2" ports = [8082, 8083] ``` -------------------------------- ### Example TOML File Structure Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md This is a sample TOML file demonstrating various data types and structures, including strings, numbers, dates, arrays, and nested tables. ```toml # This is a TOML document. title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 # First class dates [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] # Indentation (tabs and/or spaces) is allowed but not required [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10" [clients] data = [ ["gamma", "delta"], [1, 2] ] # Line breaks are OK when inside arrays hosts = [ "alpha", "omega" ] ``` -------------------------------- ### Distillery Release Configuration with TOML Provider Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md This example shows how to set up TOML as a configuration provider within a Distillery release configuration file (`rel/config.exs`). It uses environment variables for the TOML file path. ```elixir release :myapp do # ...snip... set config_providers: [ {Toml.Provider, [path: "${XDG_CONFIG_DIR}/myapp.toml", transforms: [...]]} ] end ``` -------------------------------- ### Elixir Release Configuration with TOML Provider Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md Use this example to configure TOML as a provider in Elixir releases (1.9+). It specifies the path to the TOML file using system environment variables. ```elixir config_providers: [ {Toml.Provider, [ path: {:system, "XDG_CONFIG_DIR", "myapp.toml"}, transforms: [...] ]} ] ``` -------------------------------- ### Decoding TOML Data in Elixir Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md Demonstrates decoding TOML data from strings, streams, and files using the `Toml.decode` and related functions. Includes examples for default decoding and decoding with atoms as keys, as well as handling invalid TOML input. ```elixir iex> input = """ [database] server = "192.168.1.1" """ ...> {:ok, %{"database" => %{"server" => "192.168.1.1"}}} = Toml.decode(input) ...> {:ok, %{database: %{server: "192.168.1.1"}}} = Toml.decode(input, keys: :atoms) ...> stream = File.stream!("example.toml") ...> {:ok, %{"database" => %{"server" => "192.168.1.1"}}} = Toml.decode_stream(stream) ...> {:ok, %{"database" => %{"server" => "192.168.1.1"}}} = Toml.decode_file("example.toml") ...> invalid = """ [invalid] a = 1 b = 2 """ ...> {:error, {:invalid_toml, reason}} = Toml.decode(invalid); IO.puts(reason) expected '\n', but got 'b' in nofile on line 2: a = 1 b = 2 ^ :ok ``` -------------------------------- ### TOML Configuration File Structure Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md This TOML structure demonstrates how to organize configuration for applications like 'myapp' and 'logger'. Keys at the root must be tables corresponding to application names. ```toml # This is an example of something that would be ignored title = "My config file" # We're expecting something like this: [myapp] key = "value" # To use a bit of Phoenix config, you translate to TOML like so: [myapp."MyApp.Endpoint"] cache_static_manifest = "priv/static/cache_manifest.json" [myapp."MyApp.Endpoint".http] port = "4000" [myapp."MyApp.Endpoint".force_ssl] hsts = true # Or logger.. [logger] level = "info" [logger.console] format = "[$level] $message \n" ``` -------------------------------- ### Applying Multiple TOML Transformations in Elixir Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md Shows how to apply a list of custom transformation modules to a TOML document during decoding. The transformations are applied in order, depth-first, and bottom-up. ```elixir iex> transforms = [IPStringToCharlist, CharlistToIP, ServerMapToList] ...> {:ok, result} = Toml.decode("example.toml", keys: :atoms, transforms: transforms) %{servers: [%Server{name: :alpha, ip: {192,168,1,1}}, ports: [8080, 8081] | _]} ``` -------------------------------- ### Add TOML Dependency to Elixir Project Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md Add the `:toml` dependency to your Elixir project's `deps` function. You can check the latest version on Hex.pm. ```elixir def deps do [ {:toml, "~> 0.7"} ] end ``` -------------------------------- ### Elixir Modules for TOML Transformations Source: https://github.com/bitwalker/toml-elixir/blob/main/README.md Defines Elixir modules that implement the `Toml.Transform` behavior to customize how specific TOML values are converted. These include converting IP strings to charlists, charlists to IP addresses, and server maps to a list of structs. ```elixir defmodule Server do defstruct [:name, :ip, :ports] end defmodule IPStringToCharlist do use Toml.Transform def transform(:ip, v) when is_binary(v) do String.to_charlist(v) end def transform(_k, v), do: v end defmodule CharlistToIP do use Toml.Transform def transform(:ip, v) when is_list(v) do case :inet.parse_ipv4_address(v) do {:ok, address} -> address {:error, reason} -> {:error, {:invalid_ip_address, reason}} end end def transform(:ip, v), do: {:error, {:invalid_ip_address, v}} def transform(_k, v), do: v end defmodule ServerMapToList do use Toml.Transform def transform(:servers, v) when is_map(v) do for {name, server} <- v, do: struct(Server, Map.put(server, :name, name)) end def transform(_k, v), do: v end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.