-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Cleanup public API, move parse to internal
- Loading branch information
1 parent
5e0fc75
commit 21b9544
Showing
6 changed files
with
235 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import gleam/dynamic | ||
import gleam/float | ||
import gleam/int | ||
import gleam/list | ||
import gleam/string | ||
|
||
pub fn bool( | ||
key: String, | ||
value: String, | ||
) -> Result(#(String, dynamic.Dynamic), Nil) { | ||
let resolution = | ||
["true", "yes", "1"] |> list.contains(string.lowercase(value)) | ||
|
||
Ok(#(key, dynamic.from(resolution))) | ||
} | ||
|
||
pub fn float( | ||
key: String, | ||
value: String, | ||
) -> Result(#(String, dynamic.Dynamic), Nil) { | ||
case float.parse(value) { | ||
Ok(resolution) -> Ok(#(key, dynamic.from(resolution))) | ||
Error(_) -> Error(Nil) | ||
} | ||
} | ||
|
||
pub fn int( | ||
key: String, | ||
value: String, | ||
) -> Result(#(String, dynamic.Dynamic), Nil) { | ||
case int.parse(value) { | ||
Ok(resolution) -> Ok(#(key, dynamic.from(resolution))) | ||
Error(_) -> Error(Nil) | ||
} | ||
} | ||
|
||
pub fn string( | ||
key: String, | ||
value: String, | ||
) -> Result(#(String, dynamic.Dynamic), Nil) { | ||
Ok(#(key, dynamic.from(value))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import gleam/dynamic | ||
import gleeunit | ||
import gleeunit/should | ||
import internal/parse | ||
|
||
pub fn main() { | ||
gleeunit.main() | ||
} | ||
|
||
const key = "GLENV_TEST_VAR" | ||
|
||
pub fn parse_bool_test() { | ||
parse.bool(key, "yes") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "YES") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "yEs") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "true") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "TRUE") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "trUE") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "1") | ||
|> should.equal(Ok(#(key, dynamic.from(True)))) | ||
|
||
parse.bool(key, "no") | ||
|> should.equal(Ok(#(key, dynamic.from(False)))) | ||
|
||
parse.bool(key, "false") | ||
|> should.equal(Ok(#(key, dynamic.from(False)))) | ||
|
||
parse.bool(key, "0") | ||
|> should.equal(Ok(#(key, dynamic.from(False)))) | ||
|
||
parse.bool(key, "anythingelse") | ||
|> should.equal(Ok(#(key, dynamic.from(False)))) | ||
} | ||
|
||
pub fn parse_float_test() { | ||
parse.float(key, "1.0") | ||
|> should.equal(Ok(#(key, dynamic.from(1.0)))) | ||
|
||
parse.float(key, "1.0001") | ||
|> should.equal(Ok(#(key, dynamic.from(1.0001)))) | ||
|
||
parse.float(key, "1.000") | ||
|> should.equal(Ok(#(key, dynamic.from(1.0)))) | ||
|
||
parse.float(key, "1") | ||
|> should.be_error | ||
|
||
parse.float(key, "abc") | ||
|> should.be_error | ||
} | ||
|
||
pub fn parse_int_test() { | ||
parse.int(key, "1") | ||
|> should.equal(Ok(#(key, dynamic.from(1)))) | ||
|
||
parse.int(key, "2000000") | ||
|> should.equal(Ok(#(key, dynamic.from(2_000_000)))) | ||
|
||
parse.int(key, "1.0") | ||
|> should.be_error | ||
|
||
parse.int(key, "abc") | ||
|> should.be_error | ||
|
||
parse.int(key, "2_000") | ||
|> should.be_error | ||
} | ||
|
||
pub fn parse_string_test() { | ||
parse.string(key, "1") | ||
|> should.equal(Ok(#(key, dynamic.from("1")))) | ||
|
||
parse.string(key, "2000000") | ||
|> should.equal(Ok(#(key, dynamic.from("2000000")))) | ||
|
||
parse.string(key, "1.0") | ||
|> should.equal(Ok(#(key, dynamic.from("1.0")))) | ||
|
||
parse.string(key, "abc") | ||
|> should.equal(Ok(#(key, dynamic.from("abc")))) | ||
|
||
parse.string(key, "2_000") | ||
|> should.equal(Ok(#(key, dynamic.from("2_000")))) | ||
} |
Oops, something went wrong.