Skip to content
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,25 @@ file:close()
local output = ftcsv.encode(everyUser, {encodeNilAs=0}) -- for setting it to 0
```

- `allowMissingKeys`

If set to a non-`nil` value, this option allows encoding data sets that are entirely missing a field that was specified in `fieldsToKeep`. Otherwise, ftcsv would raise an error.

```lua
local data = {
{a = 1, b = 2, c = 3},
{a = 10, b = 20},
{a = 100, c = 200},
}
ftcsv.encode(data, {fieldsToKeep = {"a", "b", "c", "d"}}) --> [throws an error]
ftcsv.encode(data, {fieldsToKeep = {"a", "b", "c", "d"}, allowMissingKeys = true})
--> [[
--> "a","b","c","d"
--> "1","2","3","nil"
--> "10","20","nil","nil"
--> "100","nil","200","nil"
--> ]]
```

## Error Handling
ftcsv returns a litany of errors when passed a bad csv file or incorrect parameters. You can find a more detailed explanation of the more cryptic errors in [ERRORS.md](ERRORS.md)
Expand Down
4 changes: 3 additions & 1 deletion ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,9 @@ local function initializeGenerator(inputTable, delimiter, options)
if headers == nil then
headers = extractHeadersFromTable(inputTable)
end
validateHeaders(headers, inputTable)
if options and options.allowMissingKeys == nil then
validateHeaders(headers, inputTable)
end

local escapedHeaders = escapeHeadersForOutput(headers, delimiter, options)
local output = initializeOutputWithEscapedHeaders(escapedHeaders, delimiter, options)
Expand Down
4 changes: 4 additions & 0 deletions spec/csvs/missing_keys.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a,b,c
1,2,3
10,20,
100,,300
22 changes: 22 additions & 0 deletions spec/json/missing_keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"a": "1",
"b": "2",
"c": "3",
"d": "nil"
},
{
"a": "10",
"b": "20",
"c": "nil",
"d": "nil"

},
{
"a": "100",
"b": "nil",
"c": "300",
"d": "nil"

}
]
32 changes: 32 additions & 0 deletions spec/parse_encode_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,35 @@ describe("csv encode without quotes", function()
end)
end
end)

--[[ This breaks simple_crlf.
describe("csv encode with missing keys", function()
for _, value in ipairs(files) do
it("should handle " .. value, function()
local jsonFile = loadFile("spec/json/" .. value .. ".json")
local jsonDecode = cjson.decode(jsonFile)
local reEncoded = ftcsv.parse(ftcsv.encode(
jsonDecode, ",", {
fieldsToKeep = {"a", "b", "c", "d"},
allowMissingKeys = true,
}
), ",", {loadFromString=true})
assert.are.same(jsonDecode, reEncoded)
end)
end
end)
--]]

describe("csv encode with missing keys", function()
it("should handle missing_keys", function()
local jsonFile = loadFile("spec/json/missing_keys.json")
local jsonDecode = cjson.decode(jsonFile)
local reEncoded = ftcsv.parse(ftcsv.encode(
jsonDecode, ",", {
fieldsToKeep = {"a", "b", "c", "d"},
allowMissingKeys = true,
}
), ",", {loadFromString=true})
assert.are.same(jsonDecode, reEncoded)
end)
end)