@@ -10,13 +10,10 @@ module TypedEnv
10
10
, readValue
11
11
, class ReadEnv
12
12
, readEnv
13
- , class ReadEnvFields
14
- , readEnvFields
15
13
) where
16
14
17
15
import Prelude
18
16
19
- import Data.Bifunctor (lmap )
20
17
import Data.Either (Either (..), note )
21
18
import Data.Generic.Rep (class Generic )
22
19
import Data.Int (fromString ) as Int
@@ -30,19 +27,21 @@ import Data.Symbol (class IsSymbol, reflectSymbol)
30
27
import Foreign.Object (Object , lookup )
31
28
import Prim.Row (class Cons , class Lacks ) as Row
32
29
import Prim.RowList (class RowToList , Cons , Nil , RowList )
33
- import Record ( insert ) as Record
34
- import Type.Equality ( class TypeEquals , to )
30
+ import Record.Builder ( Builder ) as Record
31
+ import Record.Builder as RB
35
32
import Type.Proxy (Proxy (..))
36
33
import Type.RowList (class ListToRow )
37
34
38
35
-- | Gets a record of environment variables from a Node environment.
39
36
fromEnv
40
- :: forall e r proxy
41
- . ReadEnv e r
42
- => proxy e
37
+ :: forall r rl
38
+ . RowToList r rl
39
+ => ReadEnv rl () r
40
+ => Proxy r
43
41
-> Object String
44
42
-> Either (List EnvError ) (Record r )
45
- fromEnv = readEnv
43
+ fromEnv _ env = RB .buildFromScratch <$> readEnv (Proxy :: _ rl ) (Proxy :: _ ())
44
+ env
46
45
47
46
-- | An error that can occur while reading an environment variable
48
47
data EnvError
@@ -111,55 +110,36 @@ else instance readValueRequired :: ParseValue a => ReadValue a where
111
110
(note (EnvLookupError name) $ lookup name env)
112
111
>>= (parseValue >>> note (EnvParseError name))
113
112
114
- -- | Transforms a row of environment variable specifications to a record.
115
- class ReadEnv (e :: Row Type ) (r :: Row Type ) where
116
- readEnv
117
- :: forall proxy
118
- . proxy e
119
- -> Object String
120
- -> Either (List EnvError ) (Record r )
121
-
122
- instance readEnvImpl ::
123
- ( RowToList e el
124
- , RowToList r rl
125
- , ReadEnvFields el rl r
126
- , ListToRow rl r
127
- , ListToRow el l
128
- ) =>
129
- ReadEnv e r where
130
- readEnv _ = readEnvFields (Proxy :: Proxy el ) (Proxy :: Proxy rl )
131
-
132
113
-- | Transforms a list of environment variable specifications to a record.
133
114
class
134
- ReadEnvFields (el :: RowList Type ) (rl :: RowList Type ) (r :: Row Type )
135
- | el -> rl where
136
- readEnvFields
137
- :: forall proxy
138
- . proxy el
139
- -> proxy rl
115
+ ReadEnv (rl :: RowList Type ) (rin :: Row Type ) (rout :: Row Type )
116
+ | rl -> rout where
117
+ readEnv
118
+ :: Proxy rl
119
+ -> Proxy rin
140
120
-> Object String
141
- -> Either (List EnvError ) (Record r )
121
+ -> Either (List EnvError ) (Record.Builder ( Record rin ) ( Record rout ) )
142
122
143
- instance readEnvFieldsCons ::
123
+ instance readEnvCons ::
144
124
( IsSymbol name
145
- , IsSymbol name
146
- , ListToRow rlt rt
147
- , ReadEnvFields elt rlt rt
148
- , Row.Lacks name rt
149
- , Row.Cons name ty rt r
150
125
, ReadValue ty
126
+ , ListToRow tail tailRout
127
+ , ReadEnv tail rin tailRout
128
+ , Row.Cons name ty tailRout rout
129
+ , Row.Lacks name tailRout
151
130
) =>
152
- ReadEnvFields (Cons name ty elt ) ( Cons name ty rlt ) r where
153
- readEnvFields _ _ env = insert value tail
131
+ ReadEnv (Cons name ty tail ) rin rout where
132
+ readEnv _ _ env = insert value tail
154
133
where
155
134
nameP = Proxy :: _ name
156
- value = readValue (reflectSymbol nameP) env
157
- tail = readEnvFields (Proxy :: _ elt ) (Proxy :: _ rlt ) env
135
+ value = readValue (reflectSymbol nameP) env :: Either EnvError ty
136
+ tail = readEnv (Proxy :: _ tail ) (Proxy :: _ rin ) env
158
137
159
- insert (Left valueErr) (Left tailErrs) = Left $ valueErr : tailErrs
160
- insert valE tailE = Record .insert nameP <$> lmap pure valE <*> tailE
138
+ insert (Right val) (Right builder) = Right $ RB .insert nameP val <<< builder
139
+ insert (Right _) (Left errs) = Left errs
140
+ insert (Left err) (Right _) = Left $ pure err
141
+ insert (Left err) (Left errs) = Left $ err : errs
161
142
162
- instance readEnvFieldsNil ::
163
- TypeEquals { } (Record row ) =>
164
- ReadEnvFields Nil Nil row where
165
- readEnvFields _ _ _ = pure $ to {}
143
+ instance readEnvNil ::
144
+ ReadEnv Nil rout rout where
145
+ readEnv _ _ _ = pure $ RB .union {}
0 commit comments