Skip to content

Commit

Permalink
Add parameter with_names to to_csv (#17) (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gryumov authored Feb 24, 2024
1 parent 860d31e commit 9ddb8bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 7 additions & 4 deletions src/Ser/SerCsv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion test/Ser/SerCsv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9ddb8bc

Please sign in to comment.