Skip to content

Proposal: add visible ToMap for tests in applications #274

@maxfilov

Description

@maxfilov

Good day.
I'm using the env library in on of my projects and I lacked ability to use toMap defined in this module. Let me elaborate.

In my application I have something like

func parseConfiguration() (*Configuration, error) 

which uses env library under the hood. Now I want to test it and my first thought is to parse environment as an argument to my function and then let it through my function to env library. Something lik

func parseConfiguration(environ []string) (*Configuration, error) {
	// ...
	err := env.ParseWithOptions(
		&configuration,
		env.Options{
			Prefix:      "STATUS_METER_",
			Environment: environ, // <- this won't work
		})
	// ...
}

The Environment: environ, line won't work because Environment is map[string]string and it uses the toMap function which is not visible outside of the library.

What are the options?

  • set env vars during the test invocation. The tests will be not clear because it won't be easy to deduce where did the environment come from
  • set env through os.Setenv. This is better and this is what I'm using currently, but it still not clear because I would like to have pure function which converts input to output.
  • provide public ToMap which just wraps internal toMap. I would be able to duplicate the library logic, but it will be duplication.
  • provide another option called Environ or smth like that alongside Environment and teach library to use the former if it's provided and the latter for the backward compatibility. Environ should have []string type.

What is your feedback on that?

PS: I'm a new to the Go world so I could miss something.

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