Skip to content

Commit a4d4c85

Browse files
author
BirdeeHub
committed
fix(decode_to_defaults): == to !=
#26
1 parent aebf820 commit a4d4c85

4 files changed

Lines changed: 43 additions & 12 deletions

File tree

lua/tomlua/meta.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ error("Cannot import a meta module")
5959
---@class Tomlua.main
6060
---@field opts TomluaOptions
6161
---@field types table<TomlType, TomlTypeNum>
62-
---@field decode fun(str:string):(any, string?): table?, string? -- returns result?, err?
62+
---@field decode fun(str:string, defaults?:table):(any, string?): table?, string? -- returns result?, err?
6363
---@field encode fun(val:any):(string, string?): string?, string? -- returns result?, err?
6464
---@field type fun(val:any):TomlType
6565
---@field type_of fun(val:any):TomlTypeNum

src/decode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static bool recursive_lua_nav(
105105
return err_push_keys(L, err, keys_start, keys_end);
106106
}
107107
lua_Integer len = lua_tointeger(L, defidx);
108-
if (had_defaults && deftype == LUA_TNUMBER) {
108+
if (had_defaults && deftype != LUA_TNUMBER) {
109109
len = lua_arraylen(L, validx);
110110
}
111111
lua_pop(L, 1);

tests/decode_tests.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ return function(define, test_dir)
2121
it(function() assert(err == nil, err) end, "Should not error on valid TOML")
2222
end)
2323

24+
define("reading headings to default table", function()
25+
local appendtoml = [=[
26+
[example]
27+
test = "this is a test"
28+
[[example2]]
29+
test = "this is a test"
30+
]=]
31+
local defaults = {
32+
example = {
33+
if_defined = "as a heading",
34+
the_values_here = "will be recursively updated",
35+
},
36+
example2 = {
37+
{ test = "if defined as a heading", },
38+
{ test = "it will append", }
39+
},
40+
}
41+
local data, err = tomlua_default.decode(appendtoml, defaults)
42+
it(err == nil, "Should not error")
43+
it(#data.example2 == 3, "Should be longer")
44+
it(data.example.test ~= nil, "Should have been added")
45+
it(data.example.if_defined ~= nil, "Should not have been removed")
46+
end)
47+
2448
define("decode basic integer", function()
2549
local data, err = tomlua_default.decode("key = 123")
2650
it(err == nil, "Should not error")

tests/scratch.lua

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,23 @@ print(tomlua.type(val.b))
182182
print(tomlua.encode(val))
183183

184184
do
185-
local toml_edit = require("toml_edit")
186-
local errtoml = [=[
187-
# test = { a.TEST = [ 1, 2, 3 ], a.HA = "hmmmm" }
188-
# test2 = { a = [ 1, 2, 3 ], a.HA = "hmmmm" }
185+
local appendtoml = [=[
186+
[example]
187+
test = "this is a test"
188+
[[example2]]
189+
test = "this is a test"
189190
]=]
191+
local defaults = {
192+
example = {
193+
if_defined = "as a heading",
194+
the_values_here = "will be recursively updated",
195+
},
196+
example2 = {
197+
{ test = "if defined as a heading", },
198+
{ test = "it will append", }
199+
},
200+
}
190201

191-
local d, e = tomlua.decode(errtoml)
192-
local ok, data = pcall(toml_edit.parse_as_tbl, errtoml)
193-
print(ok, require("inspect")(data))
194-
-- print(require("inspect")(d))
195-
print(errtoml)
196-
print(tomlua.encode(d), e)
202+
local d, e = tomlua.decode(appendtoml, defaults)
203+
print(require('inspect')(d), e)
197204
end

0 commit comments

Comments
 (0)