Coder Social home page Coder Social logo

Comments (6)

Gabriella439 avatar Gabriella439 commented on May 29, 2024

I'm actually okay with exposing the innards of Type. The only reason I haven't exposed it yet was just as a precaution in case I wanted to make internal changes to Type without breaking API changes.

Before I do that, though, I just want to make sure it's necessary. There are ways to define new Interpret instances without exposing the Type internals by using the Functor instance for type instead. Do you have an example of the type that you wanted to implement Interpret for?

from dhall-haskell.

newhoggy avatar newhoggy commented on May 29, 2024

What do I need to do to get the instance Interpret (Map String String)?

I'm trying to load "environment variables" like this from a config file:

{ HACKAGE_PASS="xxxx"
, HACKAGE_USER="xxxx"
, ...
}

These will be used to set up environment variables, and there can be any number of them.

from dhall-haskell.

Gabriella439 avatar Gabriella439 commented on May 29, 2024

@newhoggy: You can't load a Dhall record into a Haskell Map String String, even if I exposed the internals of the Type constructor. To see why, think about what the expected field of the Type would be

I would suggest that you should instead do one of the following two approaches:

  • If you know what the record fields will be ahead of time then load them into a Haskell record with matching fields, using Dhall's support for GHC generics, like this:

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveAnyClass    #-}
    {-# LANGUAGE DeriveGeneric     #-}
    
    import Dhall
    
    data Config = Config
        { hackagePass :: Text
        , hackageUser :: Text
        } deriving (Generic, Interpret, Show)
    
    main :: IO ()
    main = do
        x <- input auto "./example"
        print (x :: Config)

    ... with an input file like this:

    { hackagePass = "xxx"
    , hackageUser = "xxx"
    }
  • If some fields are going to be optional then make the Haskell record fields Maybe and the corresponding Dhall fields Optional, like this:

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveAnyClass    #-}
    {-# LANGUAGE DeriveGeneric     #-}
    
    import Dhall
    
    data Config = Config
        { hackagePass :: Maybe Text
        , hackageUser :: Text
        } deriving (Generic, Interpret, Show)
    
    main :: IO ()
    main = do
        x <- input auto "./example3"
        print (x :: Config)

    ... with an input file like this:

    { hackagePass = [] : Optional Text
    , hackageUser = "xxx"
    }
  • If you don't know what the fields will be ahead of time, then do this:

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveAnyClass    #-}
    {-# LANGUAGE DeriveGeneric     #-}
    
    import Dhall
    
    data KeyVal = KeyVal { key :: Text, val :: Text }
        deriving (Generic, Interpret, Show)
    
    main :: IO ()
    main = do
        x <- input auto "./example"
        print (x :: Vector KeyVal)

    ... with an input file like this:

    [ { key = "HACKAGE_PASS" , val = "xxx" }
    , { key = "HACKAGE_USER" , val = "xxx" }
    ]
  • If you want the previous approach but you want to decode directly into a Map Text Text, then use the Functor instance for Type:

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveAnyClass    #-}
    {-# LANGUAGE DeriveGeneric     #-}
    {-# LANGUAGE RecordWildCards   #-}
    
    import Data.Map (Map)
    import Dhall
    
    import qualified Data.Vector
    import qualified Data.Map
    
    data KeyVal = KeyVal { key :: Text, val :: Text }
        deriving (Generic, Interpret, Show)
    
    newtype Config = Config { keyVals :: Map Text Text } deriving (Show)
    
    instance Interpret Config where
        auto = fmap adapt0 auto
          where
            adapt0 = Config . Data.Map.fromList . fmap adapt1 . Data.Vector.toList
              where
                adapt1 (KeyVal {..}) = (key, val)
    
    main :: IO ()
    main = do
        x <- input auto "./example"
        print (x :: Config)

from dhall-haskell.

newhoggy avatar newhoggy commented on May 29, 2024

Perfect. Thanks!

from dhall-haskell.

Gabriella439 avatar Gabriella439 commented on May 29, 2024

You're welcome!

from dhall-haskell.

Gabriella439 avatar Gabriella439 commented on May 29, 2024

I'll go ahead and close this issue since I believe this has been answered

from dhall-haskell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.