caarlos0 / caarlos0/env

Do not support pointer to user type

Open
#385 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.3k
Forks
284
Avg merge
1d 17h
Merged PRs (30d)
1

Description

// 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 https://github.com/serg2014/env/commit/931c721de5f2c30e04742b1221fb44af9d736487
resolve problem

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the supplied TestCustomParserWIthDefault reproducer and inspect ParseWithOptions with its FuncMap handling for pointer fields. Compare the referenced commit, then verify that custom parsing updates both direct and nested *foo fields to the environment values without changing non-pointer behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.