Skip to content

Commit 5d20da4

Browse files
committed
Rendering CSV
1 parent f64a0ff commit 5d20da4

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

universal/gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ envoy = ">= 1.0.2 and < 2.0.0"
77
gleam_crypto = ">= 1.3.0 and < 2.0.0"
88
gleam_json = ">= 1.0.1 and < 2.0.0"
99
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
10-
gsv = ">= 2.0.0 and < 3.0.0"
10+
gsv = ">= 3.0.0 and < 4.0.0"
1111
lustre = ">= 4.4.4 and < 5.0.0"
1212
simplifile = ">= 2.1.0 and < 3.0.0"
1313
tom = ">= 1.1.0 and < 2.0.0"

universal/manifest.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ packages = [
99
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
1010
{ name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" },
1111
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
12+
{ name = "glearray", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "B99767A9BC63EF9CC8809F66C7276042E5EFEACAA5B25188B552D3691B91AC6D" },
1213
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
13-
{ name = "gsv", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gsv", source = "hex", outer_checksum = "DBC35126CB3621D305EC2412C9D43F90D496692BB58BA3BEDF70C1AD34143587" },
14+
{ name = "gsv", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glearray"], otp_app = "gsv", source = "hex", outer_checksum = "2FD89349BB9E3A2D9060C0E9F4B6AC70680088E7B0629F20F37653E93D400EF2" },
1415
{ name = "lustre", version = "4.4.4", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "35A8597B3054AFAF734A54979B7C92D8AB43273B843717F854CF5A05CF2BC00E" },
1516
{ name = "simplifile", version = "2.1.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "BDD04F5D31D6D34E2EDFAEF0B68A6297AEC939888C3BFCE61133DE13857F6DA2" },
1617
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
@@ -24,7 +25,7 @@ gleam_crypto = { version = ">= 1.3.0 and < 2.0.0" }
2425
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
2526
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
2627
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
27-
gsv = { version = ">= 2.0.0 and < 3.0.0" }
28+
gsv = { version = ">= 3.0.0 and < 4.0.0" }
2829
lustre = { version = ">= 4.4.4 and < 5.0.0" }
2930
simplifile = { version = ">= 2.1.0 and < 3.0.0" }
3031
tom = { version = ">= 1.1.0 and < 2.0.0" }

universal/test/formats/rendering_csv.gleam

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,45 @@ import gleeunit/should
1111
import gsv
1212

1313
pub fn main_test() {
14-
let data = [
14+
// If you have your data as a list of dicts then you can use the
15+
// `from_dicts` function to create a CSV.
16+
// The headers will be taken automatically from the keys of all of the dicts.
17+
[
1518
dict.from_list([#("name", "Lucy"), #("score", "100"), #("colour", "Pink")]),
16-
dict.from_list([
17-
#("name", "Isaac"),
18-
#("youtube", "@IsaacHarrisHolt"),
19-
#("score", "99"),
20-
]),
19+
dict.from_list([#("name", "Louis"), #("youtube", "@lpil"), #("score", "99")]),
2120
]
22-
23-
gsv.from_dicts(data, ",", gsv.Unix)
21+
|> gsv.from_dicts(",", gsv.Unix)
2422
|> should.equal(
2523
"colour,name,score,youtube
2624
Pink,Lucy,100,
27-
,Isaac,99,@IsaacHarrisHolt
28-
",
25+
,Louis,99,@lpil",
26+
)
27+
28+
// If you want to have control over the order of the columns than you can use
29+
// the `from_lists` function.
30+
[
31+
["name", "score", "youtube", "colour"],
32+
["Lucy", "100", "", "Pink"],
33+
["Louis", "99", "@lpil", ""],
34+
]
35+
|> gsv.from_lists(",", gsv.Unix)
36+
|> should.equal(
37+
"name,score,youtube,colour
38+
Lucy,100,,Pink
39+
Louis,99,@lpil,",
40+
)
41+
42+
// You can specify a different line ending and separator with either
43+
// function. Here a `;` is used to render TSV.
44+
[
45+
["name", "score", "youtube", "colour"],
46+
["Lucy", "100", "", "Pink"],
47+
["Louis", "99", "@lpil", ""],
48+
]
49+
|> gsv.from_lists(";", gsv.Unix)
50+
|> should.equal(
51+
"name;score;youtube;colour
52+
Lucy;100;;Pink
53+
Louis;99;@lpil;",
2954
)
3055
}

0 commit comments

Comments
 (0)