-
Notifications
You must be signed in to change notification settings - Fork 359
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
Triple operator for conditional shortcut #115
Comments
Yup, a ternary operator for macro conditionals would be really handy. No strong opinion on the separator character. Other than noting how unfortunate it is that rpm used a syntax that is incompatible with the rest of the world :( |
The separator character should be chosen such that it will not cause problems with old macros: If we define, that: |
I do not see any possibility how to define sensible syntax of the triple operator, without possible causing problems for macros %{?condition:true} and %{!?condition:false}. Thus I think that adding this macro without additional changes is not a good idea. The syntax of the macro should start "%{?condition:" or "%{!?condition:" If we change separator operator from "!" to another character it will not help us a lot. The mentioned type of mistake will still occur. The only characters which are different from rpm point of view are "{", "}", "(", ")". To use some of them is not a good choice too. The triple operator will look very weird and probably another type of problems occur. Similarly for a separator operator defined as a sequence of chars. |
Thinking about it some more... Syntax: %{?condition:{true}!{false}} is OK. So if it is acceptable, I will make a patch for it. |
An old macro could just as legally and likely contain standalone { } characters as it can contain !'es, I don't see that as being any safer really. |
Notation
looks promising for me.
Similar notations that can be chosen instead:
More detailed description of the notation:
It can not cause problems in the existing macros because: If the implementation will be done wisely it can change the expansion to more The only different unexpected beaviour for the current spec files can arise in a case like: %{!?{condition}:sth1:sth2}. In such a case the current implementation expands it to "" and the new behaviour will expand it to sth1 or sth2. Thus it can potentially change behavior from the current one to different unexpected behavior. But such case is highly unlikely and the original behavior is already different from the expected one (as explained above). So it changes one unexpected behavior to some different one. |
Bike shedding: I'd prefer Or |
You are correct %{?:condition:true:false} and %{?!:condition:false:true} also should not conflict with the current macro usage. I am OK with this notation too. |
Given the proposed syntax, which looks ugly in all cases, I'd prefer to forget about the ternary operator in RPM and focus on other important issues of RPM. |
…ment#115) %{? { macro_name } : true : false } %{? { macro_name } : true } %{?! { macro_name } : false : true } %{?! { macro_name } : false } More detailed description of the notation: * Between the first chars "%{?" resp. "%{!?" or "%{?!" there can not be a space. * On the other hand arround {condition} and :, there can but not need to be a space. * In {condition}, there can be a space before or after the macro_name. So e.g. the folowing conditions are similar (from rpm point of view): %{?{macro}:true:false} %{? { macro } : true : false } The relevant tests are added.
%{? { macro_name } : true : false } %{? { macro_name } : true } %{?! { macro_name } : false : true } %{?! { macro_name } : false } More detailed description of the notation: * Between the first chars "%{?" resp. "%{!?" or "%{?!" there can not be a space. * On the other hand around {condition} and :, there can but not need to be a space. * In {condition}, there can be a space before or after the macro_name. So e.g. the following conditions are similar (from rpm point of view): %{?{macro}:true:false} %{? { macro } : true : false } The relevant tests are added. Closes: rpm-software-management#115
%{? { macro_name } : true : false } %{? { macro_name } : true } %{?! { macro_name } : false : true } %{?! { macro_name } : false } More detailed description of the notation: * Between the first chars "%{?" resp. "%{!?" or "%{?!" there can not be a space. * On the other hand around {condition} and :, there can but not need to be a space. * In {condition}, there can be a space before or after the macro_name. So e.g. the following conditions are similar (from rpm point of view): %{?{macro}:true:false} %{? { macro } : true : false } The relevant tests are added. Closes: rpm-software-management#115
%{? { macro_name } : true : false } %{? { macro_name } : true } %{?! { macro_name } : false : true } %{?! { macro_name } : false } More detailed description of the notation: * Between the first chars "%{?" resp. "%{!?" or "%{?!" there can not be a space. * On the other hand around {condition} and :, there can but not need to be a space. * In {condition}, there can be a space before or after the macro_name. So e.g. the following conditions are similar (from rpm point of view): %{?{macro}:true:false} %{? { macro } : true : false } The relevant tests are added. Closes: rpm-software-management#115
While we're thinking about extending the conditional macro syntax, here's another thing to consider: The current ? test is only for (non-)existence of macro, which is extremely limiting. We could easily make the spec %if expression parser available to macro engine, which would give a whole new level of power to macros. I think we should plan the new syntax to be able to accommodate this, even if it's not initially implemented, we don't want to have to invent yet another syntax for that later on. |
%{?{macro_name}:true:false} %{?{macro_name}:true} %{?!{macro_name}:false:true} %{?!{macro_name}:false} The relevant tests are added. Closes: rpm-software-management#115
Accepted syntax: %{?{macro_name}:true:false} %{?{macro_name}:true} %{?!{macro_name}:false:true} %{?!{macro_name}:false} The relevant tests are added. Closes: rpm-software-management#115
Another thing is that this syntax makes it impossible to have colons (':') in the output (eg '%{!?foo::}'), which is a limitation the original syntax doesn't have'. It obviously has it's own set of limitations and quirks... |
PR #817 adds support for arbitrary expression parsing in macros. The real power would come from wedding that to macro conditionals, and that's the thing I want to see at least planned for before we add any new condition syntaxes. |
Not impossible, but not straightforward. For ':' in the output you can use %define / %global: |
Accepted syntax: %{?{macro_name}:true:false} %{?{macro_name}:true} %{?!{macro_name}:false:true} %{?!{macro_name}:false} The relevant tests are added. Closes: rpm-software-management#115
Please, what it the expected be gain from it? |
I thought the potential gains from ability of testing arbitrary expressions instead of simple macro existence would be obvious enough not to need explanations. The most basic case is that there's tonne of functionality in rpm which uses macro existence test to determine whether something is enabled or disabled, because that's all that's available. When people see something like "%_include_minidebuginfo 1", they tend to assume defining it to 0 disables it, and get confused and annoyed when it doesn't. It gets worse because, as you know, macros stack so undefining doesn't guarantee said macro actually goes away. So in practise, there's no guaranteed way to disable such a feature from a spec or otherwise. There are countless other examples, such as defining a macro depending on the string value of another, etc. |
To elaborate a bit further... so what rpm really needs, much more than the triple-operator for existence, is a macro syntax that supports the generic form:
expression is an arbitrary expression evaluated by rpmExprBool() (ie the same as spec %if), true case is output when expression is true, optionally followed by a false case. The macro existence test (triple syntax or not) could be seen as a specific sub-case of this all. |
Just to start the discussion, I wonder if support for
|
OTOH we could add a
|
My general idea has been that %{expr:...} is strictly for evaluating expressions into strings, and that a different syntax would be used for boolean evaluation, something along the lines of Adding support for |
Support for ? :in the expression parser has the addition bonus that it resolves the "whitespace stripping" discussion:
Also note that the expr parser already has a (somewhat insane) macro expansion feature:
|
I do not see any problem in the syntax that @pmatilai proposed in his previous comment: Using these syntax the example from the previous @mlschroe comment looks: Syntax: |
So thanks to @mlschroe , we now have much more than originally bargained for. See https://github.com/rpm-software-management/rpm/blob/master/doc/manual/macros#L238 for docs and from Line 248 in 796104e
|
I suggest the following notations
as shortcuts for the current form
The text was updated successfully, but these errors were encountered: