ini icon indicating copy to clipboard operation
ini copied to clipboard

Parse struct slice with ReflectFrom

Open byarbrough opened this issue 4 years ago • 0 comments

I am getting an error when trying to ReflectFrom a slice of structs.

Is your feature request related to a problem? Please describe. I have the following (WireGuard) configuration:

[Interface]
PrivateKey = "iL7NXm+Z/yOAYx/7WHksdlPX1k4HGqVe/7vgu4bNcGQ="

[Peer]
PublicKey = "qpbABX58Z58bPH5glGt4GH8QXHf8AshiVlwnaJ6v+Ro="

[Peer]
PublicKey = "3/xgYsitp0qqRFmszphtI80FE5TuJAPZbH7l4NJBWWg="

And am mapping it to the following structs

type Config struct {
	PrivateKey string
	Peers      []*Peer `ini:",omitempty"`
}

type Peer struct {
	PublicKey           string
}

The pointer to a slice is needed because each configuration may have an indefinite amount of Peers.

I am using ReflectFrom to produce an INI file, which works if there are no Peers. But adding one or more gives this error:

reflect field "Peers": unsupported type '[]ptr'

Describe the solution you'd like If it was just a slice of keys, I would say write a parseSlice function with the following signature:

func (k *Key) parseSlice(s *[]interface{}, addInvalid, returnOnInvalid bool) ([]interface{}, error) {

This case would then need to be added to setSliceWithProperType.

But for an entire struct, I think a recursive call to ReflectFrom is needed.

Describe alternatives you've considered There is also ReflectINIStruct but it is in a test file, and I can't really tell what it does.

Additional Context https://gitlab.com/byarbrough/wgconf

byarbrough avatar Nov 12 '20 18:11 byarbrough