summaryrefslogtreecommitdiff
path: root/src/Dhall/JSON.hs
blob: 10bb746c5b7f01c93cddb7e966d9d3febca47dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedLists    #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}

{-| This library only exports a single `dhallToJSON` function for translating a
    Dhall syntax tree to a JSON syntax tree (i.e. a `Value`) for the @aeson@
    library

    NOTE: The @yaml@ library uses the same `Value` type to represent YAML
    files, so you can use this to convert Dhall expressions to YAML, too

    See the @dhall@ package if you would like to transform Dhall source code
    into a Dhall syntax tree.  Similarly, see the @aeson@ package if you would
    like to translate a JSON syntax tree into JSON.

    This package also provides @dhall-to-json@ and @dhall-to-yaml@ executables
    which you can use to compile Dhall source code directly to JSON or YAML for
    your convenience

    Not all Dhall expressions can be converted to JSON since JSON is not a
    programming language.  The only things you can convert are:

    * @Bool@s
    * @Natural@s
    * @Integer@s
    * @Double@s
    * @Text@
    * @List@s
    * @Optional@ values
    * unions
    * records

    Dhall @Bool@s translate to JSON bools:

> $ dhall-to-json <<< 'True'
> true
> $ dhall-to-json <<< 'False'
> false

    Dhall numbers translate to JSON numbers:

> $ dhall-to-json <<< '+2'
> 2
> $ dhall-to-json <<< '2'
> 2
> $ dhall-to-json <<< '2.3'
> 2.3

    Dhall @Text@ translates to JSON text:

> $ dhall-to-json <<< '"ABC"'
> "ABC"

    Dhall @List@s translate to JSON lists:

> $ dhall-to-json <<< '[1, 2, 3] : List Integer'
> [1,2,3]

    Dhall @Optional@ values translate to @null@ if absent and the unwrapped
    value otherwise:

> $ dhall-to-json <<< '[] : Optional Integer'
> null
> $ dhall-to-json <<< '[1] : Optional Integer'
> 1

    Dhall records translate to JSON records:

> $ dhall-to-json <<< '{ foo = 1, bar = True }'
> {"foo":1,"bar":true}

    Dhall unions translate to the wrapped value:

> $ dhall-to-json <<< "< Left = +2 | Right : Natural>"
> 2
> $ cat config
> [ < Person = { age = +47, name = "John" }
>   | Place  : { location : Text }
>   >
> , < Place  = { location = "North Pole" }
>   | Person : { age : Natural, name : Text }
>   >
> , < Place  = { location = "Sahara Desert" }
>   | Person : { age : Natural, name : Text }
>   >
> , < Person = { age = +35, name = "Alice" }
>   | Place  : { location : Text }
>   >
> ]
> $ dhall-to-json <<< "./config"
> [{"age":47,"name":"John"},{"location":"North Pole"},{"location":"Sahara Desert"},{"age":35,"name":"Alice"}]

    You can preserve the name of the alternative if you wrap the value in a
    record with three fields:

    * @contents@: The union literal that you want to preserve the tag of

    * @field@: the name of the field that will store the name of the
      alternative

    * @nesting@: A value of type @\< Inline : {} | Nested : Text \>@.

    If @nesting@ is set to @Inline@ and the union literal stored in @contents@
    contains a record then the name of the alternative is stored inline within
    the same record.  For example, this code:

>     let Example = < Left : { foo : Natural } | Right : { bar : Bool } >
> 
> in  let example = constructors Example
> 
> in  let Nesting = < Inline : {} | Nested : Text >
> 
> in  let nesting = constructors Nesting
> 
> in  { field    = "name"
>     , nesting  = nesting.Inline {=}
>     , contents = example.Left { foo = 2 }
>     }

    ... produces this JSON:

> {
>   "foo": 2,
>   "name": "Left"
> }

    If @nesting@ is set to @Nested nestedField@ then the union is store
    underneath a field named @nestedField@.  For example, this code:

>     let Example = < Left : { foo : Natural } | Right : { bar : Bool } >
> 
> in  let example = constructors Example
> 
> in  let Nesting = < Inline : {} | Nested : Text >
> 
> in  let nesting = constructors Nesting
> 
> in  { field    = "name"
>     , nesting  = nesting.Nested "value"
>     , contents = example.Left { foo = 2 }
>     }

    ... produces this JSON:

> {
>   "name": "Left",
>   "value": {
>     "foo": 2
>   }
> }

    Also, all Dhall expressions are normalized before translation to JSON:

> $ dhall-to-json <<< "True == False"
> false

-}

module Dhall.JSON (
    -- * Dhall to JSON
      dhallToJSON
    , omitNull
    , omitEmpty
    , parseOmission
    , Conversion(..)
    , convertToHomogeneousMaps
    , parseConversion
    , codeToValue

    -- * Exceptions
    , CompileError(..)
    ) where

import Control.Applicative (empty, (<|>))
import Control.Monad (guard)
import Control.Exception (Exception, throwIO)
import Data.Aeson (Value(..), ToJSON(..))
import Data.Monoid ((<>), mempty)
import Data.Text (Text)
import Data.Typeable (Typeable)
import Dhall.Core (Expr)
import Dhall.TypeCheck (X)
import Dhall.Map (Map)
import Options.Applicative (Parser)

import qualified Data.Foldable
import qualified Data.HashMap.Strict
import qualified Data.List
import qualified Data.Ord
import qualified Data.Text
import qualified Dhall.Core
import qualified Dhall.Import
import qualified Dhall.Map
import qualified Dhall.Parser
import qualified Dhall.TypeCheck
import qualified Options.Applicative

{-| This is the exception type for errors that might arise when translating
    Dhall to JSON

    Because the majority of Dhall language features do not translate to JSON
    this just returns the expression that failed
-}
data CompileError = Unsupported (Expr X X) deriving (Typeable)

instance Show CompileError where
    show (Unsupported e) =
        Data.Text.unpack $
            "" <> _ERROR <> ": Cannot translate to JSON                                     \n\
            \                                                                               \n\
            \Explanation: Only primitive values, records, unions, ❰List❱s, and ❰Optional❱   \n\
            \values can be translated from Dhall to JSON                                    \n\
            \                                                                               \n\
            \The following Dhall expression could not be translated to JSON:                \n\
            \                                                                               \n\
            \↳ " <> txt <> "                                                                "
      where
        txt = Dhall.Core.pretty e

_ERROR :: Data.Text.Text
_ERROR = "\ESC[1;31mError\ESC[0m"

instance Exception CompileError

{-| Convert a Dhall expression to the equivalent JSON expression

>>> :set -XOverloadedStrings
>>> :set -XOverloadedLists
>>> import Dhall.Core
>>> dhallToJSON (RecordLit [("foo", IntegerLit 1), ("bar", TextLit "ABC")])
Right (Object (fromList [("foo",Number 1.0),("bar",String "ABC")]))
>>> fmap Data.Aeson.encode it
Right "{\"foo\":1,\"bar\":\"ABC\"}"
-}
dhallToJSON :: Expr s X -> Either CompileError Value
dhallToJSON e0 = loop (Dhall.Core.normalize e0)
  where
    loop e = case e of 
        Dhall.Core.BoolLit a -> return (toJSON a)
        Dhall.Core.NaturalLit a -> return (toJSON a)
        Dhall.Core.IntegerLit a -> return (toJSON a)
        Dhall.Core.DoubleLit a
          | isInfinite a && a > 0 -> return (toJSON ( 1.7976931348623157e308 :: Double))
          | isInfinite a && a < 0 -> return (toJSON (-1.7976931348623157e308 :: Double))
          | otherwise -> return (toJSON a)
        Dhall.Core.TextLit (Dhall.Core.Chunks [] a) -> do
            return (toJSON a)
        Dhall.Core.ListLit _ a -> do
            a' <- traverse loop a
            return (toJSON a')
        Dhall.Core.OptionalLit _ a -> do
            a' <- traverse loop a
            return (toJSON a')
        Dhall.Core.Some a -> do
            a' <- loop a
            return (toJSON a')
        Dhall.Core.App Dhall.Core.None _ -> do
            return Data.Aeson.Null
        Dhall.Core.RecordLit a ->
            case toOrderedList a of
                [   (   "contents"
                    ,   Dhall.Core.UnionLit alternativeName contents _
                    )
                 ,  (   "field"
                    ,   Dhall.Core.TextLit
                            (Dhall.Core.Chunks [] field)
                    )
                 ,  (   "nesting"
                    ,   Dhall.Core.UnionLit
                            "Nested"
                            (Dhall.Core.TextLit
                                (Dhall.Core.Chunks [] nestedField)
                            )
                            [ ("Inline", Dhall.Core.Record []) ]
                    )
                 ] -> do
                    contents' <- loop contents

                    let taggedValue =
                            Dhall.Map.fromList
                                [   (   field
                                    ,   toJSON alternativeName
                                    )
                                ,   (   nestedField
                                    ,   contents'
                                    )
                                ]

                    return (Data.Aeson.toJSON ( Dhall.Map.toMap taggedValue ))

                [   (   "contents"
                    ,   Dhall.Core.UnionLit
                            alternativeName
                            (Dhall.Core.RecordLit contents)
                            _
                    )
                 ,  (   "field"
                    ,   Dhall.Core.TextLit
                            (Dhall.Core.Chunks [] field)
                    )
                 ,  (   "nesting"
                    ,   Dhall.Core.UnionLit
                            "Inline"
                            (Dhall.Core.RecordLit [])
                            [ ("Nested", Dhall.Core.Text) ]
                    )
                 ] -> do
                    let contents' =
                            Dhall.Map.insert
                                field
                                (Dhall.Core.TextLit
                                    (Dhall.Core.Chunks
                                        []
                                        alternativeName
                                    )
                                )
                                contents

                    loop (Dhall.Core.RecordLit contents')
                _ -> do
                    a' <- traverse loop a
                    return (Data.Aeson.toJSON (Dhall.Map.toMap a'))
        Dhall.Core.UnionLit _ b _ -> loop b
        _ -> Left (Unsupported e)

toOrderedList :: Ord k => Map k v -> [(k, v)]
toOrderedList =
        Data.List.sortBy (Data.Ord.comparing fst)
    .   Dhall.Map.toList

-- | Omit record fields that are @null@
omitNull :: Value -> Value
omitNull (Object object) = Object fields
  where
    fields =Data.HashMap.Strict.filter (/= Null) (fmap omitNull object)
omitNull (Array array) =
    Array (fmap omitNull array)
omitNull (String string) =
    String string
omitNull (Number number) =
    Number number
omitNull (Bool bool) =
    Bool bool
omitNull Null =
    Null

{-| Omit record fields that are @null@ or records whose transitive fields are
    all null
-}
omitEmpty :: Value -> Value
omitEmpty (Object object) =
    if null fields then Null else Object fields
  where
    fields = Data.HashMap.Strict.filter (/= Null) (fmap omitEmpty object)
omitEmpty (Array array) =
    Array (fmap omitEmpty array)
omitEmpty (String string) =
    String string
omitEmpty (Number number) =
    Number number
omitEmpty (Bool bool) =
    Bool bool
omitEmpty Null =
    Null

-- | Parser for command-line options related to omitting fields
parseOmission :: Parser (Value -> Value)
parseOmission =
        Options.Applicative.flag'
            omitNull
            (   Options.Applicative.long "omitNull"
            <>  Options.Applicative.help "Omit record fields that are null"
            )
    <|> Options.Applicative.flag'
            omitEmpty
            (   Options.Applicative.long "omitEmpty"
            <>  Options.Applicative.help "Omit record fields that are null or empty records"
            )
    <|> pure id

{-| Specify whether or not to convert association lists of type
    @List { mapKey: Text, mapValue : v }@ to records
-}
data Conversion
    = NoConversion
    | Conversion { mapKey :: Text, mapValue :: Text }

{-| Convert association lists to homogeneous maps

    This converts an association list of the form:

    > [ { mapKey = k0, mapValue = v0 }, { mapKey = k1, mapValue = v1 } ]

    ... to a record of the form:

    > { k0 = v0, k1 = v1 }
-}
convertToHomogeneousMaps :: Conversion -> Expr s X -> Expr s X
convertToHomogeneousMaps NoConversion e0 = e0
convertToHomogeneousMaps (Conversion {..}) e0 = loop (Dhall.Core.normalize e0)
  where
    loop e = case e of
        Dhall.Core.Const a ->
            Dhall.Core.Const a

        Dhall.Core.Var v ->
            Dhall.Core.Var v

        Dhall.Core.Lam a b c ->
            Dhall.Core.Lam a b' c'
          where
            b' = loop b
            c' = loop c

        Dhall.Core.Pi a b c ->
            Dhall.Core.Pi a b' c'
          where
            b' = loop b
            c' = loop c

        Dhall.Core.App a b ->
            Dhall.Core.App a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Let as b ->
            Dhall.Core.Let as' b'
          where
            f (Dhall.Core.Binding x y z) = Dhall.Core.Binding x y' z'
              where
                y' = fmap loop y
                z' =      loop z

            as' = fmap f as

            b' = loop b

        Dhall.Core.Annot a b ->
            Dhall.Core.Annot a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Bool ->
            Dhall.Core.Bool

        Dhall.Core.BoolLit a ->
            Dhall.Core.BoolLit a

        Dhall.Core.BoolAnd a b ->
            Dhall.Core.BoolAnd a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.BoolOr a b ->
            Dhall.Core.BoolOr a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.BoolEQ a b ->
            Dhall.Core.BoolEQ a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.BoolNE a b ->
            Dhall.Core.BoolNE a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.BoolIf a b c ->
            Dhall.Core.BoolIf a' b' c'
          where
            a' = loop a
            b' = loop b
            c' = loop c

        Dhall.Core.Natural ->
            Dhall.Core.Natural

        Dhall.Core.NaturalLit a ->
            Dhall.Core.NaturalLit a

        Dhall.Core.NaturalFold ->
            Dhall.Core.NaturalFold

        Dhall.Core.NaturalBuild ->
            Dhall.Core.NaturalBuild

        Dhall.Core.NaturalIsZero ->
            Dhall.Core.NaturalIsZero

        Dhall.Core.NaturalEven ->
            Dhall.Core.NaturalEven

        Dhall.Core.NaturalOdd ->
            Dhall.Core.NaturalOdd

        Dhall.Core.NaturalToInteger ->
            Dhall.Core.NaturalToInteger

        Dhall.Core.NaturalShow ->
            Dhall.Core.NaturalShow

        Dhall.Core.NaturalPlus a b ->
            Dhall.Core.NaturalPlus a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.NaturalTimes a b ->
            Dhall.Core.NaturalTimes a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Integer ->
            Dhall.Core.Integer

        Dhall.Core.IntegerLit a ->
            Dhall.Core.IntegerLit a

        Dhall.Core.IntegerShow ->
            Dhall.Core.IntegerShow

        Dhall.Core.IntegerToDouble ->
            Dhall.Core.IntegerToDouble

        Dhall.Core.Double ->
            Dhall.Core.Double

        Dhall.Core.DoubleLit a ->
            Dhall.Core.DoubleLit a

        Dhall.Core.DoubleShow ->
            Dhall.Core.DoubleShow

        Dhall.Core.Text ->
            Dhall.Core.Text

        Dhall.Core.TextLit (Dhall.Core.Chunks a b) ->
            Dhall.Core.TextLit (Dhall.Core.Chunks a' b)
          where
            a' = fmap (fmap loop) a

        Dhall.Core.TextAppend a b ->
            Dhall.Core.TextAppend a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.TextShow ->
            Dhall.Core.TextShow

        Dhall.Core.List ->
            Dhall.Core.List

        Dhall.Core.ListLit a b ->
            case transform of
                Just c  -> loop c
                Nothing -> Dhall.Core.ListLit a' b'
          where
            elements = Data.Foldable.toList b

            toKeyValue :: Expr s X -> Maybe (Text, Expr s X)
            toKeyValue (Dhall.Core.RecordLit m) = do
                guard (Data.Foldable.length m == 2)

                key   <- Dhall.Map.lookup mapKey   m
                value <- Dhall.Map.lookup mapValue m

                keyText <- case key of
                    Dhall.Core.TextLit (Dhall.Core.Chunks [] keyText) ->
                        return keyText

                    _ ->
                        empty

                return (keyText, value)
            toKeyValue _ = do
                empty

            transform =
                case elements of
                    [] ->
                        case a of
                            Just (Dhall.Core.Record m) -> do
                                guard (Data.Foldable.length m == 2)
                                guard (Dhall.Map.member mapKey   m)
                                guard (Dhall.Map.member mapValue m)
                                return (Dhall.Core.RecordLit mempty)
                            _ -> do
                                empty

                    _  -> do
                        keyValues <- traverse toKeyValue elements

                        let recordLiteral =
                                Dhall.Map.fromList keyValues

                        return (Dhall.Core.RecordLit recordLiteral)

            a' = fmap loop a
            b' = fmap loop b

        Dhall.Core.ListAppend a b ->
            Dhall.Core.ListAppend a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.ListBuild ->
            Dhall.Core.ListBuild

        Dhall.Core.ListFold ->
            Dhall.Core.ListFold

        Dhall.Core.ListLength ->
            Dhall.Core.ListLength

        Dhall.Core.ListHead ->
            Dhall.Core.ListHead

        Dhall.Core.ListLast ->
            Dhall.Core.ListLast

        Dhall.Core.ListIndexed ->
            Dhall.Core.ListIndexed

        Dhall.Core.ListReverse ->
            Dhall.Core.ListReverse

        Dhall.Core.Optional ->
            Dhall.Core.Optional

        Dhall.Core.OptionalLit a b ->
            Dhall.Core.OptionalLit a' b'
          where
            a' =      loop a
            b' = fmap loop b

        Dhall.Core.Some a ->
            Dhall.Core.Some a'
          where
            a' = loop a

        Dhall.Core.None ->
            Dhall.Core.None

        Dhall.Core.OptionalFold ->
            Dhall.Core.OptionalFold

        Dhall.Core.OptionalBuild ->
            Dhall.Core.OptionalBuild

        Dhall.Core.Record a ->
            Dhall.Core.Record a'
          where
            a' = fmap loop a

        Dhall.Core.RecordLit a ->
            Dhall.Core.RecordLit a'
          where
            a' = fmap loop a

        Dhall.Core.Union a ->
            Dhall.Core.Union a'
          where
            a' = fmap loop a

        Dhall.Core.UnionLit a b c ->
            Dhall.Core.UnionLit a b' c'
          where
            b' =      loop b
            c' = fmap loop c

        Dhall.Core.Combine a b ->
            Dhall.Core.Combine a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.CombineTypes a b ->
            Dhall.Core.CombineTypes a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Prefer a b ->
            Dhall.Core.Prefer a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Merge a b c ->
            Dhall.Core.Merge a' b' c'
          where
            a' =      loop a
            b' =      loop b
            c' = fmap loop c

        Dhall.Core.Field a b ->
            Dhall.Core.Field a' b
          where
            a' = loop a

        Dhall.Core.Project a b ->
            Dhall.Core.Project a' b
          where
            a' = loop a

        Dhall.Core.ImportAlt a b ->
            Dhall.Core.ImportAlt a' b'
          where
            a' = loop a
            b' = loop b

        Dhall.Core.Note a b ->
            Dhall.Core.Note a b'
          where
            b' = loop b

        Dhall.Core.Embed a ->
            Dhall.Core.Embed a

-- | Parser for command-line options related to homogeneous map support
parseConversion :: Parser Conversion
parseConversion =
        conversion
    <|> noConversion
  where
    conversion = Conversion <$> parseKeyField <*> parseValueField
      where
        parseKeyField =
            Options.Applicative.strOption
                (   Options.Applicative.long "key"
                <>  Options.Applicative.help "Reserved key field name for association lists"
                <>  Options.Applicative.value "mapKey"
                <>  Options.Applicative.showDefaultWith Data.Text.unpack
                )

        parseValueField =
            Options.Applicative.strOption
                (   Options.Applicative.long "value"
                <>  Options.Applicative.help "Reserved value field name for association lists"
                <>  Options.Applicative.value "mapValue"
                <>  Options.Applicative.showDefaultWith Data.Text.unpack
                )

    noConversion =
        Options.Applicative.flag'
            NoConversion
            (   Options.Applicative.long "noMaps"
            <>  Options.Applicative.help "Disable conversion of association lists to homogeneous maps"
            )


{-| Convert a piece of Text carrying a Dhall inscription to an equivalent JSON Value

>>> :set -XOverloadedStrings
>>> import Dhall.Core
>>> Dhall.JSON.codeToValue "(stdin)" "{ a = 1 }"
>>> Object (fromList [("a",Number 1.0)])
-}
codeToValue
  :: Conversion
  -> Text  -- ^ Describe the input for the sake of error location.
  -> Text  -- ^ Input text.
  -> IO Value
codeToValue conversion name code = do
    parsedExpression <- case Dhall.Parser.exprFromText (Data.Text.unpack name) code of
      Left  err              -> Control.Exception.throwIO err
      Right parsedExpression -> return parsedExpression

    resolvedExpression <- Dhall.Import.load parsedExpression

    case Dhall.TypeCheck.typeOf resolvedExpression  of
      Left  err -> Control.Exception.throwIO err
      Right _   -> return ()

    let convertedExpression =
            convertToHomogeneousMaps conversion resolvedExpression

    case dhallToJSON convertedExpression of
      Left  err  -> Control.Exception.throwIO err
      Right json -> return json