Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toml: Provide a way to set default values when decoding TOML #22181

Open
1 of 2 tasks
syobocat opened this issue Sep 8, 2024 · 0 comments
Open
1 of 2 tasks

toml: Provide a way to set default values when decoding TOML #22181

syobocat opened this issue Sep 8, 2024 · 0 comments
Labels
Feature Request This issue is made to request a feature.

Comments

@syobocat
Copy link

syobocat commented Sep 8, 2024

Describe the feature

Currently, this code:

import toml

struct Object {
	text    string
	integer int
}

fn main() {
	null_toml := toml.parse_text('')!
	object := null_toml.decode[Object]()!
	println(object.text)
	println(object.integer)
}

returns

toml.Any(toml.Null{})
0

and I believe that there is no way to change this behavior (You can use .reflect() instead of .decode() to get an empty string instead of toml.Any(toml.Null{}), though).

I think it is very useful if Vlang has a way to provide default values:

option 1) by an attribute, like

struct Object {
	text    string [toml_default: 'hello']
	integer int    [toml_default: 123]
}

or

option 2) by respecting the default value of the field, like

struct Object {
	text    string = 'hello'
	integer int    = 123
}

Use Case

This should be useful, for example, when building an application that uses TOML format for its configuration file.

Proposed Solution

No response

Other Information

json.decode() respects the default value of the field:

import json

struct Object {
	text    string = 'hello'
	integer int    = 123
}

fn main() {
	object := json.decode(Object, '{}')!
	println(object.text)
	println(object.integer)
}
hello
123

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.7 620e064

Environment details (OS name and version, etc.)

V full version: V 0.4.7 e9c9580.620e064
OS: macos, macOS, 14.6.1, 23G93
Processor: 8 cpus, 64bit, little endian, Apple M3

getwd: /Users/syobon
vexe: /Users/syobon/.v/v
vexe mtime: 2024-09-07 12:37:14

vroot: OK, value: /Users/syobon/.v
VMODULES: OK, value: /Users/syobon/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.46.0
Git vroot status: weekly.2024.36-18-g620e0641 (2 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@syobocat syobocat added the Feature Request This issue is made to request a feature. label Sep 8, 2024
@syobocat syobocat changed the title Provide a way to set default values when decoding TOML toml: Provide a way to set default values when decoding TOML Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

1 participant