Skip to content

1.8.0 seems to introduce breaking change to custom decoders #498

@christian-stephen

Description

@christian-stephen

I have a custom decoder on a struct using the MapperValue interface. In 1.8.0, it appears that setting the target value (as seen in this example) doesn't update the value after parsing anymore, which I thought was the "proper" way to update the value. However, setting the value on the pointer receiver does work like in this example.

This is an example test that passes in 1.7.0, but not 1.8.0:

package decode_test

import (
	"github.com/alecthomas/kong"
	"gotest.tools/v3/assert"
	"reflect"
	"testing"
)

type CLI struct {
	Foo Foo `env:"FOO"`
}

type Foo struct {
	Bar string
}

func (f Foo) Decode(ctx *kong.DecodeContext) error {
	// Decoding stuff...

	ctx.Value.Target.Set(reflect.ValueOf(struct {
		Bar string
	}{"baz"}))
	return nil
}

func Test_Decode(t *testing.T) {
	c := &CLI{}
	parser, err := kong.New(c)
	assert.NilError(t, err)

	t.Setenv("FOO", "")

	_, err = parser.Parse([]string{})
	assert.NilError(t, err)

	assert.Equal(t, c.Foo.Bar, "baz")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions