@@ -10,13 +10,10 @@ module TypedEnv
1010 , readValue
1111 , class ReadEnv
1212 , readEnv
13- , class ReadEnvFields
14- , readEnvFields
1513 ) where
1614
1715import Prelude
1816
19- import Data.Bifunctor (lmap )
2017import Data.Either (Either (..), note )
2118import Data.Generic.Rep (class Generic )
2219import Data.Int (fromString ) as Int
@@ -30,19 +27,21 @@ import Data.Symbol (class IsSymbol, reflectSymbol)
3027import Foreign.Object (Object , lookup )
3128import Prim.Row (class Cons , class Lacks ) as Row
3229import 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
3532import Type.Proxy (Proxy (..))
3633import Type.RowList (class ListToRow )
3734
3835-- | Gets a record of environment variables from a Node environment.
3936fromEnv
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
4341 -> Object String
4442 -> Either (List EnvError ) (Record r )
45- fromEnv = readEnv
43+ fromEnv _ env = RB .buildFromScratch <$> readEnv (Proxy :: _ rl ) (Proxy :: _ ())
44+ env
4645
4746-- | An error that can occur while reading an environment variable
4847data EnvError
@@ -111,55 +110,36 @@ else instance readValueRequired :: ParseValue a => ReadValue a where
111110 (note (EnvLookupError name) $ lookup name env)
112111 >>= (parseValue >>> note (EnvParseError name))
113112
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-
132113-- | Transforms a list of environment variable specifications to a record.
133114class
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
140120 -> Object String
141- -> Either (List EnvError ) (Record r )
121+ -> Either (List EnvError ) (Record.Builder ( Record rin ) ( Record rout ) )
142122
143- instance readEnvFieldsCons ::
123+ instance readEnvCons ::
144124 ( 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
150125 , ReadValue ty
126+ , ListToRow tail tailRout
127+ , ReadEnv tail rin tailRout
128+ , Row.Cons name ty tailRout rout
129+ , Row.Lacks name tailRout
151130 ) =>
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
154133 where
155134 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
158137
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
161142
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