Comments (6)
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.
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.
@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 fieldsOptional
, 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 theFunctor
instance forType
:{-# 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.
Perfect. Thanks!
from dhall-haskell.
You're welcome!
from dhall-haskell.
I'll go ahead and close this issue since I believe this has been answered
from dhall-haskell.
Related Issues (20)
- dhall-to-yaml does not properly quote strings HOT 1
- Can not install dhall-lsp-server HOT 2
- Allow hnix 0.17
- Hackage build failed for dhall-toml-1.0.3
- Get back into Stackage Nightly with GHC 9.8 HOT 1
- Missing binaries in release HOT 1
- Hackage release for dhall-lsp-server HOT 1
- Build failure on GHC 9.8.1: 'Illegal invisible type variable binder'
- Report imports HOT 2
- dhall-docs: Prelude.head: empty list
- Suggesting a new construct for dealing with optional fields in Dhall defaults
- dhall-json bound on aeson can be relaxed
- Questions regarding the right strategy for upgrading to ghc-9.8
- Build failure on macos-latest stack.yaml HOT 3
- Is there a specification for dhall-to-{json,yaml} and {json,yaml}-to-dhall? HOT 2
- Pretty-printer: escape unprintable characters HOT 2
- `with` expressions regressed HOT 10
- to-directory-tree with Empty Map causes an error
- Guide to compile dhall to JS backend, wasm backend and GHCJS HOT 1
- dhall format produces unparseable file when `Some` is used
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dhall-haskell.