Skip to content

Commit

Permalink
Add parameter with_names to to_csv (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gryumov committed Feb 24, 2024
1 parent 860d31e commit b0ca9f1
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

2 comments on commit b0ca9f1

@gryumov
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101598

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.0 -m "<description of version>" b0ca9f17ed456e76fc4ce6fdd977a16100331174
git push origin v2.0.0

Please sign in to comment.