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:
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:
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.
input = { order = { items = { { sku = "A" }, { sku = "B" } } } }Steps
- Return
sku: theskuof the second item, at"order.items[2].sku"ininput. - Add
missing: theskuat"order.items[9].sku", or"none". - Add
meta:input.order.metaafter setting"order.meta.priority"to"high"withtpath.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.