Jump to content

Module:Dump/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 06:51, 17 October 2016 (how to dump a table from a module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This module can dump a table by displaying its contents as text. That may help develop other modules.

An alternative is to use mw.dumpObject() but the result from this module is clearer and is close to valid Lua source.

Dump of a Wikidata entity

When working with Wikidata, it can be useful to examine a table representing an entity.

For example, Southern African Large Telescope is d:Q833639. That entity can be viewed as a Lua table by previewing:

{{#invoke:dump|wikidata|Q833639}}

To do that, edit your sandbox and replace its contents with the above line, then click Show preview. Module talk:Dump shows this example.

If wanted, the width of each indented column can be set, for example to 2 spaces:

{{#invoke:dump|wikidata|Q833639|indent=2}}

A property such as diameter (P2386) can also be displayed:

{{#invoke:dump|wikidata|P2386}}

Dump of table for another module

If there is a problem debugging a module, it can be helpful to use a sandbox copy of the module to display the contents of a table to confirm that it contains expected data. The following shows how a module can dump a table.

local function main()
	local example = {
		link = true,
		fruit = { yellow = 'bannana', red = 'cherry' },
		11, 22, 33,
	}
	local dump = require('Module:Dump')._dump
	return dump(example, 'example')
end

return { main = main }

With the above code in Module:Example, the result could be displayed by previewing:

{{#invoke:example|main}}

Complex table example

The module contains a complex table for testing. The table can be displayed by previewing:

{{#invoke:dump|testcase}}

Global table _G

In Lua, _G is a global variable which is a table holding information about all global variables. The _G table can be displayed by previewing (both G and _G work):

{{#invoke:dump|testcase|G}}

If wanted, the width of each indented column can be set, for example to 2 spaces:

{{#invoke:dump|testcase|G|indent=2}}