Skip to content

Conversation

@lyrixx
Copy link
Member

@lyrixx lyrixx commented Nov 25, 2024

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

This is the second time where I face this "bugs".
I wrote:

public function __construct(
    #[Autowire(env: 'bool:default:true:CASTOR_GENERATE_STUBS')]
    public $generateStubs,
) {
}

And I got this message:

>…egoire/dev/github.com/jolicode/castor(config-stubs *) CASTOR_GENERATE_STUBS=false castor 
Symfony\Component\DependencyInjection\Exception\RuntimeException^ {#471
  #message: "Invalid env fallback in "default:true:CASTOR_GENERATE_STUBS": parameter "true" not found."
  #code: 0
  #file: "./vendor/symfony/dependency-injection/EnvVarProcessor.php"
  #line: 145
  trace: {

I now understand the meaning of default, but there are many cases where we want to define
a raw default value.

So I want to introduce the default_value processor.

@stof
Copy link
Member

stof commented Nov 25, 2024

the issue with a raw default value is that this is restricted to values that don't contain :, which is why it cannot solve things in a general way.

However, for the simple cases of boolean or numbers, it might indeed make sense to have default_value

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Nov 27, 2024

Isn't this confusing? default_value:false:FOO will lead to string 'false', not false the boolean.
The current way is verbose, but at least it fully supports any possible values (arrays, booleans, strings, etc.)

@nicolas-grekas
Copy link
Member

Instead, we could create parameters that are named true/false. 🤔

@lyrixx
Copy link
Member Author

lyrixx commented Nov 27, 2024

Isn't this confusing? default_value:false:FOO will lead to string 'false', not false the boolean.

Indeed, this is why I cast to bool in the PR description.

The current way is verbose, but at least it fully supports any possible values (arrays, booleans, strings, etc.)

What current way? without the PR, I didn't found a way to achieve that without defining new parameters true. And this is what I commited as a workaround, but it's is not really handy

Instead, we could create parameters that are named true/false. 🤔

It only work with true / false. But I had another case (I don't remember where), but it was a string (not a bool). So it was even more weird do do that

@carsonbot carsonbot changed the title Add support for default_value as env var processor [DependencyInjection] Add support for default_value as env var processor Jan 7, 2025
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 7, 2025

I'm not super confident about this approach. I understand the convenient part, but I also anticipate this could be confusing.
Did you try using default parameters instead of the default in env var?

parameters:
    'env(MY_VAR)': 'the-default'

@lyrixx
Copy link
Member Author

lyrixx commented Jan 27, 2025

Did you try using default parameters instead of the default in env var?

It works. Thanks, Let's close

@lyrixx lyrixx closed this Jan 27, 2025
@lyrixx lyrixx deleted the dic-defaul-value branch January 27, 2025 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants