Skip to content

Do not support pointer to user type #385

@serg2014

Description

@serg2014

// use github.com/caarlos0/env/v11 v11.3.1

func TestCustomParserWIthDefault(t *testing.T) {
	type foo struct {
		name string
	}

	type bar struct {
		Name string `env:"OTHER_CUSTOM"`
		Foo  *foo   `env:"BLAH_CUSTOM"`
	}

	type config struct {
		Var   foo  `env:"VAR_CUSTOM"`
		Foo   *foo `env:"BLAH_CUSTOM"`
		Other *bar
	}

	t.Setenv("VAR_CUSTOM", "test")
	t.Setenv("OTHER_CUSTOM", "test2")
	t.Setenv("BLAH_CUSTOM", "test3")

	runtest := func(t *testing.T) {
		t.Helper()
		cfg := &config{
			Var: foo{"var"},
			Foo: &foo{"foo"},
			Other: &bar{
				Name: "name",
				Foo:  &foo{"foo"},
			},
		}
		err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
			reflect.TypeOf(foo{}): func(v string) (interface{}, error) {
				return foo{name: v}, nil
			},
		}})

		isNoErr(t, err)
		expect := &config{
			Var: foo{"test"},
			Foo: &foo{"test3"},
			Other: &bar{
				Name: "test2",
				Foo:  &foo{"test3"},
			},
		}
		assert.Equal(t, expect, cfg)
	}

	for i := 0; i < 10; i++ {
		t.Run(fmt.Sprintf("%d", i), runtest)
	}
}

this test is failed

 Diff:
                                --- Expected
                                +++ Actual
                                @@ -5,3 +5,3 @@
                                  Foo: (*env.foo)({
                                -  name: (string) (len=5) "test3"
                                +  name: (string) (len=3) "foo"
                                  }),
                                @@ -10,3 +10,3 @@
                                   Foo: (*env.foo)({
                                -   name: (string) (len=5) "test3"
                                +   name: (string) (len=3) "foo"
                                   })

this patch serg2014@931c721
resolve problem

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