summaryrefslogtreecommitdiff
path: root/src/Conftrack/Source.hs
blob: ab13172282b112a75b9ad2cb889c027527aa0b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

module Conftrack.Source (ConfigSource(..), SomeSource(..)) where

import Conftrack.Value (Key, Value(..), ConfigError(..))

import Control.Monad.State (StateT (..))
import Data.Text (Text)


-- | An abstraction over "config sources". This might mean file formats,
-- environment variables, or any other kind of format that can be seen as a
-- key-value store.
class ConfigSource s where
  -- | Some sources require state, e.g. to keep track of which values were
  -- already read.
  type SourceState s

  -- | read a single value from the source.
  fetchValue :: Key -> s -> StateT (SourceState s) IO (Either ConfigError (Value, Text))

  -- | given @s@, determine if any keys are "left over" and were not used.
  -- This is used to produce warnings for unknown configuration options;
  -- since not all sources can support this, this function's return type
  -- includes @Maybe@ and sources are free to return @Nothing@ if they
  -- cannot determine if any unknown keys are present.
  leftovers :: s -> StateT (SourceState s) IO (Maybe [Key])

-- | An opaque type for any kind of config sources. Values of this type can be
-- acquired from they @Conftrack.Source.*@ modules, or by implementing the
-- 'ConfigSource' type class.
data SomeSource = forall source. ConfigSource source
   => SomeSource (source, SourceState source)