diff options
| author | William Boman <william@redwill.se> | 2022-12-26 17:04:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-26 17:04:53 +0100 |
| commit | 0d14400ab3755f27041f70566d6b78f48c82c54c (patch) | |
| tree | bd18e1f1c5a02cf359027b72c1a10934b21e9c23 /lua/mason-core | |
| parent | feat(result): add .try() interface (#804) (diff) | |
| download | mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar.gz mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar.bz2 mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar.lz mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar.xz mason-0d14400ab3755f27041f70566d6b78f48c82c54c.tar.zst mason-0d14400ab3755f27041f70566d6b78f48c82c54c.zip | |
feat(expr): add tbl_interpolate() (#805)
Diffstat (limited to 'lua/mason-core')
| -rw-r--r-- | lua/mason-core/installer/registry/expr.lua | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/lua/mason-core/installer/registry/expr.lua b/lua/mason-core/installer/registry/expr.lua index 17307ac2..539b557c 100644 --- a/lua/mason-core/installer/registry/expr.lua +++ b/lua/mason-core/installer/registry/expr.lua @@ -49,24 +49,21 @@ function M.eval(str, ctx) setfenv( assert( loadstring("return " .. components.value_expr), - ("Failed to parse value :%q."):format(components.value_expr) + ("Failed to parse value: %q"):format(components.value_expr) ), ctx )(), - ("Value is nil: %q."):format(components.value_expr) + ("Value is nil: %q"):format(components.value_expr) ) return _.reduce( _.apply_to, value, _.map(function(filter_expr) local filter = setfenv( - assert( - loadstring("return " .. filter_expr), - ("Failed to parse filter: %q."):format(filter_expr) - ), + assert(loadstring("return " .. filter_expr), ("Failed to parse filter: %q"):format(filter_expr)), ctx )() - assert(type(filter) == "function", ("Invalid filter expression: %q."):format(filter_expr)) + assert(type(filter) == "function", ("Invalid filter expression: %q"):format(filter_expr)) return filter end, components.filters) ) @@ -74,4 +71,24 @@ function M.eval(str, ctx) end) end +---@generic T : table +---@param tbl T +---@param ctx table +---@return T +function M.tbl_interpolate(tbl, ctx) + return Result.try(function(try) + local interpolated = {} + for k, v in pairs(tbl) do + if type(v) == "string" then + interpolated[k] = try(M.eval(v, ctx)) + elseif type(v) == "table" then + interpolated[k] = try(M.tbl_interpolate(v, ctx)) + else + interpolated[k] = v + end + end + return interpolated + end) +end + return M |
