From b0ca9f17ed456e76fc4ce6fdd977a16100331174 Mon Sep 17 00:00:00 2001 From: stanislav Date: Sat, 24 Feb 2024 23:24:01 +0300 Subject: [PATCH] Add parameter `with_names` to `to_csv` (#17) --- CHANGELOG.md | 17 +++++++++-------- src/Ser/SerCsv.jl | 11 +++++++---- test/Ser/SerCsv.jl | 9 ++++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dc1572..34fcdcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,16 +2,17 @@ The latest version of this file can be found at the master branch of the [Serde.jl repository](https://bhftbootcamp.github.io/Serde.jl). -## 2.0.0 (22/02/2024) ([merge request]()) +## 2.0.0 (22/02/2024) -### Added (5 changes) +### Added -- Function `Serde.deser_xml` for deserializing XML. -- Function `Serde.parse_xml` for parsing XML. -- Function `Serde.to_yaml` for converting to YAML. -- Function `Serde.parse_yaml` for parsing YAML. -- Function `Serde.deser_yaml` for deserializing YAML. +- Function `Serde.deser_xml` for deserializing XML (#14). +- Function `Serde.parse_xml` for parsing XML (#14). +- Function `Serde.to_yaml` for converting to YAML (#14). +- Function `Serde.parse_yaml` for parsing YAML (#14). +- Function `Serde.deser_yaml` for deserializing YAML (#14). +- Parameter `with_names` to `to_csv` function to toggle inclusion of headers in CSV output (#17). -### Changed (1 change) +### Changed - Refactored tests to improve maintainability and performance. diff --git a/src/Ser/SerCsv.jl b/src/Ser/SerCsv.jl index 828b21e..0d682dd 100644 --- a/src/Ser/SerCsv.jl +++ b/src/Ser/SerCsv.jl @@ -26,7 +26,7 @@ In case of nested `data`, names of resulting headers will be concatenate by "_" ## Keyword arguments - `delimiter::String = ","`: The delimiter that will be used in the returned csv string. - `headers::Vector{String} = String[]`: Specifies which column headers will be used and in what order. - +- `with_names::Bool = true`: Determines if column headers are included in the CSV output (true to include, false to exclude). ## Examples Converting a vector of regular dictionaries with fixed headers order. @@ -88,19 +88,22 @@ function to_csv( data::Vector{T}; delimiter::String = ",", headers::Vector{String} = String[], + with_names::Bool = true, )::String where {T} cols = Set{String}() - vals = Vector{Dict{String,Any}}(undef, length(data) + 1) + vals = Vector{Dict{String,Any}}(undef, length(data) + with_names) + for (index, item) in enumerate(data) val = to_flatten(item) push!(cols, keys(val)...) - vals[index+1] = val + vals[index + with_names] = val end - vals[1] = Dict{String,String}(cols .=> string.(cols)) + with_names && (vals[1] = Dict{String,String}(cols .=> string.(cols))) t_cols = isempty(headers) ? sort([cols...]) : headers l_cols = t_cols[end] buf = IOBuffer() + for csv_item in vals for col in t_cols val = get(csv_item, col, nothing) diff --git a/test/Ser/SerCsv.jl b/test/Ser/SerCsv.jl index 18c43ea..fbffef9 100644 --- a/test/Ser/SerCsv.jl +++ b/test/Ser/SerCsv.jl @@ -105,7 +105,14 @@ 10,20,baz,foo 10,20,, """ - @test Serde.to_csv(exp_obj, headers = ["a", "B", "C_cbaz", "C_cfoo"]) |> strip == + @test Serde.to_csv(exp_obj, headers = ["a", "B", "C_cbaz", "C_cfoo"], with_names = true) |> strip == + exp_str |> strip + + exp_str = """ + 10,20,baz,foo + 10,20,, + """ + @test Serde.to_csv(exp_obj, headers = ["a", "B", "C_cbaz", "C_cfoo"], with_names = false) |> strip == exp_str |> strip end end