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

add t.fromType #36

Closed
wants to merge 2 commits into from
Closed
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
40 changes: 37 additions & 3 deletions lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,19 @@ function t.match(pattern)
end
end

local optionalMap = setmetatable({}, { __mode = "k" })
local interfaceMap = setmetatable({}, { __mode = "k" })

--[[**
ensures value is either nil or passes check

@param check The check to use

@returns A function that will return true iff the condition is passed
**--]]
function t.optional(check)
function t.optional(check, default)
assert(t.callback(check))
return function(value)
local result = function(value)
if value == nil then
return true
end
Expand All @@ -655,6 +658,9 @@ function t.optional(check)
return false, string.format("(optional) %s", errMsg or "")
end
end
optionalMap[result] = default
interfaceMap[result] = check
return result
end

--[[**
Expand Down Expand Up @@ -864,7 +870,7 @@ do
**--]]
function t.interface(checkTable)
assert(checkInterface(checkTable))
return function(value)
local result = function(value)
local tableSuccess, tableErrMsg = t.table(value)
if tableSuccess == false then
return false, tableErrMsg or ""
Expand All @@ -878,6 +884,8 @@ do
end
return true
end
interfaceMap[result] = checkTable
return result
end

--[[**
Expand Down Expand Up @@ -1088,4 +1096,30 @@ do
end
end

local function getInterface(check)
local result = interfaceMap[check]
while result and not t.table(result) do
result = interfaceMap[result]
end
return result
end

function t.fromType(check, value)
assert(t.callback(check))
assert(check(value))

if value == nil then
return t.fromType(interfaceMap[check], optionalMap[check])
end

local int = getInterface(check)
if int then
for i in pairs(int) do
value[i] = t.fromType(int[i], value[i])
end
end

return value
end

return t
30 changes: 30 additions & 0 deletions lib/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,34 @@ return function()
assert(check(myNewEnum.OptionA))
assert(not (check(1010)))
end)

it("should support t.fromType", function()
local IObject = t.optional(t.interface({
foo = t.optional(t.string, "a"),
bar = t.optional(t.interface({
foo = t.optional(t.string, "b"),
bar = t.optional(t.interface({
foo = t.optional(t.string, "c"),
bar = t.optional(t.interface({
foo = t.optional(t.string, "d"),
bar = t.optional(t.boolean, false)
}), {})
}), {})
}), {})
}), {})

local obj1 = t.fromType(IObject, {})
expect(obj1.foo).to.equal("a")
expect(obj1.bar.foo).to.equal("b")
expect(obj1.bar.bar.foo).to.equal("c")
expect(obj1.bar.bar.bar.foo).to.equal("d")
expect(obj1.bar.bar.bar.bar).to.equal(false)

local obj2 = t.fromType(IObject)
expect(obj2.foo).to.equal("a")
expect(obj2.bar.foo).to.equal("b")
expect(obj2.bar.bar.foo).to.equal("c")
expect(obj2.bar.bar.bar.foo).to.equal("d")
expect(obj2.bar.bar.bar.bar).to.equal(false)
end)
end