-
Notifications
You must be signed in to change notification settings - Fork 92
refactor(widget): improve props handling, esp for templates #6319
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,17 +8,78 @@ | |||||||||
| local Lua = require('Module:Lua') | ||||||||||
|
|
||||||||||
| local Class = Lua.import('Module:Class') | ||||||||||
| local Json = Lua.import('Module:Json') | ||||||||||
| local Logic = Lua.import('Module:Logic') | ||||||||||
|
|
||||||||||
| local WidgetFactory = {} | ||||||||||
|
|
||||||||||
| ---@param args {widget: string, children: ((Widget|Html|string|number)[])|Widget|Html|string|number, [any]:any} | ||||||||||
| ---@param args {widget: string, children: any, [any]:any} | ||||||||||
|
Comment on lines
-14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo make a type alias for the original children type and use that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice idea we could use it in some other places too :) |
||||||||||
| ---@return Widget | ||||||||||
| function WidgetFactory.fromTemplate(args) | ||||||||||
| local widgetClass = args.widget | ||||||||||
| args.widget = nil | ||||||||||
| args.children = type(args.children) == 'table' and args.children or {args.children} | ||||||||||
| local WidgetClass = Lua.import('Module:Widget/' .. widgetClass) | ||||||||||
|
|
||||||||||
| local WidgetClass = Lua.requireIfExists('Module:Widget/' .. widgetClass) | ||||||||||
| assert(WidgetClass, 'Widget not found: ' .. widgetClass) | ||||||||||
| ---@cast WidgetClass Widget | ||||||||||
|
|
||||||||||
| local propSpec = WidgetClass.propSpec | ||||||||||
| args.children = WidgetFactory._parseTable(args.children) or args.children | ||||||||||
|
|
||||||||||
| for propName, prop in pairs(propSpec) do | ||||||||||
| if prop.type == 'integer' then | ||||||||||
| args[propName] = WidgetFactory._parseInteger(args[propName]) | ||||||||||
|
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about non integer numbers?
Suggested change
|
||||||||||
| elseif prop.type == 'string' then | ||||||||||
| args[propName] = WidgetFactory._parseString(args[propName]) | ||||||||||
| elseif prop.type == 'table' then | ||||||||||
| args[propName] = WidgetFactory._parseTable(args[propName]) | ||||||||||
| elseif prop.type == 'boolean' then | ||||||||||
| args[propName] = WidgetFactory._parseBoolean(args[propName]) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| return WidgetClass(args) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| ---@param integer any | ||||||||||
| ---@return integer|nil | ||||||||||
| function WidgetFactory._parseInteger(integer) | ||||||||||
| if type(integer) == 'number' then | ||||||||||
| return integer | ||||||||||
| end | ||||||||||
| return tonumber(integer) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| ---@param string any | ||||||||||
| ---@return string|nil | ||||||||||
| function WidgetFactory._parseString(string) | ||||||||||
| if type(string) == 'string' then | ||||||||||
| return string | ||||||||||
| end | ||||||||||
| return tostring(string) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| ---@param table any | ||||||||||
| ---@return table|nil | ||||||||||
| function WidgetFactory._parseTable(table) | ||||||||||
| if type(table) == 'table' then | ||||||||||
| return table | ||||||||||
| elseif type(table) == 'string' then | ||||||||||
| local parsedTable, parsingError = Json.parseStringified(table) | ||||||||||
| if not parsingError then | ||||||||||
| return parsedTable | ||||||||||
| end | ||||||||||
| end | ||||||||||
| return nil | ||||||||||
| end | ||||||||||
|
|
||||||||||
| ---@param boolean any | ||||||||||
| ---@return boolean|nil | ||||||||||
| function WidgetFactory._parseBoolean(boolean) | ||||||||||
| if type(boolean) == 'boolean' then | ||||||||||
| return boolean | ||||||||||
| end | ||||||||||
| return Logic.readBool(boolean) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| return Class.export(WidgetFactory, {exports = {'fromTemplate'}}) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo add a comment that this handling is only legacy and deprecated