Learn Lua · Time, IDs and checks · Lesson 3 of 4

Reaching into data

tpath reads deep into nested tables with a path such as "items[2].sku". A missing step gives nil, or a default you choose, instead of an error:

LUA
local order = { items = { { sku = "A" }, { sku = "B" } } }
tpath.get(order, "items[2].sku")       --> "B"
tpath.get(order, "items[9].sku", "?")  --> "?"

tpath.set writes a value at a path, creating the tables along the way:

LUA
tpath.set(order, "meta.source", "web")
tpath.get(order, "meta.source")  --> "web"

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { order = { items = { { sku = "A" }, { sku = "B" } } } }

Steps

  1. Return sku: the sku of the second item, at "order.items[2].sku" in input.
  2. Add missing: the sku at "order.items[9].sku", or "none".
  3. Add meta: input.order.meta after setting "order.meta.priority" to "high" with tpath.set.
Show a hint

Set the priority before you read input.order.meta, so the meta you return includes it. There may be no meta yet: tpath.set creates it.