From 5d20da4507d6d82f1691844c5e45b6fca4b44fa4 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Mon, 11 Nov 2024 23:11:23 +0000 Subject: [PATCH] Rendering CSV --- universal/gleam.toml | 2 +- universal/manifest.toml | 5 ++- universal/test/formats/rendering_csv.gleam | 45 +++++++++++++++++----- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/universal/gleam.toml b/universal/gleam.toml index bcdd378..22adb0f 100644 --- a/universal/gleam.toml +++ b/universal/gleam.toml @@ -7,7 +7,7 @@ envoy = ">= 1.0.2 and < 2.0.0" gleam_crypto = ">= 1.3.0 and < 2.0.0" gleam_json = ">= 1.0.1 and < 2.0.0" gleam_stdlib = ">= 0.34.0 and < 2.0.0" -gsv = ">= 2.0.0 and < 3.0.0" +gsv = ">= 3.0.0 and < 4.0.0" lustre = ">= 4.4.4 and < 5.0.0" simplifile = ">= 2.1.0 and < 3.0.0" tom = ">= 1.1.0 and < 2.0.0" diff --git a/universal/manifest.toml b/universal/manifest.toml index 832e5d2..1e4cfda 100644 --- a/universal/manifest.toml +++ b/universal/manifest.toml @@ -9,8 +9,9 @@ packages = [ { name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, { name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" }, { name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" }, + { name = "glearray", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "B99767A9BC63EF9CC8809F66C7276042E5EFEACAA5B25188B552D3691B91AC6D" }, { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, - { name = "gsv", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gsv", source = "hex", outer_checksum = "DBC35126CB3621D305EC2412C9D43F90D496692BB58BA3BEDF70C1AD34143587" }, + { name = "gsv", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glearray"], otp_app = "gsv", source = "hex", outer_checksum = "2FD89349BB9E3A2D9060C0E9F4B6AC70680088E7B0629F20F37653E93D400EF2" }, { 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" }, { name = "simplifile", version = "2.1.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "BDD04F5D31D6D34E2EDFAEF0B68A6297AEC939888C3BFCE61133DE13857F6DA2" }, { 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" } gleam_json = { version = ">= 1.0.1 and < 2.0.0" } gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } -gsv = { version = ">= 2.0.0 and < 3.0.0" } +gsv = { version = ">= 3.0.0 and < 4.0.0" } lustre = { version = ">= 4.4.4 and < 5.0.0" } simplifile = { version = ">= 2.1.0 and < 3.0.0" } tom = { version = ">= 1.1.0 and < 2.0.0" } diff --git a/universal/test/formats/rendering_csv.gleam b/universal/test/formats/rendering_csv.gleam index 6472ec2..e718169 100644 --- a/universal/test/formats/rendering_csv.gleam +++ b/universal/test/formats/rendering_csv.gleam @@ -11,20 +11,45 @@ import gleeunit/should import gsv pub fn main_test() { - let data = [ + // If you have your data as a list of dicts then you can use the + // `from_dicts` function to create a CSV. + // The headers will be taken automatically from the keys of all of the dicts. + [ dict.from_list([#("name", "Lucy"), #("score", "100"), #("colour", "Pink")]), - dict.from_list([ - #("name", "Isaac"), - #("youtube", "@IsaacHarrisHolt"), - #("score", "99"), - ]), + dict.from_list([#("name", "Louis"), #("youtube", "@lpil"), #("score", "99")]), ] - - gsv.from_dicts(data, ",", gsv.Unix) + |> gsv.from_dicts(",", gsv.Unix) |> should.equal( "colour,name,score,youtube Pink,Lucy,100, -,Isaac,99,@IsaacHarrisHolt -", +,Louis,99,@lpil", + ) + + // If you want to have control over the order of the columns than you can use + // the `from_lists` function. + [ + ["name", "score", "youtube", "colour"], + ["Lucy", "100", "", "Pink"], + ["Louis", "99", "@lpil", ""], + ] + |> gsv.from_lists(",", gsv.Unix) + |> should.equal( + "name,score,youtube,colour +Lucy,100,,Pink +Louis,99,@lpil,", + ) + + // You can specify a different line ending and separator with either + // function. Here a `;` is used to render TSV. + [ + ["name", "score", "youtube", "colour"], + ["Lucy", "100", "", "Pink"], + ["Louis", "99", "@lpil", ""], + ] + |> gsv.from_lists(";", gsv.Unix) + |> should.equal( + "name;score;youtube;colour +Lucy;100;;Pink +Louis;99;@lpil;", ) }