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

Closed
1 of 2 tasks
syobocat opened this issue Sep 8, 2024 · 3 comments
Closed
1 of 2 tasks

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

syobocat opened this issue Sep 8, 2024 · 3 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
@prashanth-hegde
Copy link
Contributor

This should already be possible.
https://modules.vlang.io/toml.html

Check out the Value Retrieval section
You should be able to use .default_to('')

@syobocat
Copy link
Author

syobocat commented Sep 24, 2024

.default_to() should be enough for manual deserialization, but I think .default_to() cannot be used along with .decode() (Sorry if I'm wrong).

@kbkpbot
Copy link
Contributor

kbkpbot commented Oct 5, 2024

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

// 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
}

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

3 participants