dhall-lang / dhall-haskell

Maintainable configuration files
https://dhall-lang.org/
BSD 3-Clause "New" or "Revised" License
912 stars 213 forks source link

regression: defaultInterpretOptions is not inherited from parent record #2470

Open smarwei opened 1 year ago

smarwei commented 1 year ago

It seems like I stumbled upon a bug, which was already fixed in #33 a few years ago. I slightly adjusted the old example:

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Dhall
import qualified Data.Text

data GitRepo
  = GitRepo
  { _host :: Text
  , _repo :: Text
  } deriving (Generic, Show)

data BoxConfig
  = BoxConfig
  { _userName        :: Text
  , _dotfilesRepo    :: GitRepo
  } deriving (Generic, Show)

instance FromDhall GitRepo
instance FromDhall BoxConfig

main :: IO ()
main = do
    let auto' = genericAutoWith
            (defaultInterpretOptions { fieldModifier = Data.Text.dropWhile (== '_') })
    x <- input auto' "./config.dhall"
    print (x :: BoxConfig)

Prepending _ only works for top-level attributes.

Gabriella439 commented 1 year ago

Yeah, this change in behavior was intentional and is due to:

https://github.com/dhall-lang/dhall-haskell/pull/1696

The basic idea is that InterpretOptions are no longer applied globally but are now specified on a type-by-type basis, so the way you would fix your example is to do this:

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Dhall
import qualified Data.Text

data GitRepo
  = GitRepo
  { _host :: Text
  , _repo :: Text
  } deriving (Generic, Show)

data BoxConfig
  = BoxConfig
  { _userName        :: Text
  , _dotfilesRepo    :: GitRepo
  } deriving (Generic, Show)

instance FromDhall GitRepo where
    autoWith =
      genericAutoWithInputNormalizer defaultInterpretOptions
        { fieldModifier = Data.Text.dropWhile (== '_') }

instance FromDhall BoxConfig where
    autoWith =
      genericAutoWithInputNormalizer defaultInterpretOptions
        { fieldModifier = Data.Text.dropWhile (== '_') }

main :: IO ()
main = do
    x <- input auto "./config.dhall"
    print (x :: BoxConfig)