Skip to content

Commit 13266da

Browse files
authored
Cleanup / Record Builder (#9)
* Use concrete Proxy type. * Remove unnecessary type class cruft. * Remove redundant row proxy. * Use Record Builder to improve performance. * Remove redundant dependencies. * Dependency fix
1 parent 8cd8b98 commit 13266da

File tree

4 files changed

+33
-54
lines changed

4 files changed

+33
-54
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"purescript-either": "^v6.1.0",
1818
"purescript-foreign-object": "^v4.0.0",
1919
"purescript-integers": "^v6.0.0",
20+
"purescript-lists": "^v7.0.0",
2021
"purescript-maybe": "^v6.0.0",
2122
"purescript-numbers": "^v9.0.0",
2223
"purescript-prelude": "^v6.0.0",
2324
"purescript-record": "^v4.0.0",
2425
"purescript-strings": "^v6.0.0",
25-
"purescript-type-equality": "^v4.0.1",
2626
"purescript-typelevel-prelude": "^v7.0.0"
2727
}
2828
}

example.dhall

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ in conf // {
99
, "lists"
1010
, "node-process"
1111
, "transformers"
12+
, "type-equality"
1213
]
1314
}

spago.dhall

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
, license = "MIT"
33
, repository = "https://github.com/nsaunders/purescript-typedenv"
44
, dependencies =
5-
[ "bifunctors"
6-
, "either"
5+
[ "either"
76
, "foreign-object"
87
, "integers"
98
, "lists"
@@ -12,7 +11,6 @@
1211
, "prelude"
1312
, "record"
1413
, "strings"
15-
, "type-equality"
1614
, "typelevel-prelude"
1715
]
1816
, packages = ./packages.dhall

src/TypedEnv.purs

+30-50
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ module TypedEnv
1010
, readValue
1111
, class ReadEnv
1212
, readEnv
13-
, class ReadEnvFields
14-
, readEnvFields
1513
) where
1614

1715
import Prelude
1816

19-
import Data.Bifunctor (lmap)
2017
import Data.Either (Either(..), note)
2118
import Data.Generic.Rep (class Generic)
2219
import Data.Int (fromString) as Int
@@ -30,19 +27,21 @@ import Data.Symbol (class IsSymbol, reflectSymbol)
3027
import Foreign.Object (Object, lookup)
3128
import Prim.Row (class Cons, class Lacks) as Row
3229
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
3532
import Type.Proxy (Proxy(..))
3633
import Type.RowList (class ListToRow)
3734

3835
-- | Gets a record of environment variables from a Node environment.
3936
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
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
4847
data 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.
133114
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
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

Comments
 (0)