brandonchinn178 / brandonchinn178/toml-reader

Make it easy to get unused fields

Open
#12 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
16
Forks
8
PR merge metrics
No merged PRs in 30d

Description

Make it easy to get the fields in a Table that aren't used. Something like
```hs
decodeFoo :: Text -> IO Foo
decodeFoo doc =
case decodeWith (withUnusedFields tomlDecoder) doc of
Left e -> error $ show e
Right (a, []) -> pure a
Right (_, kvs) -> error $ "Found unknown keys: " ++ show (Map.keys kvs)

data Bar = Bar { a :: Bool, b :: Bool, extra :: Map Text Int }
instance DecodeTOML Bar where
tomlDecoder = do
(bar, unused) <- withUnusedFields $ Bar <$> getField "a" <*> getField "b" <*> pure Map.empty
-- https://github.com/brandonchinn178/toml-reader/issues/11
extra <- toDecoder $ mapM (runDecoder tomlDecoder) unused
pure bar{extra = extra}

withUnusedFields :: Decoder a -> Decoder (a, Map Text Value)
```

One possible implementation:
1. Make `Decoder a` return `DecodeM (a, [Text])`, storing the keys in the table that have been used
2. Make `Applicative`/`Monad` instance combine the `[Text]` (like `WriterT`)
3. Change `getFieldsOptWith` to add the path being queried to the list (just the first key)

Possibly have withUnusedFields return any `DecodeTOML` instance?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the Decoder and DecodeM design, then inspect getFieldsOptWith and the Applicative/Monad behavior described in the issue. Determine how withUnusedFields should expose unused table keys and how nested decoding should preserve them. Done means the proposed API works for the shown examples and unused fields can be identified reliably.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.