1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: update readme with value_opt() usage (#14569)

This commit is contained in:
Larpon 2022-05-31 18:02:33 +02:00 committed by GitHub
parent 80cc88427b
commit 84e375e38a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
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
import toml
@ -100,8 +104,15 @@ assert doc.value('table.array[0].a').string() == 'A'
// Provides a default value
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
// and still use .value() to get the values
// and still use .value()/.value_opt() to get the values
arr := doc.value('table.array')
assert arr.value('[1].b').string() == 'B'
```