Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue|Question] Regarding map[string][]string #308

Closed
eltonsv opened this issue May 8, 2024 · 1 comment
Closed

[Issue|Question] Regarding map[string][]string #308

eltonsv opened this issue May 8, 2024 · 1 comment
Labels

Comments

@eltonsv
Copy link

eltonsv commented May 8, 2024

Hi folks, we noticed that for configs with JSON pointing to map[string][]string, it fails with the error, and we are unable to parse these JSONs. Could you please help us if there is something wrong with the implementation? Added a test that replicates this below:

package app

import (
	"encoding/json"
	"testing"

	"github.com/caarlos0/env/v10"
	"github.com/stretchr/testify/assert"
)

type AppConfigTest struct {
	FinanceAllowedRoles FinanceRolesMapTest `env:"FINANCE_ALLOWED_ROLES"`
}

type FinanceRolesMapTest map[string][]string

func (rc *FinanceRolesMapTest) UnmarshalText(b []byte) error {
	return json.Unmarshal(b, &rc)
}

func TestParseConfig(t *testing.T) {
	t.Setenv("FINANCE_ALLOWED_ROLES", `{"DEFAULT":["ADMIN"]}`)
	_, err := parseConfigTest()
	assert.Equal(t, nil, err)
}

func parseConfigTest() (AppConfigTest, error) {
	var config AppConfigTest

	if err := env.Parse(&config); err != nil {
		return AppConfigTest{}, err
	}

	return config, nil
}

yields

=== RUN   TestParseConfig
    config_test.go:24: 
        	Error Trace:	/Users/e.savio/go/dh/dh-vt-kratos/go-services/vp-finance-backend/app/config_test.go:24
        	Error:      	Not equal: 
        	            	expected: <nil>(<nil>)
        	            	actual  : env.AggregateError(env.AggregateError{Errors:[]error{env.ParseError{Name:"FinanceAllowedRoles", Type:(*reflect.rtype)(0x1c3ce80), Err:(*json.UnmarshalTypeError)(0xc000196820)}}})
        	Test:       	TestParseConfig
--- FAIL: TestParseConfig (0.00s)

Expected :<nil>(<nil>)
Actual   :env.AggregateError(env.AggregateError{Errors:[]error{env.ParseError{Name:"FinanceAllowedRoles", Type:(*reflect.rtype)(0x1c3ce80), Err:(*json.UnmarshalTypeError)(0xc000196820)}}})
FAIL

Process finished with the exit code 1

Do you know if there is something that needs to be changed to support this in the json/implementation, or if this is an issue in the parsing?

Thank you so much

@caarlos0
Copy link
Owner

hey! you're on the right track, but the unmarshal impl needs to be a bit different afaik.

I pushed a test/example in c4db909

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants