pelletier/go-toml

Setting a root point for Unmarshal

Closed this issue · 1 comments

I've been using version 2 and it works very well. Thanks.

But I do have a question on the options I have to do something not mentioned in its documentation:

Imagine this TOML file:

a = 1

[s1]
b = 2

[s2]
c = 3

[s4]
d = 4

and these 2 structures in go:

type Cfg1 struct {
  S1 struct {
    B int
  }
  S2 struct {
    C int
  }
}

type Cfg2 struct {
  D int
}

Is it possible for me to execute Unmarshal twice to fill both structs? When running for Cfg1 it works, but I can not find a way to force Cfg2 to understand its root is s4 block inside the TOML.

Yes, I know I can create a single struct that covers the whole TOML struct, and then read its sub-structures. That's how I have it now. But in reality my TOML is huge and I have to broke it into 5 unrelated big structures to feed 5 different services of my server. Having a single central and huge structure looks "ugly".

Just asking myself if I am missing something that would allow for the cleaner approach of selecting a root point when Unmarshaling.

Hi! Sorry for the delay. I don't think you have a way to do this easily at the moment. Your best option as you mentioned is to create a full structure. Otherwise maybe you can build something on top of the parser API, but that's a very low level component which will probably make you rebuild quite a bit of the unmarshaler logic. I wonder if it's something that other encoding libraries (json, yaml, etc) have solved in Go.