toml: update readme with value_opt() usage (#14569)
parent
80cc88427b
commit
84e375e38a
|
@ -79,6 +79,10 @@ To query for a value that might not be in the document you
|
||||||
can use the `.default_to(...)` function to provide a
|
can use the `.default_to(...)` function to provide a
|
||||||
default value.
|
default value.
|
||||||
|
|
||||||
|
For cases where a default value might not be appropiate or
|
||||||
|
to check if a value exists you can use `doc.value_opt('query')?`
|
||||||
|
instead.
|
||||||
|
|
||||||
```v
|
```v
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
|
@ -100,8 +104,15 @@ assert doc.value('table.array[0].a').string() == 'A'
|
||||||
// Provides a default value
|
// Provides a default value
|
||||||
assert doc.value('non.existing').default_to(false).bool() == false
|
assert doc.value('non.existing').default_to(false).bool() == false
|
||||||
|
|
||||||
|
// Check if value exist
|
||||||
|
// doc.value_opt('should.exist') or { ... }
|
||||||
|
// or
|
||||||
|
if value := doc.value_opt('table.array[1].b') {
|
||||||
|
assert value.string() == 'B'
|
||||||
|
}
|
||||||
|
|
||||||
// You can pass parts of the TOML document around
|
// You can pass parts of the TOML document around
|
||||||
// and still use .value() to get the values
|
// and still use .value()/.value_opt() to get the values
|
||||||
arr := doc.value('table.array')
|
arr := doc.value('table.array')
|
||||||
assert arr.value('[1].b').string() == 'B'
|
assert arr.value('[1].b').string() == 'B'
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue