1
1
{-# LANGUAGE OverloadedLists #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
+ {-# LANGUAGE PatternGuards #-}
3
4
{-# LANGUAGE PatternSynonyms #-}
4
5
{-# LANGUAGE RecordWildCards #-}
5
6
116
117
> "name": "Left"
117
118
> }
118
119
119
- If @nesting@ is set to @Nested nestedField@ then the union is store
120
+ If @nesting@ is set to @Nested nestedField@ then the union is stored
120
121
underneath a field named @nestedField@. For example, this code:
121
122
122
123
> let Example = < Left : { foo : Natural } | Right : { bar : Bool } >
@@ -191,11 +192,13 @@ import Dhall.TypeCheck (X)
191
192
import Dhall.Map (Map )
192
193
import Dhall.JSON.Util (pattern V )
193
194
import Options.Applicative (Parser )
195
+ import Prelude hiding (getContents )
194
196
195
197
import qualified Data.Aeson as Aeson
196
198
import qualified Data.Foldable as Foldable
197
199
import qualified Data.HashMap.Strict as HashMap
198
200
import qualified Data.List
201
+ import qualified Data.Map
199
202
import qualified Data.Ord
200
203
import qualified Data.Text
201
204
import qualified Data.Vector as Vector
@@ -319,12 +322,7 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))
319
322
Core. RecordLit a ->
320
323
case toOrderedList a of
321
324
[ ( " contents"
322
- , Core. App
323
- (Core. Field
324
- _
325
- alternativeName
326
- )
327
- contents
325
+ , contents
328
326
)
329
327
, ( " field"
330
328
, Core. TextLit
@@ -344,28 +342,26 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))
344
342
(Core. Chunks [] nestedField)
345
343
)
346
344
)
347
- ] | all (== Core. Record [] ) mInlineType -> do
348
- contents' <- loop contents
349
-
350
- let taggedValue =
351
- Dhall.Map. fromList
352
- [ ( field
353
- , toJSON alternativeName
354
- )
355
- , ( nestedField
356
- , contents'
357
- )
358
- ]
359
-
360
- return (Aeson. toJSON ( Dhall.Map. toMap taggedValue ))
345
+ ] | all (== Core. Record [] ) mInlineType
346
+ , Just (alternativeName, mExpr) <- getContents contents -> do
347
+ contents' <- case mExpr of
348
+ Just expr -> loop expr
349
+ Nothing -> return Aeson. Null
350
+
351
+ let taggedValue =
352
+ Data.Map. fromList
353
+ [ ( field
354
+ , toJSON alternativeName
355
+ )
356
+ , ( nestedField
357
+ , contents'
358
+ )
359
+ ]
360
+
361
+ return (Aeson. toJSON taggedValue)
361
362
362
363
[ ( " contents"
363
- , Core. App
364
- (Core. Field
365
- _
366
- alternativeName
367
- )
368
- (Core. RecordLit contents)
364
+ , contents
369
365
)
370
366
, ( " field"
371
367
, Core. TextLit
@@ -374,19 +370,35 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))
374
370
, ( " nesting"
375
371
, nesting
376
372
)
377
- ] | isInlineNesting nesting -> do
378
- let contents' =
379
- Dhall.Map. insert
380
- field
381
- (Core. TextLit
382
- (Core. Chunks
383
- []
384
- alternativeName
385
- )
386
- )
387
- contents
373
+ ] | isInlineNesting nesting
374
+ , Just (alternativeName, Just (Core. RecordLit kvs)) <- getContents contents -> do
375
+ let kvs' =
376
+ Dhall.Map. insert
377
+ field
378
+ (Core. TextLit
379
+ (Core. Chunks
380
+ []
381
+ alternativeName
382
+ )
383
+ )
384
+ kvs
385
+
386
+ loop (Core. RecordLit kvs')
387
+
388
+ | isInlineNesting nesting
389
+ , Just (alternativeName, Nothing ) <- getContents contents -> do
390
+ let kvs =
391
+ Dhall.Map. singleton
392
+ field
393
+ (Core. TextLit
394
+ (Core. Chunks
395
+ []
396
+ alternativeName
397
+ )
398
+ )
399
+
400
+ loop (Core. RecordLit kvs)
388
401
389
- loop (Core. RecordLit contents')
390
402
_ -> do
391
403
a' <- traverse loop a
392
404
return (Aeson. toJSON (Dhall.Map. toMap a'))
@@ -432,6 +444,17 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))
432
444
outer value
433
445
_ -> Left (Unsupported e)
434
446
447
+ getContents :: Expr s X -> Maybe (Text , Maybe (Expr s X ))
448
+ getContents (Core. App
449
+ (Core. Field
450
+ _
451
+ alternativeName
452
+ )
453
+ expression
454
+ ) = Just (alternativeName, Just expression)
455
+ getContents (Core. Field _ alternativeName) = Just (alternativeName, Nothing )
456
+ getContents _ = Nothing
457
+
435
458
isInlineNesting :: Expr s X -> Bool
436
459
isInlineNesting (Core. App
437
460
(Core. Field
0 commit comments