Module talk:Iteminfo

From Terraria Wiki
Jump to navigation Jump to search

About The "Module:iteminfo line 321" error

Finally, I found the problem.
The structure of iteminfo data (lua table) is simplified as follows:

{
  ['_terrariaversion'] = '1.4.4.9',
  ['_generated'] = '2023-07-07 00:58:58 (+00:00)',
  [0] = { --[[Data of item 0]] },
  [1] = { --[[Data of item 1]] },
  [2] = { --[[Data of item 2]] },
  -- and so on.
}

This table will be encoded as json string and cached in luacache. When using iteminfo, the json string will be loaded and decoded back to lua table. (It is about 3x faster than storing Lua table to luacahce directly).
Since there are string indexes in the table, the expected result json string should looks like:

{
  "_generated":"2023-07-07 00:58:58 (+00:00)",
  "_terrariaversion":"1.4.4.9",
  "0": {/* data of item 0 */},
  "1": {/* data of item 1 */},
  // and so on
}

And that's true most of the time. but Weirdly, sometimes the result string will become as follows:

{
  {/* data of item 0 */},
  {/* data of item 1 */},
  // and so on
  // "_generated" and "_terrariaversion" keys are gone.
}

This is a zero-based json array, therefor it will be decoded to an one-based lua table as follows by mw.text.jsonDecode:

{
  [1] = { --[[Data of item 0]] },
  [2] = { --[[Data of item 1]] },
  [3] = { --[[Data of item 2]] },
  -- and so on.
}

Since index 0 does not exist in this case, the data[0] in Module:iteminfo line 321 will cause the error. This may be because the platform uses LuaStandalone (default for Scribunto) to run lua code, and the order of arrays is not preserved when using LuaStandalone (ref). I changed all number keys of iteminfo data to string, hope it works. --Westgrass (talk) 21:22, 12 February 2024 (UTC)

Great, thanks for investigating this! --Rye Greenwood (talk) 02:59, 14 February 2024 (UTC)
A similar problem occurred with nameDB, so now I cached the json strings directly instead. --Westgrass (talk) 16:15, 21 February 2024 (UTC)