From 4321bb0b5b90c0f92217ccd07a67f17fce44b388 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Thu, 6 Jun 2024 18:26:42 +0200 Subject: nested values, tests, and aeson --- src/Conftrack/Source/Trivial.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Conftrack/Source/Trivial.hs (limited to 'src/Conftrack/Source/Trivial.hs') diff --git a/src/Conftrack/Source/Trivial.hs b/src/Conftrack/Source/Trivial.hs new file mode 100644 index 0000000..bb06e77 --- /dev/null +++ b/src/Conftrack/Source/Trivial.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE QuantifiedConstraints #-} +{-# LANGUAGE ImpredicativeTypes #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE OverloadedStrings #-} + +module Conftrack.Source.Trivial where + +import Conftrack.Value (Key, Value(..), ConfigError(..), Origin) +import Conftrack.Source (SomeSource(..), ConfigSource (..)) + +import Control.Monad.State (get, modify, StateT (..), MonadState (..)) +import Data.Map.Strict (Map) +import qualified Data.Map.Strict as M +import Data.Function ((&)) +import qualified Data.Text as T + + +newtype Trivial = Trivial (Map Key Value) + +mkTrivialSource :: [(Key, Value)] -> SomeSource +mkTrivialSource pairs = SomeSource (source, []) + where source = Trivial (M.fromList pairs) + +instance ConfigSource Trivial where + type SourceState Trivial = [Key] + fetchValue key (Trivial tree) = do + case M.lookup key tree of + Nothing -> pure $ Left NotPresent + Just val -> do + modify (key :) + pure $ Right (val, "Trivial source with keys "<> T.pack (show (M.keys tree))) + + leftovers (Trivial tree) = do + used <- get + + M.keys tree + & filter (`notElem` used) + & Just + & pure -- cgit v1.2.3