code-generator icon indicating copy to clipboard operation
code-generator copied to clipboard

applyconfiguration-gen do not handle slices of pointers correctly

Open vflaux opened this issue 1 year ago • 5 comments

For the following struct:

type Foo struct {
  Bar []*string `json:"bar,omitempty"`
}

applyconfiguration-gen generate the following "With" function:

func (b *Foo ) WithBar(values ...*string) *Bar{
	for i := range values {
		if values[i] == nil {
			panic("nil value passed to WithBar")
		}
		b.Bar = append(b.Bar, *values[i])
	}
	return b
}

Which causes cannot use *values[i] (variable of type string) as *string value in argument to append values[i] should not be dereferenced. The correct code is:

func (b *Foo ) WithBar(values ...*string) *Bar{
	for i := range values {
		if values[i] == nil {
			panic("nil value passed to WithBar")
		}
-		b.Bar = append(b.Bar, *values[i])
+		b.Bar = append(b.Bar, values[i])
	}
	return b
}

vflaux avatar Jan 30 '24 14:01 vflaux