Dota 2 Wiki
(Added Cargo.store (not currently used))
m (No longer returning `nil` instead of empty tables)
 
Line 31: Line 31:
   
 
local results = cargoQuery( tables, fields, args )
 
local results = cargoQuery( tables, fields, args )
-- Return nil if no results were found.
 
if #results == 0 then
 
return
 
end
 
 
results = clearEmptyValues( results )
 
results = clearEmptyValues( results )
   

Latest revision as of 16:11, 24 August 2018

Documentation for Module:Cargo Jump to code ↴ [ edit | purge ]

This module processes the output of Cargo queries. Empty values are removed and in case of no results, nil is returned instead of an empty table.

Performance

The module introduces some additional overhead leading to slightly worse performance. Raw mw.ext.cargo.query queries should be used where the automatic stripping of empty strings is not needed.

Module:Cargo mw.ext.cargo
Each call was running for about 3.6e-04 seconds.
    Mean runtime for each set was 3.6e-02 seconds,
    with standard deviation of 1.8e-03 seconds,
    minimum 6.0e-06, maximum 7.0e-06.
    Total time spent was about 3.6e-01 seconds.
Relative load is estimated to 5,579.6.
Each call was running for about 2.3e-04 seconds.
    Mean runtime for each set was 2.3e-02 seconds,
    with standard deviation of 1.6e-03 seconds,
    minimum 8.0e-06, maximum 1.0e-05.
    Total time spent was about 2.3e-01 seconds.
Relative load is estimated to 2,754.4.

Syntax

Main Article: Cargo documentation

The syntax is identical to the one provided by mw.ext.cargo.


local Cargo = {}

local checkType = require( 'libraryUtil' ).checkType
local cargoQuery = mw.ext.cargo.query

-- Clear empty string values from a given table and all subtables.
--
-- @param tbl
local function clearEmptyValues( tbl )
  local newTbl = {}
  for k,v in pairs( tbl ) do
    if type( v ) == 'table' then
      newTbl[k] = clearEmptyValues( v )
    elseif v ~= '' then
      newTbl[k] = v
    end
  end

  return newTbl
end

-- Run a Cargo query.
--
-- @param tables
-- @param fields
-- @param args
function Cargo.query( tables, fields, args )
  checkType( 'query', 1, tables, 'string' )
  checkType( 'query', 2, fields, 'string' )
  checkType( 'query', 3, args, 'table', true )

  local results = cargoQuery( tables, fields, args )
  results = clearEmptyValues( results )

  return results
end

-- Store values to a Cargo table.
--
-- @param cargoTable
-- @param values
function Cargo.store( cargoTable, values )
  checkType( 'store', 1, cargoTable, 'string' )
  checkType( 'store', 2, values, 'table' )

  local args = {}
  args._table = cargoTable
  for k,v in pairs( values ) do
    k = tostring( k )
    v = tostring( v )
    args[k] = v
  end

  mw.getCurrentFrame():callParserFunction( '#cargo_store', args )
end

return Cargo