Fixes to support a wider range of field kinds

- Rename api.checkvalue() to api.checkValue() and swap argument order for consistency
- Add kind argument to api.checkValue() to identify which segment is being tested
- Limit wildcard test in remove() to string values
- Return error message from field.new() instead of raising error internally
This commit is contained in:
Jason Perkins 2014-07-14 12:00:30 -04:00
parent 7158f623b9
commit 5614bfcd17
5 changed files with 26 additions and 16 deletions

View File

@ -291,8 +291,8 @@
-- --
function vstudio.archFromPlatform(platform) function vstudio.archFromPlatform(platform)
local system = premake.api.checkvalue(platform, premake.fields.system) local system = premake.api.checkValue(premake.fields.system, platform)
local arch = premake.api.checkvalue(platform, premake.fields.architecture) local arch = premake.api.checkValue(premake.fields.architecture, platform)
return architecture(system, arch) return architecture(system, arch)
end end

View File

@ -91,7 +91,11 @@
end end
-- add this new field to my master list -- add this new field to my master list
field = premake.field.new(field) field, err = premake.field.new(field)
if not field then
error(err)
end
-- Flag fields which contain filesystem paths. The context object will -- Flag fields which contain filesystem paths. The context object will
-- use this information when expanding tokens, to ensure that the paths -- use this information when expanding tokens, to ensure that the paths
@ -357,7 +361,7 @@
table.insert(removes, value) table.insert(removes, value)
else else
local value, err, additional = api.checkvalue(value, field) local value, err, additional = api.checkValue(field, value)
if err then if err then
error { msg=err } error { msg=err }
end end
@ -382,17 +386,21 @@
-- --
-- Check to see if a value is valid for a particular field. -- Check to see if a value is valid for a particular field.
-- --
-- @param value
-- The value to check.
-- @param field -- @param field
-- The field to check against. -- The field to check against.
-- @param value
-- The value to check.
-- @param kind
-- The kind of data currently being checked, corresponding to
-- one segment of the field's kind string (e.g. "string"). If
-- not set, defaults to "string".
-- @return -- @return
-- If the value is valid for this field, the canonical version -- If the value is valid for this field, the canonical version
-- of that value is returned. If the value is not valid two -- of that value is returned. If the value is not valid two
-- values are returned: nil, and an error message. -- values are returned: nil, and an error message.
-- --
function api.checkvalue(value, field) function api.checkValue(field, value, kind)
if not field.allowed then if not field.allowed then
return value return value
end end
@ -406,7 +414,7 @@
if not canonical then if not canonical then
if type(field.allowed) == "function" then if type(field.allowed) == "function" then
canonical = field.allowed(value) canonical = field.allowed(value, kind or "string")
else else
canonical = field.allowed[lowerValue] canonical = field.allowed[lowerValue]
end end
@ -754,7 +762,7 @@
if value ~= nil then if value ~= nil then
local err local err
value, err = api.checkvalue(value, field) value, err = api.checkValue(field, value)
if err then if err then
error { msg=err } error { msg=err }
end end

View File

@ -332,7 +332,9 @@
values = premake.field.remove(field, {}, values) values = premake.field.remove(field, {}, values)
for i, value in ipairs(values) do for i, value in ipairs(values) do
values[i] = path.wildcards(value):lower() if type(value) == "string" then
values[i] = path.wildcards(value):lower()
end
end end
-- add a list of removed values to the block -- add a list of removed values to the block

View File

@ -83,7 +83,7 @@
-- All fields must have a valid store() function -- All fields must have a valid store() function
if not field.accessor(f, "store") then if not field.accessor(f, "store") then
error("invalid field kind '" .. f._kind .. "'") return nil, "invalid field kind '" .. f._kind .. "'"
end end
field._list[f.name] = f field._list[f.name] = f
@ -186,8 +186,8 @@
end end
-- Split off the first piece from the rest of the kind. If the -- Split off the first piece from the rest of the kind. If the
-- incoming kind is "list:key:string", thisKind will "list" and -- incoming kind is "list:key:string", thisKind will be "list"
-- nextKind will be "key:string". -- and nextKind will be "key:string".
local thisKind = kind:match('(.-):') or kind local thisKind = kind:match('(.-):') or kind
local nextKind = kind:sub(#thisKind + 2) local nextKind = kind:sub(#thisKind + 2)
@ -197,7 +197,7 @@
local functions = field._kinds[thisKind] local functions = field._kinds[thisKind]
if not functions then if not functions then
error("Invalid field kind '" .. thisKind .. "'") return nil, "Invalid field kind '" .. thisKind .. "'"
end end
local processor = functions[method] local processor = functions[method]

View File

@ -441,8 +441,8 @@
local architecture = nil local architecture = nil
if platform then if platform then
system = premake.api.checkvalue(platform, premake.fields.system) or system system = premake.api.checkValue(premake.fields.system, platform) or system
architecture = premake.api.checkvalue(platform, premake.fields.architecture) or architecture architecture = premake.api.checkValue(premake.fields.architecture, platform) or architecture
end end
-- Wrap the projects's configuration set (which contains all of the information -- Wrap the projects's configuration set (which contains all of the information