-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
This should already be possible. Check out the Value Retrieval section |
|
Feature request: struct Test {
text string = 'abc'
} Because struct may has custom default values, so in compile time reflection, it need some way to get the default value, 'abc' $for f in Test.fields {
if ... { test.$(f.name) = f.default_value }
} This may request add a new field in builtin/builtin.v // FieldData holds information about a field. Fields reside on structs.
pub struct FieldData {
pub:
name string // the name of the field f
typ int // the internal TypeID of the field f,
unaliased_typ int // if f's type was an alias of int, this will be TypeID(int)
default_value string // the default value of the field f
attrs []string // the attributes of the field f
is_pub bool // f is in a `pub:` section
is_mut bool // f is in a `mut:` section
is_shared bool // `f shared Abc`
is_atomic bool // `f atomic int` , TODO
is_option bool // `f ?string` , TODO
is_array bool // `f []string` , TODO
is_map bool // `f map[string]int` , TODO
is_chan bool // `f chan int` , TODO
is_enum bool // `f Enum` where Enum is an enum
is_struct bool // `f Abc` where Abc is a struct , TODO
is_alias bool // `f MyInt` where `type MyInt = int`, TODO
indirections u8 // 0 for `f int`, 1 for `f &int`, 2 for `f &&int` , TODO
} |
Describe the feature
Currently, this code:
returns
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 oftoml.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
or
option 2) by respecting the default value of the field, like
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:Acknowledgements
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.
The text was updated successfully, but these errors were encountered: