Skip to content
Snippets Groups Projects
Commit b055625f authored by Ilya Verbin's avatar Ilya Verbin Committed by Aleksandr Lyapunov
Browse files

box: support functional default field values

Now the default value of a tuple field can be computed as a result of a
function call.

Closes #8609

@TarantoolBot document
Title: Document functional default field values
Product: Tarantool
Since: 3.0
Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/format/
Depends on: tarantool/doc#3520

The format clause contains, for each field, a definition within braces:
`{name='...',type='...'[,default=...][,default_func=...]}`,
where:

* (Optional) The `default_func` string value specifies the name of a
  function, that is used to generate a default value for the field. If
  `default_func` is set, the `default` value is used as the function
  argument. See [field default functions](https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#field-default-functions).

---

Root document: https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#constraint-functions

**Field default functions**

Stored Lua function can be used to generate a default value for the tuple
field. It can take one optional parameter, and must return exactly one
value. To create a field default function, use func.create with function
body. The function must not yield.

Example:

```lua
box.schema.func.create('random_point', {
    language = 'Lua',
    body = 'function(param) return math.random(param.min, param.max) end'
})

box.schema.space.create('test')
box.space.test:create_index('pk')
box.space.test:format({
    {name = 'id', type = 'integer'},
    {name = 'latitude', type = 'number',
     default_func = 'random_point', default = {min = -90, max = 90}},
    {name = 'longitude', type = 'number',
     default_func = 'random_point', default = {min = -180, max = 180}}
})
```

```lua
tarantool> math.randomseed(os.time())
---
...

tarantool> box.space.test:insert{1}
---
- [1, 56, 38]
...
```
parent 796d4c7d
No related branches found
No related tags found
No related merge requests found
Showing
with 602 additions and 19 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment