Skip to content

Commit 2f474af

Browse files
authored
fromEnv now accepts a record type instead of a row type (#10)
1 parent 13266da commit 2f474af

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ environment variable names from which their values are sourced:
3939

4040
```purescript
4141
type Config =
42-
( "GREETING" :: String
42+
{ "GREETING" :: String
4343
, "COUNT" :: Int
44-
)
44+
}
4545
```
4646

4747
The `fromEnv` function can now convert the environment `Object String` to a typed record with no need for explicit

examples/App.purs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import TypedEnv (fromEnv) as TypedEnv
1515
import TypedEnv (printEnvError)
1616

1717
type Config =
18-
( "ALERT_EMAIL" :: String
18+
{ "ALERT_EMAIL" :: String
1919
, "ALERT_SUBJECT" :: String
20-
)
20+
}
2121

22-
newtype AppM a = AppM (ReaderT { | Config } Effect a)
22+
newtype AppM a = AppM (ReaderT Config Effect a)
2323

24-
runAppM :: { | Config } -> AppM ~> Effect
24+
runAppM :: Config -> AppM ~> Effect
2525
runAppM env (AppM m) = runReaderT m env
2626

2727
derive newtype instance functorAppM :: Functor AppM
@@ -31,7 +31,7 @@ derive newtype instance bindAppM :: Bind AppM
3131
derive newtype instance monadAppM :: Monad AppM
3232
derive newtype instance monadEffectAppM :: MonadEffect AppM
3333

34-
instance monadAskAppM :: TypeEquals e { | Config } => MonadAsk e AppM where
34+
instance monadAskAppM :: TypeEquals e Config => MonadAsk e AppM where
3535
ask = AppM $ asks from
3636

3737
main :: Effect Unit

examples/Basic.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import TypedEnv (fromEnv) as TypedEnv
1212
import TypedEnv (printEnvError)
1313

1414
type Environment =
15-
( "GREETING" :: String
15+
{ "GREETING" :: String
1616
, "COUNT" :: Int
17-
)
17+
}
1818

1919
main :: Effect Unit
2020
main = do

examples/CustomType.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ instance parseValuePort :: ParseValue Port where
2121
parseValue = map Port <<< find (_ <= 65535) <<< Int.fromString
2222

2323
type Settings =
24-
( "HOST" :: String
24+
{ "HOST" :: String
2525
, "PORT" :: Port
26-
)
26+
}
2727

2828
main :: Effect Unit
2929
main = do

examples/Optional.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Type.Proxy (Proxy(..))
1111
import TypedEnv (fromEnv) as TypedEnv
1212
import TypedEnv (printEnvError)
1313

14-
type Settings = ("USERNAME" :: Maybe String)
14+
type Settings = { "USERNAME" :: Maybe String }
1515

1616
main :: Effect Unit
1717
main = do

examples/Reader.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import TypedEnv (fromEnv) as TypedEnv
1414
import TypedEnv (printEnvError)
1515

1616
type Config =
17-
( "USERNAME" :: Maybe String
17+
{ "USERNAME" :: Maybe String
1818
, "REPEAT" :: Maybe Int
19-
)
19+
}
2020

2121
main :: Effect Unit
2222
main = do

src/TypedEnv.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fromEnv
3737
:: forall r rl
3838
. RowToList r rl
3939
=> ReadEnv rl () r
40-
=> Proxy r
40+
=> Proxy (Record r)
4141
-> Object String
4242
-> Either (List EnvError) (Record r)
4343
fromEnv _ env = RB.buildFromScratch <$> readEnv (Proxy :: _ rl) (Proxy :: _ ())

test/Main.purs

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,38 @@ main = launchAff_ $ runSpec [ consoleReporter ] $
2525
env = FO.fromHomogeneous
2626
{ "A": "a value", "B": "b value", "C": "c value" }
2727
expected = Right { "B": "b value", "C": "c value" }
28-
actual = fromEnv (Proxy :: _ ("B" :: String, "C" :: String))
28+
actual = fromEnv (Proxy :: _ { "B" :: String, "C" :: String })
2929
env
3030
actual `shouldEqual` expected
3131

3232
it "indicates when a lookup has failed" do
3333
let
3434
env = FO.fromHomogeneous { "GREETING": "Hello" }
3535
expected = Left (pure $ EnvLookupError "MESSAGE")
36-
actual = fromEnv (Proxy :: _ ("MESSAGE" :: String)) env
36+
actual = fromEnv (Proxy :: _ { "MESSAGE" :: String }) env
3737
actual `shouldEqual` expected
3838

3939
it "indicates when parsing a value has failed" do
4040
let
4141
env = FO.fromHomogeneous { "DEBUG": "50" }
4242
expected = Left (pure $ EnvParseError "DEBUG")
43-
actual = fromEnv (Proxy :: _ ("DEBUG" :: Boolean)) env
43+
actual = fromEnv (Proxy :: _ { "DEBUG" :: Boolean }) env
4444
actual `shouldEqual` expected
4545

4646
it "indicates when parsing multiple values has failed" do
4747
let
4848
env = FO.fromHomogeneous { "A": "err" }
4949
expected = Left (EnvParseError "A" : EnvLookupError "B" : Nil)
5050
actual = fromEnv
51-
(Proxy :: _ ("A" :: Int, "B" :: String))
51+
(Proxy :: _ { "A" :: Int, "B" :: String })
5252
env
5353
actual `shouldEqual` expected
5454

5555
it "parses boolean values" do
5656
traverse_
5757
( \({ given, expected }) ->
5858
shouldEqual
59-
( fromEnv (Proxy :: _ ("A" :: Boolean))
59+
( fromEnv (Proxy :: _ { "A" :: Boolean })
6060
(FO.fromHomogeneous { "A": given })
6161
)
6262
(Right { "A": expected })
@@ -71,33 +71,33 @@ main = launchAff_ $ runSpec [ consoleReporter ] $
7171
let
7272
env = FO.fromHomogeneous { "VALUE": "123" }
7373
expected = Right { "VALUE": 123 }
74-
actual = fromEnv (Proxy :: _ ("VALUE" :: Int)) env
74+
actual = fromEnv (Proxy :: _ { "VALUE" :: Int }) env
7575
actual `shouldEqual` expected
7676

7777
it "parses character values" do
7878
let
7979
env = FO.fromHomogeneous { "VALUE": "x" }
8080
expected = Right { "VALUE": 'x' }
81-
actual = fromEnv (Proxy :: _ ("VALUE" :: Char)) env
81+
actual = fromEnv (Proxy :: _ { "VALUE" :: Char }) env
8282
actual `shouldEqual` expected
8383

8484
it "parses number values" do
8585
let
8686
env = FO.fromHomogeneous { "VALUE": "123.456" }
8787
expected = Right { "VALUE": 123.456 }
88-
actual = fromEnv (Proxy :: _ ("VALUE" :: Number)) env
88+
actual = fromEnv (Proxy :: _ { "VALUE" :: Number }) env
8989
actual `shouldEqual` expected
9090

9191
it "parses optional values" do
9292
let
9393
env = FO.fromHomogeneous { "VALUE": "Hello" }
9494
expected = Right { "VALUE": Just "Hello" }
95-
actual = fromEnv (Proxy :: _ ("VALUE" :: Maybe String)) env
95+
actual = fromEnv (Proxy :: _ { "VALUE" :: Maybe String }) env
9696
actual `shouldEqual` expected
9797

9898
it "allows optional values to be absent" do
9999
let
100100
expected = Right { "VALUE": Nothing }
101-
actual = fromEnv (Proxy :: _ ("VALUE" :: Maybe String))
101+
actual = fromEnv (Proxy :: _ { "VALUE" :: Maybe String })
102102
FO.empty
103103
actual `shouldEqual` expected

0 commit comments

Comments
 (0)