1
- {-# LANGUAGE AllowAmbiguousTypes #-}
2
- {-# LANGUAGE BangPatterns #-}
3
- {-# LANGUAGE DataKinds #-}
4
- {-# LANGUAGE DuplicateRecordFields #-}
5
- {-# LANGUAGE FlexibleContexts #-}
6
- {-# LANGUAGE FlexibleInstances #-}
7
- {-# LANGUAGE GADTs #-}
8
- {-# LANGUAGE MultiParamTypeClasses #-}
9
- {-# LANGUAGE NoImplicitPrelude #-}
10
- {-# LANGUAGE OverloadedStrings #-}
11
- {-# LANGUAGE PolyKinds #-}
12
- {-# LANGUAGE ScopedTypeVariables #-}
13
- {-# LANGUAGE TypeApplications #-}
14
- {-# LANGUAGE TypeOperators #-}
15
- {-# LANGUAGE UndecidableInstances #-}
16
- {-# OPTIONS_GHC -fno-warn-orphans #-}
1
+ {-# language AllowAmbiguousTypes #-}
2
+ {-# language BangPatterns #-}
3
+ {-# language DataKinds #-}
4
+ {-# language DuplicateRecordFields #-}
5
+ {-# language FlexibleContexts #-}
6
+ {-# language FlexibleInstances #-}
7
+ {-# language GADTs #-}
8
+ {-# language MultiParamTypeClasses #-}
9
+ {-# language OverloadedStrings #-}
10
+ {-# language PolyKinds #-}
11
+ {-# language ScopedTypeVariables #-}
12
+ {-# language TypeApplications #-}
13
+ {-# language TypeOperators #-}
14
+ {-# language UndecidableInstances #-}
15
+ {-# options_ghc -fno-warn-orphans #-}
17
16
module Servant.To.Elm where
18
17
19
- import Protolude hiding (Type , functionName , moduleName )
20
-
21
18
import qualified Bound
22
19
import qualified Data.Aeson as Aeson
23
20
import qualified Data.Char as Char
21
+ import Data.Proxy
22
+ import Data.String
23
+ import Data.Text (Text )
24
24
import qualified Data.Text as Text
25
+ import qualified Data.Text.Encoding as Text
26
+ import Data.Void
27
+ import GHC.TypeLits
25
28
import qualified Network.HTTP.Types as HTTP
26
29
import Servant.API ((:<|>) , (:>) )
27
30
import qualified Servant.API as Servant
28
- import qualified Servant.Multipart as Servant
29
31
import qualified Servant.API.Modifiers as Servant
32
+ import qualified Servant.Multipart as Servant
30
33
31
34
import Language.Elm.Definition (Definition )
32
35
import qualified Language.Elm.Definition as Definition
@@ -49,7 +52,7 @@ elmEndpointDefinition urlBase moduleName endpoint =
49
52
(Name. Qualified moduleName functionName)
50
53
0
51
54
(Bound. toScope $ vacuous $ elmTypeSig)
52
- (panic " expression not closed" <$> lambdaArgs argNames elmLambdaBody)
55
+ (error " expression not closed" <$> lambdaArgs argNames elmLambdaBody)
53
56
where
54
57
functionName =
55
58
case _functionName endpoint of
@@ -162,7 +165,7 @@ elmEndpointDefinition urlBase moduleName endpoint =
162
165
Expression. App
163
166
" Http.request"
164
167
(Expression. Record
165
- [ (" method" , Expression. String $ toS $ _method endpoint)
168
+ [ (" method" , Expression. String $ Text. decodeUtf8 $ _method endpoint)
166
169
, (" headers" , elmHeaders)
167
170
, (" url" , elmUrl)
168
171
, (" body" , elmBody)
@@ -325,7 +328,7 @@ elmEndpointDefinition urlBase moduleName endpoint =
325
328
, Bound. toScope $
326
329
case _returnType endpoint of
327
330
Nothing ->
328
- panic " elmRequest: No return type" -- TODO?
331
+ error " elmRequest: No return type" -- TODO?
329
332
330
333
Just (Left Servant. NoContent ) ->
331
334
Expression. if_ (Expression. apps (" Basics.==" ) [pure $ Bound. B 1 , Expression. String " " ])
@@ -367,15 +370,15 @@ elmEndpointDefinition urlBase moduleName endpoint =
367
370
368
371
headerArgName :: Int -> Text
369
372
headerArgName i =
370
- " header" <> show i
373
+ " header" <> fromString ( show i)
371
374
372
375
capturedArgName :: Int -> Text
373
376
capturedArgName i =
374
- " capture" <> show i
377
+ " capture" <> fromString ( show i)
375
378
376
379
paramArgName :: Int -> Text
377
380
paramArgName i =
378
- " param" <> show i
381
+ " param" <> fromString ( show i)
379
382
380
383
-------------------------------------------------------------------------------
381
384
-- * Endpoints
@@ -460,7 +463,7 @@ instance (KnownSymbol symbol, HasElmEncoder Text a, HasElmEndpoints api)
460
463
}
461
464
where
462
465
str =
463
- toS $ symbolVal $ Proxy @ symbol
466
+ fromString $ symbolVal $ Proxy @ symbol
464
467
465
468
instance (KnownSymbol symbol , HasElmEncoder Text a , HasElmEndpoints api )
466
469
=> HasElmEndpoints (Servant. CaptureAll symbol a :> api ) where
@@ -473,15 +476,15 @@ instance (KnownSymbol symbol, HasElmEncoder Text a, HasElmEndpoints api)
473
476
}
474
477
where
475
478
str =
476
- toS $ symbolVal $ Proxy @ symbol
479
+ fromString $ symbolVal $ Proxy @ symbol
477
480
478
481
instance (Servant. ReflectMethod method , HasElmDecoder Aeson. Value a , list ~ '[Servant. JSON ])
479
482
=> HasElmEndpoints (Servant. Verb method 200 list a ) where
480
483
elmEndpoints' prefix =
481
484
[ prefix
482
485
{ _method = method
483
486
, _returnType = Just $ Right $ makeDecoder @ Aeson. Value @ a
484
- , _functionName = Text. toLower (toS method) : _functionName prefix
487
+ , _functionName = Text. toLower (Text. decodeUtf8 method) : _functionName prefix
485
488
}
486
489
]
487
490
where
@@ -493,7 +496,7 @@ instance Servant.ReflectMethod method => HasElmEndpoints (Servant.Verb method 20
493
496
[ prefix
494
497
{ _method = method
495
498
, _returnType = Just $ Left Servant. NoContent
496
- , _functionName = Text. toLower (toS method) : _functionName prefix
499
+ , _functionName = Text. toLower (Text. decodeUtf8 method) : _functionName prefix
497
500
}
498
501
]
499
502
where
@@ -509,7 +512,7 @@ instance
509
512
elmEndpoints' prefix =
510
513
elmEndpoints' @ api prefix
511
514
{ _headers = _headers prefix <>
512
- [ ( toS $ symbolVal $ Proxy @ symbol
515
+ [ ( fromString $ symbolVal $ Proxy @ symbol
513
516
, makeEncoder @ (Servant. RequiredArgument mods Text ) @ (Servant. RequiredArgument mods a )
514
517
, case Servant. sbool @ (Servant. FoldRequired mods ) of
515
518
Servant. STrue ->
@@ -532,7 +535,7 @@ instance
532
535
{ _url = (_url prefix)
533
536
{ _queryString =
534
537
_queryString (_url prefix) <>
535
- [ ( toS $ symbolVal $ Proxy @ symbol
538
+ [ ( fromString $ symbolVal $ Proxy @ symbol
536
539
, case Servant. sbool @ (Servant. FoldRequired mods ) of
537
540
Servant. STrue ->
538
541
Required
@@ -552,7 +555,7 @@ instance (KnownSymbol symbol, HasElmEncoder Text a, HasElmEndpoints api)
552
555
{ _url = (_url prefix)
553
556
{ _queryString =
554
557
_queryString (_url prefix) <>
555
- [ ( toS $ symbolVal $ Proxy @ symbol
558
+ [ ( fromString $ symbolVal $ Proxy @ symbol
556
559
, List
557
560
, makeEncoder @ Text @ a
558
561
)
@@ -567,7 +570,7 @@ instance (KnownSymbol symbol, HasElmEndpoints api)
567
570
{ _url = (_url prefix)
568
571
{ _queryString =
569
572
_queryString (_url prefix) <>
570
- [ ( toS $ symbolVal $ Proxy @ symbol
573
+ [ ( fromString $ symbolVal $ Proxy @ symbol
571
574
, Flag
572
575
, Encoder " Basics.identity" " Basics.Bool"
573
576
)
@@ -599,7 +602,7 @@ instance (KnownSymbol path, HasElmEndpoints api) => HasElmEndpoints (path :> api
599
602
}
600
603
where
601
604
path =
602
- toS $ symbolVal $ Proxy @ path
605
+ fromString $ symbolVal $ Proxy @ path
603
606
604
607
instance HasElmEndpoints api => HasElmEndpoints (Servant. RemoteHost :> api ) where
605
608
elmEndpoints' = elmEndpoints' @ api
0 commit comments