Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CSV structure functions with a simpler method #18

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "DOMO"
uuid = "72b704fe-efc7-41fd-b723-a3ca9fa252d1"
authors = ["Michael Johnson"]
version = "0.1.3"
version = "0.1.4"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Expand Down
38 changes: 10 additions & 28 deletions src/schema.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using DataFrames: DataFrame
using CSV
"""
match_domo_types(type)

Expand Down Expand Up @@ -44,37 +46,17 @@ function create_dataset_schema(df, name, description)
return JSON3.write(schema)
end

function create_csv_string(row, col_num)
csv_string = string(ifelse(ismissing(row[col_num]), "", row[col_num]))

if occursin(",", csv_string)
csv_string = "\"" * csv_string * "\""
end

return csv_string
end

"""
create_csv_structure(df)
dataframe_to_csv

This function creates a CSV, in the form of a single string, from a DataFrame to send to Domo.
Convert a Julia DataFrame to a CSV string for the API.
"""
function create_csv_structure(df)
csv_data = ""

for row in eachrow(df), col_num in 1:ncol(df)
if col_num < ncol(df) && rownumber(row) < nrow(df)
csv_data = csv_data * create_csv_string(row, col_num) * ","
elseif col_num == ncol(df) && rownumber(row) < nrow(df)
csv_data = csv_data * create_csv_string(row, col_num) * "\n"
elseif col_num < ncol(df) && rownumber(row) == nrow(df)
csv_data = csv_data * create_csv_string(row, col_num) * ","
elseif col_num == ncol(df) && rownumber(row) == nrow(df)
csv_data = csv_data * create_csv_string(row, col_num) * "\n"
end
end

return csv_data
function dataframe_to_csv(df::DataFrame)
io = IOBuffer()
io_file = CSV.write(io, df; header = false)
csv_string = String(take!(io_file))
close(io)
return csv_string
end

function push_schema_to_domo(dataset_schema)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ include("test-sets/schema-tests.jl")
@test match_domo_types(types[type]) == expected_types[type]
end
# test whether behavior of csv creator is valid
@test create_csv_structure(schema_test_mathematicians_dataset) == test_csv_string_math
@test create_csv_structure(null_schema_test_df) == test_csv_string_crows
@test dataframe_to_csv(schema_test_mathematicians_dataset) == test_csv_string_math
@test dataframe_to_csv(null_schema_test_df) == test_csv_string_crows
end;
2 changes: 1 addition & 1 deletion test/test-sets/schema-tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DOMO: match_domo_types, create_dataset_schema, create_csv_structure
import DOMO: match_domo_types, create_dataset_schema, dataframe_to_csv
using JSON3
import DataFrames: DataFrame
using Dates
Expand Down
Loading