-
Notifications
You must be signed in to change notification settings - Fork 211
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
default values for objects in variable length arrays #857
Comments
i stumbled upon #484 and its test, and noticed that in fact
results in:
|
Sorry for taking so long to respond! The func (v *VarData) UnmarshalTOML(text []byte) error {
v.SetDefaults()
return toml.NewDecoder(text).Decode(&v)
} I'll think about it. I dropped the support for it when switching to v2 because it wasn't really used and poorly defined at the time, but it may be worth rebuilding this feature in the new codebase. |
thanks for replying! makes sense, realized that this wouldn't work as i expected. I saw that BurntSushi/toml also has a it seems like even with one solution would be if it was somehow possible to define how new objects are constructed (eg being able to instruct the decoder to call a another hacky idea is to parse twice:
haven't yet tested if this works.. 🙂 |
Since we are talking about hacks, how about using a "serialization field" that is only used to know whether there is an actual value in the document, and set the default or value after unmarshaling? package main
import (
"fmt"
"github.com/pelletier/go-toml/v2"
)
type Integer struct {
OptValue *int `toml:"Value"`
V int
}
type Config struct {
Integers []Integer
}
func main() {
raw := []byte(`
[[Integers]]
Value = 3
[[Integers]]
Value = 4
[[Integers]] # should have a default
`)
var cfg Config
fmt.Println(toml.Unmarshal(raw, &cfg))
for i, x := range cfg.Integers {
if x.OptValue == nil {
cfg.Integers[i].V = -1
} else {
cfg.Integers[i].V = *x.OptValue
}
}
fmt.Printf("%#v", cfg)
}
# main.Config{Integers:[]main.Integer{main.Integer{OptValue:(*int)(0xc0000b6048), V:3}, main.Integer{OptValue:(*int)(0xc0000b6050), V:4}, main.Integer{OptValue:(*int)(nil), V:-1}}} https://play.golang.com/p/vzo_qqbQKAK I'm curious how people do it with |
ah, that also works well. the reason i had started investigating this is because we were doing something similar for a codebase i work on, and was wondering if there's a simpler way; instead of having it in the same struct though, we had two different structs, one with all pointers and one with no pointers. and we'd deserialize into the pointer struct, and then compare if it's
I think for and because of this, I think if implementing
.. and seems inconsistent since the latter has the I wonder if using an interface like |
Hm at that point why not unmarshing into a an |
Hi there, I realize that v2 doesn't support default values - but I'm wondering the best way to do this, or if it's possible even in combination with go-defaults or other custom code.
I am trying to decode a TOML document that contains a variable length array of objects - and I'd like to prefill the field values as recommended in the readme.
eg, in this example, i'd like C to be set to
-1
when the key is not present:it seems like with a library like go-defaults, you'd still need to call a
SetDefaults()
method, and there isn't a good way to do this in advance for variable length array objects.An option seems to be to use
encoding.TextUnmarshaler
perhaps? i was thinking something like this:..but I was also curious with the TextUnmarshaler approach (if it works?), how you would know that the provided
text []bytes
parameter is a TOML string and due to a go-toml invocation, and not from decoding other formats - ie, this would error out if we were trying to decode a JSON string instead.Thank you!
The text was updated successfully, but these errors were encountered: