From d10c2f0dac08fb2cf0a7df2fb6a745a4759a73cf Mon Sep 17 00:00:00 2001 From: stuebinm Date: Fri, 7 Jun 2024 22:43:14 +0200 Subject: nested configs which are optional --- src/Conftrack.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Conftrack.hs b/src/Conftrack.hs index b15fa02..ab830ff 100644 --- a/src/Conftrack.hs +++ b/src/Conftrack.hs @@ -16,6 +16,7 @@ module Conftrack , readOptionalValue , readRequiredValue , readNested + , readNestedOptional , SomeSource , ConfigError(..) , Key(..) @@ -145,6 +146,17 @@ readNested (Key prefix') = Fetch $ \s1 -> do (config, s2) <- nested (s1 { fetcherPrefix = fetcherPrefix s1 <> NonEmpty.toList prefix' }) pure (config, s2 { fetcherPrefix = fetcherPrefix s1 }) +readNestedOptional :: forall a. Config a => Key -> Fetch (Maybe a) +readNestedOptional (Key prefix) = Fetch $ \s1 -> do + let (Fetch nested) = readConfig @a + (config, s2) <- nested (s1 { fetcherPrefix = fetcherPrefix s1 <> NonEmpty.toList prefix }) + + if null (fetcherErrors s2) + then pure (Just config, s2 { fetcherPrefix = fetcherPrefix s1 }) + -- TODO: resetting errors like this makes configKeysOf less useful. Perhaps move nested errors to warnings? + else pure (Nothing, s2 { fetcherPrefix = fetcherPrefix s1 + , fetcherErrors = fetcherErrors s1 }) + collectUnused :: [SomeSource] -> IO [Warning] collectUnused sources = do -- cgit v1.2.3