ilyakaznacheev / cleanenv

✨Clean and minimalistic environment configuration reader for Golang
MIT License
1.66k stars 115 forks source link

Using error struct for additionals data #141

Open MashinaMashina opened 6 months ago

MashinaMashina commented 6 months ago

With several nested structures with identical fields, there is no way to understand which error occurred.

This MP, without changing backward compatibility, adds a structure from which you can find out the field name.

If the community wishes, you can include the name in the error message.

ilyakaznacheev commented 6 months ago

Hi @MashinaMashina ,

Thanks for the PR. I'm not sure if putting the env name into the error description is the best idea, since env is an optional tag. I'm thinking about something like a path to the variable in the structure.

Not saying your solution is bad or something, it is just not comprehensive. Building a path will solve this problem for all kinds of fields, even lists and maps.

In my head, it looks like a simplified JSONPath format. Something like scruct_field1/list_field2[0]/map_field3[key]/field4.

What do you think?

MashinaMashina commented 6 months ago

@ilyakaznacheev I added the path to the field with which the error occurred. I tried to do it using []string, but there are rewriting errors in slice or the code becomes too complicated using the copy function.

Example error string: field "Database.Host" is required but the value is not provided

dreksun commented 5 months ago

@ilyakaznacheev Could you please merge this PR?

MashinaMashina commented 3 months ago

@ilyakaznacheev what about this PR?