Damien Martin-Guillerez | ebb0039 | 2015-09-29 11:55:59 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
ccalvarin | f39dc6f | 2017-06-09 11:51:45 -0400 | [diff] [blame] | 14 | package com.google.devtools.common.options; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 15 | |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 16 | import static java.util.concurrent.TimeUnit.MINUTES; |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 17 | import static java.util.stream.Collectors.joining; |
| 18 | |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 19 | import com.google.common.base.Verify; |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ArrayListMultimap; |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Janak Ramakrishnan | e9cd0f3 | 2016-04-29 22:46:16 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 23 | import com.google.common.collect.Multimap; |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 24 | import com.google.common.flogger.GoogleLogger; |
| 25 | import com.google.common.flogger.LazyArgs; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues; |
| 27 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues; |
| 28 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy; |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy.OperationCase; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; |
| 31 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.SetValue; |
ajurkowski | ecab297 | 2021-06-17 14:26:34 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.SetValue.Behavior; |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.UseDefault; |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 34 | import com.google.devtools.common.options.OptionPriority.PriorityCategory; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 35 | import com.google.devtools.common.options.OptionsParser.OptionDescription; |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 36 | import java.util.ArrayList; |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 37 | import java.util.Collections; |
| 38 | import java.util.HashMap; |
Jonathan Bluett-Duncan | 0df3ddbd | 2017-08-09 11:13:54 +0200 | [diff] [blame] | 39 | import java.util.HashSet; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 40 | import java.util.List; |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 41 | import java.util.Map; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 42 | import java.util.Set; |
Janak Ramakrishnan | b40cbaa | 2016-11-18 00:20:45 +0000 | [diff] [blame] | 43 | import java.util.logging.Level; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 44 | import javax.annotation.Nullable; |
| 45 | |
| 46 | /** |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 47 | * Enforces the {@link FlagPolicy}s (from an {@link InvocationPolicy} proto) on an {@link |
| 48 | * OptionsParser} by validating and changing the flag values in the given {@link OptionsParser}. |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 49 | * |
| 50 | * <p>"Flag" and "Option" are used interchangeably in this file. |
| 51 | */ |
| 52 | public final class InvocationPolicyEnforcer { |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 53 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 54 | |
ccalvarin | 5fe8e66 | 2017-09-14 15:56:43 +0200 | [diff] [blame] | 55 | private static final String INVOCATION_POLICY_SOURCE = "Invocation policy"; |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 56 | @Nullable private final InvocationPolicy invocationPolicy; |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 57 | private final Level loglevel; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 58 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Creates an InvocationPolicyEnforcer that enforces the given policy. |
| 61 | * |
| 62 | * @param invocationPolicy the policy to enforce. A null policy means this enforcer will do |
| 63 | * nothing in calls to enforce(). |
| 64 | */ |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 65 | public InvocationPolicyEnforcer(@Nullable InvocationPolicy invocationPolicy) { |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 66 | this(invocationPolicy, Level.FINE); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates an InvocationPolicyEnforcer that enforces the given policy. |
| 71 | * |
| 72 | * @param invocationPolicy the policy to enforce. A null policy means this enforcer will do |
| 73 | * nothing in calls to enforce(). |
| 74 | * @param loglevel the level at which to log informational statements. Warnings and errors will |
| 75 | * still be logged at the appropriate level. |
| 76 | */ |
| 77 | public InvocationPolicyEnforcer(@Nullable InvocationPolicy invocationPolicy, Level loglevel) { |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 78 | this.invocationPolicy = invocationPolicy; |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 79 | this.loglevel = loglevel; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 82 | private static final class FlagPolicyWithContext { |
| 83 | private final FlagPolicy policy; |
| 84 | private final OptionDescription description; |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 85 | private final OptionInstanceOrigin origin; |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 86 | |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 87 | public FlagPolicyWithContext( |
| 88 | FlagPolicy policy, OptionDescription description, OptionInstanceOrigin origin) { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 89 | this.policy = policy; |
| 90 | this.description = description; |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 91 | this.origin = origin; |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 95 | public InvocationPolicy getInvocationPolicy() { |
| 96 | return invocationPolicy; |
| 97 | } |
| 98 | |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 99 | /** |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 100 | * Applies this OptionsPolicyEnforcer's policy to the given OptionsParser for all blaze commands. |
| 101 | * |
| 102 | * @param parser The OptionsParser to enforce policy on. |
| 103 | * @throws OptionsParsingException if any flag policy is invalid. |
| 104 | */ |
| 105 | public void enforce(OptionsParser parser) throws OptionsParsingException { |
Janak Ramakrishnan | e9cd0f3 | 2016-04-29 22:46:16 +0000 | [diff] [blame] | 106 | enforce(parser, null); |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 110 | * Applies this OptionsPolicyEnforcer's policy to the given OptionsParser. |
| 111 | * |
| 112 | * @param parser The OptionsParser to enforce policy on. |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 113 | * @param command The current blaze command, for flag policies that apply to only specific |
Janak Ramakrishnan | e9cd0f3 | 2016-04-29 22:46:16 +0000 | [diff] [blame] | 114 | * commands. Such policies will be enforced only if they contain this command or a command |
| 115 | * they inherit from |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 116 | * @throws OptionsParsingException if any flag policy is invalid. |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 117 | */ |
Janak Ramakrishnan | e9cd0f3 | 2016-04-29 22:46:16 +0000 | [diff] [blame] | 118 | public void enforce(OptionsParser parser, @Nullable String command) |
| 119 | throws OptionsParsingException { |
Luis Fernando Pino Duque | 5816b3b | 2016-04-11 15:33:26 +0000 | [diff] [blame] | 120 | if (invocationPolicy == null || invocationPolicy.getFlagPoliciesCount() == 0) { |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 121 | return; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 124 | // TODO(b/186167747): Remove the warning once we migrate to the new enum. |
| 125 | invocationPolicy.getFlagPoliciesList().stream() |
| 126 | .filter(p -> p.hasSetValue() && p.getSetValue().getBehavior() == Behavior.UNDEFINED) |
| 127 | .findFirst() |
| 128 | .ifPresent( |
| 129 | policy -> |
| 130 | logger.atWarning().atMostEvery(5, MINUTES).log( |
| 131 | "Invocation policy has missing/undefined behavior: %s", policy)); |
| 132 | |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 133 | // The effective policy returned is expanded, filtered for applicable commands, and cleaned of |
| 134 | // redundancies and conflicts. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 135 | List<FlagPolicyWithContext> effectivePolicies = |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 136 | getEffectivePolicies(invocationPolicy, parser, command, loglevel); |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 137 | |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 138 | for (FlagPolicyWithContext flagPolicy : effectivePolicies) { |
| 139 | String flagName = flagPolicy.policy.getFlagName(); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 140 | |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 141 | OptionValueDescription valueDescription; |
| 142 | try { |
| 143 | valueDescription = parser.getOptionValueDescription(flagName); |
| 144 | } catch (IllegalArgumentException e) { |
| 145 | // This flag doesn't exist. We are deliberately lenient if the flag policy has a flag |
| 146 | // we don't know about. This is for better future proofing so that as new flags are added, |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 147 | // new policies can use the new flags without worrying about older versions of Bazel. |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 148 | logger.at(loglevel).log( |
| 149 | "Flag '%s' specified by invocation policy does not exist", flagName); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | |
ccalvarin | 5fe8e66 | 2017-09-14 15:56:43 +0200 | [diff] [blame] | 153 | // getOptionDescription() will return null if the option does not exist, however |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 154 | // getOptionValueDescription() above would have thrown an IllegalArgumentException if that |
| 155 | // were the case. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 156 | Verify.verifyNotNull(flagPolicy.description); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 157 | |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 158 | switch (flagPolicy.policy.getOperationCase()) { |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 159 | case SET_VALUE: |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 160 | applySetValueOperation(parser, flagPolicy, valueDescription, loglevel); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 161 | break; |
| 162 | |
| 163 | case USE_DEFAULT: |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 164 | applyUseDefaultOperation( |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 165 | parser, "UseDefault", flagPolicy.description.getOptionDefinition(), loglevel); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case ALLOW_VALUES: |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 169 | AllowValues allowValues = flagPolicy.policy.getAllowValues(); |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 170 | FilterValueOperation.AllowValueOperation allowValueOperation = |
| 171 | new FilterValueOperation.AllowValueOperation(loglevel); |
| 172 | allowValueOperation.apply( |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 173 | parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 174 | flagPolicy.origin, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 175 | allowValues.getAllowedValuesList(), |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 176 | allowValues.hasNewValue() ? allowValues.getNewValue() : null, |
| 177 | allowValues.hasUseDefault(), |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 178 | valueDescription, |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 179 | flagPolicy.description); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 180 | break; |
| 181 | |
| 182 | case DISALLOW_VALUES: |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 183 | DisallowValues disallowValues = flagPolicy.policy.getDisallowValues(); |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 184 | FilterValueOperation.DisallowValueOperation disallowValueOperation = |
| 185 | new FilterValueOperation.DisallowValueOperation(loglevel); |
| 186 | disallowValueOperation.apply( |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 187 | parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 188 | flagPolicy.origin, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 189 | disallowValues.getDisallowedValuesList(), |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 190 | disallowValues.hasNewValue() ? disallowValues.getNewValue() : null, |
| 191 | disallowValues.hasUseDefault(), |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 192 | valueDescription, |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 193 | flagPolicy.description); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | case OPERATION_NOT_SET: |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 197 | throw new PolicyOperationNotSetException(flagName); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 198 | |
| 199 | default: |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 200 | logger.atWarning().log( |
| 201 | "Unknown operation '%s' from invocation policy for flag '%s'", |
| 202 | flagPolicy.policy.getOperationCase(), flagName); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 208 | private static class PolicyOperationNotSetException extends OptionsParsingException { |
| 209 | PolicyOperationNotSetException(String flagName) { |
| 210 | super(String.format("Flag policy for flag '%s' does not " + "have an operation", flagName)); |
| 211 | } |
| 212 | } |
| 213 | |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 214 | private static boolean policyApplies(FlagPolicy policy, ImmutableSet<String> applicableCommands) { |
| 215 | // Skip the flag policy if it doesn't apply to this command. If the commands list is empty, |
| 216 | // then the policy applies to all commands. |
| 217 | if (policy.getCommandsList().isEmpty() || applicableCommands.isEmpty()) { |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return !Collections.disjoint(policy.getCommandsList(), applicableCommands); |
| 222 | } |
| 223 | |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 224 | /** Returns the expanded and filtered policy that would be enforced for the given command. */ |
| 225 | public static InvocationPolicy getEffectiveInvocationPolicy( |
| 226 | InvocationPolicy invocationPolicy, OptionsParser parser, String command, Level loglevel) |
| 227 | throws OptionsParsingException { |
| 228 | ImmutableList<FlagPolicyWithContext> effectivePolicies = |
| 229 | getEffectivePolicies(invocationPolicy, parser, command, loglevel); |
| 230 | |
| 231 | InvocationPolicy.Builder builder = InvocationPolicy.newBuilder(); |
| 232 | for (FlagPolicyWithContext policyWithContext : effectivePolicies) { |
| 233 | builder.addFlagPolicies(policyWithContext.policy); |
| 234 | } |
| 235 | return builder.build(); |
| 236 | } |
| 237 | |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 238 | /** |
| 239 | * Takes the provided policy and processes it to the form that can be used on the user options. |
| 240 | * |
| 241 | * <p>Expands any policies on expansion flags. |
| 242 | */ |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 243 | private static ImmutableList<FlagPolicyWithContext> getEffectivePolicies( |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 244 | InvocationPolicy invocationPolicy, OptionsParser parser, String command, Level loglevel) |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 245 | throws OptionsParsingException { |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 246 | if (invocationPolicy == null) { |
| 247 | return ImmutableList.of(); |
| 248 | } |
| 249 | |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 250 | ImmutableSet<String> commandAndParentCommands = |
| 251 | command == null |
Jonathan Bluett-Duncan | 0df3ddbd | 2017-08-09 11:13:54 +0200 | [diff] [blame] | 252 | ? ImmutableSet.of() |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 253 | : CommandNameCache.CommandNameCacheInstance.INSTANCE.get(command); |
| 254 | |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 255 | // Expand all policies to transfer policies on expansion flags to policies on the child flags. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 256 | List<FlagPolicyWithContext> expandedPolicies = new ArrayList<>(); |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 257 | OptionPriority nextPriority = |
| 258 | OptionPriority.lowestOptionPriorityAtCategory(PriorityCategory.INVOCATION_POLICY); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 259 | for (FlagPolicy policy : invocationPolicy.getFlagPoliciesList()) { |
ccalvarin | 9c8c775 | 2018-04-17 07:48:38 -0700 | [diff] [blame] | 260 | // Explicitly disallow --config in invocation policy. |
| 261 | if (policy.getFlagName().equals("config")) { |
| 262 | throw new OptionsParsingException( |
| 263 | "Invocation policy is applied after --config expansion, changing config values now " |
| 264 | + "would have no effect and is disallowed to prevent confusion. Please remove the " |
| 265 | + "following policy : " |
| 266 | + policy); |
| 267 | } |
| 268 | |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 269 | // These policies are high-level, before expansion, and so are not the implicitDependents or |
| 270 | // expansions of any other flag, other than in an obtuse sense from --invocation_policy. |
| 271 | OptionPriority currentPriority = nextPriority; |
| 272 | OptionInstanceOrigin origin = |
| 273 | new OptionInstanceOrigin(currentPriority, INVOCATION_POLICY_SOURCE, null, null); |
| 274 | nextPriority = OptionPriority.nextOptionPriority(currentPriority); |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 275 | if (!policyApplies(policy, commandAndParentCommands)) { |
| 276 | // Only keep and expand policies that are applicable to the current command. |
| 277 | continue; |
| 278 | } |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 279 | |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 280 | OptionDescription optionDescription = parser.getOptionDescription(policy.getFlagName()); |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 281 | if (optionDescription == null) { |
| 282 | // InvocationPolicy ignores policy on non-existing flags by design, for version |
| 283 | // compatibility. |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 284 | logger.at(loglevel).log( |
| 285 | "Flag '%s' specified by invocation policy does not exist, and will be ignored", |
| 286 | policy.getFlagName()); |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | FlagPolicyWithContext policyWithContext = |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 290 | new FlagPolicyWithContext(policy, optionDescription, origin); |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 291 | List<FlagPolicyWithContext> policies = expandPolicy(policyWithContext, parser, loglevel); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 292 | expandedPolicies.addAll(policies); |
| 293 | } |
| 294 | |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 295 | // Only keep that last policy for each flag. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 296 | Map<String, FlagPolicyWithContext> effectivePolicy = new HashMap<>(); |
| 297 | for (FlagPolicyWithContext expandedPolicy : expandedPolicies) { |
| 298 | String flagName = expandedPolicy.policy.getFlagName(); |
ccalvarin | efc9b63 | 2017-05-03 19:05:22 +0200 | [diff] [blame] | 299 | effectivePolicy.put(flagName, expandedPolicy); |
| 300 | } |
| 301 | |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 302 | return ImmutableList.copyOf(effectivePolicy.values()); |
| 303 | } |
| 304 | |
| 305 | private static void throwAllowValuesOnExpansionFlagException(String flagName) |
| 306 | throws OptionsParsingException { |
| 307 | throw new OptionsParsingException( |
| 308 | String.format("Allow_Values on expansion flags like %s is not allowed.", flagName)); |
| 309 | } |
| 310 | |
| 311 | private static void throwDisallowValuesOnExpansionFlagException(String flagName) |
| 312 | throws OptionsParsingException { |
| 313 | throw new OptionsParsingException( |
| 314 | String.format("Disallow_Values on expansion flags like %s is not allowed.", flagName)); |
| 315 | } |
| 316 | |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 317 | /** |
| 318 | * Expand a single policy. If the policy is not about an expansion flag, this will simply return a |
| 319 | * list with a single element, oneself. If the policy is for an expansion flag, the policy will |
| 320 | * get split into multiple policies applying to each flag the original flag expands to. |
| 321 | * |
| 322 | * <p>None of the flagPolicies returned should be on expansion flags. |
| 323 | */ |
ajurkowski | 6977067 | 2021-06-04 04:43:28 -0700 | [diff] [blame] | 324 | private static ImmutableList<FlagPolicyWithContext> expandPolicy( |
ccalvarin | 06e6874 | 2018-03-01 07:29:45 -0800 | [diff] [blame] | 325 | FlagPolicyWithContext originalPolicy, OptionsParser parser, Level loglevel) |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 326 | throws OptionsParsingException { |
ajurkowski | 6977067 | 2021-06-04 04:43:28 -0700 | [diff] [blame] | 327 | ImmutableList.Builder<FlagPolicyWithContext> expandedPolicies = ImmutableList.builder(); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 328 | |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 329 | boolean isExpansion = originalPolicy.description.isExpansion(); |
| 330 | ImmutableList<ParsedOptionDescription> subflags = |
| 331 | parser.getExpansionValueDescriptions( |
| 332 | originalPolicy.description.getOptionDefinition(), originalPolicy.origin); |
| 333 | |
| 334 | // If we have nothing to expand to, no need to do any further work. |
| 335 | if (subflags.isEmpty()) { |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 336 | return ImmutableList.of(originalPolicy); |
| 337 | } |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 338 | |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 339 | // Log the expansion. This is only really useful for understanding the invocation policy itself. |
| 340 | logger.at(loglevel).log( |
| 341 | "Expanding %s on option %s to its %s: %s.", |
| 342 | originalPolicy.policy.getOperationCase(), |
| 343 | originalPolicy.policy.getFlagName(), |
| 344 | isExpansion ? "expansions" : "implied flags", |
| 345 | LazyArgs.lazy( |
| 346 | () -> |
| 347 | subflags.stream() |
| 348 | .map(f -> "--" + f.getOptionDefinition().getOptionName()) |
| 349 | .collect(joining("; ")))); |
ccalvarin | 36109fc | 2017-04-05 15:18:51 +0000 | [diff] [blame] | 350 | |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 351 | // Repeated flags are special, and could set multiple times in an expansion, with the user |
| 352 | // expecting both values to be valid. Collect these separately. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 353 | Multimap<OptionDescription, ParsedOptionDescription> repeatableSubflagsInSetValues = |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 354 | ArrayListMultimap.create(); |
| 355 | |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 356 | // Create a flag policy for the child that looks like the parent's policy "transferred" to its |
| 357 | // child. Note that this only makes sense for SetValue, when setting an expansion flag, or |
| 358 | // UseDefault, when preventing it from being set. |
ccalvarin | fb153cd | 2017-09-15 19:29:50 +0200 | [diff] [blame] | 359 | for (ParsedOptionDescription currentSubflag : subflags) { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 360 | OptionDescription subflagOptionDescription = |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 361 | parser.getOptionDescription(currentSubflag.getOptionDefinition().getOptionName()); |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 362 | |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 363 | if (currentSubflag.getOptionDefinition().allowsMultiple() |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 364 | && originalPolicy.policy.getOperationCase().equals(OperationCase.SET_VALUE)) { |
| 365 | repeatableSubflagsInSetValues.put(subflagOptionDescription, currentSubflag); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 366 | } else { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 367 | FlagPolicyWithContext subflagAsPolicy = |
| 368 | getSingleValueSubflagAsPolicy( |
| 369 | subflagOptionDescription, currentSubflag, originalPolicy, isExpansion); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 370 | // In case any of the expanded flags are themselves expansions, recurse. |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 371 | expandedPolicies.addAll(expandPolicy(subflagAsPolicy, parser, loglevel)); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 372 | } |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 373 | } |
| 374 | |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 375 | // If there are any repeatable flag SetValues, deal with them together now. |
| 376 | // Note that expansion flags have no value, and so cannot have multiple values either. |
| 377 | // Skipping the recursion above is fine. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 378 | for (OptionDescription repeatableFlag : repeatableSubflagsInSetValues.keySet()) { |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 379 | int numValues = repeatableSubflagsInSetValues.get(repeatableFlag).size(); |
| 380 | ArrayList<String> newValues = new ArrayList<>(numValues); |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 381 | ArrayList<OptionInstanceOrigin> origins = new ArrayList<>(numValues); |
ccalvarin | fb153cd | 2017-09-15 19:29:50 +0200 | [diff] [blame] | 382 | for (ParsedOptionDescription setValue : repeatableSubflagsInSetValues.get(repeatableFlag)) { |
ccalvarin | a8c0c8d | 2017-09-14 16:54:39 +0200 | [diff] [blame] | 383 | newValues.add(setValue.getUnconvertedValue()); |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 384 | origins.add(setValue.getOrigin()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 385 | } |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 386 | // These options come from expanding a single policy, so they have effectively the same |
| 387 | // priority. They could have come from different expansions or implicit requirements in the |
| 388 | // recursive resolving of the option list, so just pick the first one. Do collapse the source |
| 389 | // strings though, in case there are different sources. |
| 390 | OptionInstanceOrigin arbitraryFirstOptionOrigin = origins.get(0); |
| 391 | OptionInstanceOrigin originOfSubflags = |
| 392 | new OptionInstanceOrigin( |
| 393 | arbitraryFirstOptionOrigin.getPriority(), |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 394 | origins.stream() |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 395 | .map(OptionInstanceOrigin::getSource) |
| 396 | .distinct() |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 397 | .collect(joining(", ")), |
ccalvarin | 34a9fea | 2017-10-17 23:27:19 +0200 | [diff] [blame] | 398 | arbitraryFirstOptionOrigin.getImplicitDependent(), |
| 399 | arbitraryFirstOptionOrigin.getExpandedFrom()); |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 400 | expandedPolicies.add( |
| 401 | getSetValueSubflagAsPolicy(repeatableFlag, newValues, originOfSubflags, originalPolicy)); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // Don't add the original policy if it was an expansion flag, which have no value, but do add |
| 405 | // it if there was either no expansion or if it was a valued flag with implicit requirements. |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 406 | if (!isExpansion) { |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 407 | expandedPolicies.add(originalPolicy); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 408 | } |
| 409 | |
ajurkowski | 6977067 | 2021-06-04 04:43:28 -0700 | [diff] [blame] | 410 | return expandedPolicies.build(); |
ccalvarin | 706bafe | 2017-03-30 17:34:04 +0000 | [diff] [blame] | 411 | } |
| 412 | |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 413 | /** |
| 414 | * Expand a SetValue flag policy on a repeatable flag. SetValue operations are the only flag |
| 415 | * policies that set the flag, and so interact with repeatable flags, flags that can be set |
| 416 | * multiple times, in subtle ways. |
| 417 | * |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 418 | * @param subflagDesc, the description of the flag the SetValue'd expansion flag expands to. |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 419 | * @param subflagValue, the values that the SetValue'd expansion flag expands to for this flag. |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 420 | * @param originalPolicy, the original policy on the expansion flag. |
| 421 | * @return the flag policy for the subflag given, this will be part of the expanded form of the |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 422 | * SetValue policy on the original flag. |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 423 | */ |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 424 | private static FlagPolicyWithContext getSetValueSubflagAsPolicy( |
| 425 | OptionDescription subflagDesc, |
| 426 | List<String> subflagValue, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 427 | OptionInstanceOrigin subflagOrigin, |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 428 | FlagPolicyWithContext originalPolicy) { |
Googler | 199e70c | 2020-10-08 19:57:27 -0700 | [diff] [blame] | 429 | // Some checks. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 430 | OptionDefinition subflag = subflagDesc.getOptionDefinition(); |
| 431 | Verify.verify(originalPolicy.policy.getOperationCase().equals(OperationCase.SET_VALUE)); |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 432 | if (!subflag.allowsMultiple()) { |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 433 | Verify.verify(subflagValue.size() <= 1); |
| 434 | } |
| 435 | |
| 436 | // Flag value from the expansion, overridability from the original policy, unless the flag is |
| 437 | // repeatable, in which case we care about appendability, not overridability. |
| 438 | SetValue.Builder setValueExpansion = SetValue.newBuilder(); |
| 439 | for (String value : subflagValue) { |
| 440 | setValueExpansion.addFlagValue(value); |
| 441 | } |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 442 | if (subflag.allowsMultiple()) { |
| 443 | setValueExpansion.setAppend(originalPolicy.policy.getSetValue().getOverridable()); |
| 444 | } else { |
| 445 | setValueExpansion.setOverridable(originalPolicy.policy.getSetValue().getOverridable()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Commands from the original policy, flag name of the expansion |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 449 | return new FlagPolicyWithContext( |
| 450 | FlagPolicy.newBuilder() |
| 451 | .addAllCommands(originalPolicy.policy.getCommandsList()) |
| 452 | .setFlagName(subflag.getOptionName()) |
| 453 | .setSetValue(setValueExpansion) |
| 454 | .build(), |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 455 | subflagDesc, |
| 456 | subflagOrigin); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * For an expansion flag in an invocation policy, each flag it expands to must be given a |
| 461 | * corresponding policy. |
| 462 | */ |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 463 | private static FlagPolicyWithContext getSingleValueSubflagAsPolicy( |
| 464 | OptionDescription subflagContext, |
| 465 | ParsedOptionDescription currentSubflag, |
| 466 | FlagPolicyWithContext originalPolicy, |
| 467 | boolean isExpansion) |
Googler | 7c7255e | 2017-06-27 20:05:20 +0200 | [diff] [blame] | 468 | throws OptionsParsingException { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 469 | FlagPolicyWithContext subflagAsPolicy = null; |
| 470 | switch (originalPolicy.policy.getOperationCase()) { |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 471 | case SET_VALUE: |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 472 | if (currentSubflag.getOptionDefinition().allowsMultiple()) { |
ccalvarin | c331f24 | 2017-08-03 02:12:48 +0200 | [diff] [blame] | 473 | throw new AssertionError( |
| 474 | "SetValue subflags with allowMultiple should have been dealt with separately and " |
| 475 | + "accumulated into a single FlagPolicy."); |
| 476 | } |
| 477 | // Accept null originalValueStrings, they are expected when the subflag is also an expansion |
| 478 | // flag. |
| 479 | List<String> subflagValue; |
ccalvarin | a8c0c8d | 2017-09-14 16:54:39 +0200 | [diff] [blame] | 480 | if (currentSubflag.getUnconvertedValue() == null) { |
ccalvarin | c331f24 | 2017-08-03 02:12:48 +0200 | [diff] [blame] | 481 | subflagValue = ImmutableList.of(); |
| 482 | } else { |
ccalvarin | a8c0c8d | 2017-09-14 16:54:39 +0200 | [diff] [blame] | 483 | subflagValue = ImmutableList.of(currentSubflag.getUnconvertedValue()); |
ccalvarin | c331f24 | 2017-08-03 02:12:48 +0200 | [diff] [blame] | 484 | } |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 485 | subflagAsPolicy = |
| 486 | getSetValueSubflagAsPolicy( |
| 487 | subflagContext, subflagValue, currentSubflag.getOrigin(), originalPolicy); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 488 | break; |
| 489 | |
| 490 | case USE_DEFAULT: |
| 491 | // Commands from the original policy, flag name of the expansion |
| 492 | subflagAsPolicy = |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 493 | new FlagPolicyWithContext( |
| 494 | FlagPolicy.newBuilder() |
| 495 | .addAllCommands(originalPolicy.policy.getCommandsList()) |
| 496 | .setFlagName(currentSubflag.getOptionDefinition().getOptionName()) |
| 497 | .setUseDefault(UseDefault.getDefaultInstance()) |
| 498 | .build(), |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 499 | subflagContext, |
| 500 | currentSubflag.getOrigin()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case ALLOW_VALUES: |
| 504 | if (isExpansion) { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 505 | throwAllowValuesOnExpansionFlagException(originalPolicy.policy.getFlagName()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 506 | } |
| 507 | // If this flag is an implicitRequirement, and some values for the parent flag are |
| 508 | // allowed, nothing needs to happen on the implicitRequirement that is set for all |
| 509 | // values of the flag. |
| 510 | break; |
| 511 | |
| 512 | case DISALLOW_VALUES: |
| 513 | if (isExpansion) { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 514 | throwDisallowValuesOnExpansionFlagException(originalPolicy.policy.getFlagName()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 515 | } |
| 516 | // If this flag is an implicitRequirement, and some values for the parent flag are |
| 517 | // disallowed, that implies that all others are allowed, so nothing needs to happen |
| 518 | // on the implicitRequirement that is set for all values of the parent flag. |
| 519 | break; |
| 520 | |
| 521 | case OPERATION_NOT_SET: |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 522 | throw new PolicyOperationNotSetException(originalPolicy.policy.getFlagName()); |
ccalvarin | ae1d0de | 2017-04-15 05:19:09 +0200 | [diff] [blame] | 523 | |
| 524 | default: |
| 525 | return null; |
| 526 | } |
| 527 | return subflagAsPolicy; |
| 528 | } |
| 529 | |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 530 | private static void applySetValueOperation( |
| 531 | OptionsParser parser, |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 532 | FlagPolicyWithContext flagPolicy, |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 533 | OptionValueDescription valueDescription, |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 534 | Level loglevel) |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 535 | throws OptionsParsingException { |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 536 | SetValue setValue = flagPolicy.policy.getSetValue(); |
| 537 | OptionDefinition optionDefinition = flagPolicy.description.getOptionDefinition(); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 538 | |
| 539 | // SetValue.flag_value must have at least 1 value. |
| 540 | if (setValue.getFlagValueCount() == 0) { |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 541 | throw new OptionsParsingException( |
| 542 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 543 | "SetValue operation from invocation policy for %s does not have a value", |
| 544 | optionDefinition)); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 545 | } |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 546 | |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 547 | // Flag must allow multiple values if multiple values are specified by the policy. |
ccalvarin | 0044349 | 2017-08-30 00:23:40 +0200 | [diff] [blame] | 548 | if (setValue.getFlagValueCount() > 1 |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 549 | && !flagPolicy.description.getOptionDefinition().allowsMultiple()) { |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 550 | throw new OptionsParsingException( |
| 551 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 552 | "SetValue operation from invocation policy sets multiple values for %s which " |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 553 | + "does not allow multiple values", |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 554 | optionDefinition)); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 555 | } |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 556 | |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 557 | if (setValue.getOverridable() && valueDescription != null) { |
| 558 | // The user set the value for the flag but the flag policy is overridable, so keep the user's |
| 559 | // value. |
| 560 | logger.at(loglevel).log( |
| 561 | "Keeping value '%s' from source '%s' for %s because the invocation policy specifying " |
| 562 | + "the value(s) '%s' is overridable", |
| 563 | valueDescription.getValue(), |
| 564 | valueDescription.getSourceString(), |
| 565 | optionDefinition, |
| 566 | setValue.getFlagValueList()); |
| 567 | } else { |
| 568 | |
| 569 | if (!setValue.getAppend()) { |
Alex Humesky | 0595007 | 2016-05-09 18:38:55 +0000 | [diff] [blame] | 570 | // Clear the value in case the flag is a repeated flag so that values don't accumulate. |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 571 | parser.clearValue(flagPolicy.description.getOptionDefinition()); |
ajurkowski | 6d7557a | 2021-10-15 07:48:21 -0700 | [diff] [blame] | 572 | } |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 573 | |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 574 | // Set all the flag values from the policy. |
| 575 | for (String flagValue : setValue.getFlagValueList()) { |
| 576 | if (valueDescription == null) { |
| 577 | logger.at(loglevel).log( |
| 578 | "Setting value for %s from invocation policy to '%s', overriding the default value " |
| 579 | + "'%s'", |
| 580 | optionDefinition, flagValue, optionDefinition.getDefaultValue()); |
| 581 | } else { |
| 582 | logger.at(loglevel).log( |
| 583 | "Setting value for %s from invocation policy to '%s', overriding value '%s' from " |
| 584 | + "'%s'", |
| 585 | optionDefinition, |
| 586 | flagValue, |
| 587 | valueDescription.getValue(), |
| 588 | valueDescription.getSourceString()); |
| 589 | } |
| 590 | |
ajurkowski | e88245d | 2021-06-10 18:00:38 -0700 | [diff] [blame] | 591 | parser.setOptionValueAtSpecificPriorityWithoutExpansion( |
| 592 | flagPolicy.origin, optionDefinition, flagValue); |
Googler | 4442bb9 | 2021-10-15 09:15:28 -0700 | [diff] [blame] | 593 | } |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 597 | private static void applyUseDefaultOperation( |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 598 | OptionsParser parser, String policyType, OptionDefinition option, Level loglevel) |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 599 | throws OptionsParsingException { |
| 600 | OptionValueDescription clearedValueDescription = parser.clearValue(option); |
ccalvarin | 0e02f53 | 2017-04-04 18:31:27 +0000 | [diff] [blame] | 601 | if (clearedValueDescription != null) { |
| 602 | // Log the removed value. |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 603 | String clearedFlagName = clearedValueDescription.getOptionDefinition().getOptionName(); |
ccalvarin | ca74482 | 2017-10-10 13:50:15 +0200 | [diff] [blame] | 604 | Object clearedFlagDefaultValue = |
| 605 | clearedValueDescription.getOptionDefinition().getDefaultValue(); |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 606 | logger.at(loglevel).log( |
| 607 | "Using default value '%s' for flag '%s' as specified by %s invocation policy, " |
| 608 | + "overriding original value '%s' from '%s'", |
| 609 | clearedFlagDefaultValue, |
| 610 | clearedFlagName, |
| 611 | policyType, |
| 612 | clearedValueDescription.getValue(), |
| 613 | clearedValueDescription.getSourceString()); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 617 | /** Checks the user's flag values against a filtering function. */ |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 618 | private abstract static class FilterValueOperation { |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 619 | |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 620 | private static final class AllowValueOperation extends FilterValueOperation { |
| 621 | AllowValueOperation(Level loglevel) { |
| 622 | super("Allow", loglevel); |
| 623 | } |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 624 | |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 625 | @Override |
| 626 | boolean isFlagValueAllowed(Set<Object> convertedPolicyValues, Object value) { |
| 627 | return convertedPolicyValues.contains(value); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | private static final class DisallowValueOperation extends FilterValueOperation { |
| 632 | DisallowValueOperation(Level loglevel) { |
| 633 | super("Disalllow", loglevel); |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | boolean isFlagValueAllowed(Set<Object> convertedPolicyValues, Object value) { |
| 638 | // In a disallow operation, the values that the flag policy specifies are not allowed, |
| 639 | // so the value is allowed if the set of policy values does not contain the current |
| 640 | // flag value. |
| 641 | return !convertedPolicyValues.contains(value); |
| 642 | } |
| 643 | } |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 644 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 645 | private final String policyType; |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 646 | private final Level loglevel; |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 647 | |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 648 | FilterValueOperation(String policyType, Level loglevel) { |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 649 | this.policyType = policyType; |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 650 | this.loglevel = loglevel; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 653 | /** |
| 654 | * Determines if the given value is allowed. |
| 655 | * |
| 656 | * @param convertedPolicyValues The values given from the FlagPolicy, converted to real objects. |
| 657 | * @param value The user value of the flag. |
| 658 | * @return True if the value should be allowed, false if it should not. |
| 659 | */ |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 660 | abstract boolean isFlagValueAllowed(Set<Object> convertedPolicyValues, Object value); |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 661 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 662 | void apply( |
| 663 | OptionsParser parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 664 | OptionInstanceOrigin origin, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 665 | List<String> policyValues, |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 666 | String newValue, |
| 667 | boolean useDefault, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 668 | OptionValueDescription valueDescription, |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 669 | OptionDescription optionDescription) |
| 670 | throws OptionsParsingException { |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 671 | OptionDefinition optionDefinition = optionDescription.getOptionDefinition(); |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 672 | // Convert all the allowed values from strings to real objects using the options' |
| 673 | // converters so that they can be checked for equality using real .equals() instead |
| 674 | // of string comparison. For example, "--foo=0", "--foo=false", "--nofoo", and "-f-" |
| 675 | // (if the option has an abbreviation) are all equal for boolean flags. Plus converters |
| 676 | // can be arbitrarily complex. |
Jonathan Bluett-Duncan | 0df3ddbd | 2017-08-09 11:13:54 +0200 | [diff] [blame] | 677 | Set<Object> convertedPolicyValues = new HashSet<>(); |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 678 | for (String value : policyValues) { |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 679 | Object convertedValue = optionDefinition.getConverter().convert(value); |
Alex Humesky | 6415587 | 2016-12-16 22:12:52 +0000 | [diff] [blame] | 680 | // Some converters return lists, and if the flag is a repeatable flag, the items in the |
| 681 | // list from the converter should be added, and not the list itself. Otherwise the items |
| 682 | // from invocation policy will be compared to lists, which will never work. |
| 683 | // See OptionsParserImpl.ParsedOptionEntry.addValue. |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 684 | if (optionDefinition.allowsMultiple() && convertedValue instanceof List<?>) { |
Alex Humesky | 6415587 | 2016-12-16 22:12:52 +0000 | [diff] [blame] | 685 | convertedPolicyValues.addAll((List<?>) convertedValue); |
| 686 | } else { |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 687 | convertedPolicyValues.add(optionDefinition.getConverter().convert(value)); |
Alex Humesky | 6415587 | 2016-12-16 22:12:52 +0000 | [diff] [blame] | 688 | } |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 691 | // Check that if the default value of the flag is disallowed by the policy, that the policy |
ccalvarin | 06e6874 | 2018-03-01 07:29:45 -0800 | [diff] [blame] | 692 | // does not also set use_default. Otherwise the default value would still be set if the |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 693 | // user uses a disallowed value. This doesn't apply to repeatable flags since the default |
ccalvarin | 06e6874 | 2018-03-01 07:29:45 -0800 | [diff] [blame] | 694 | // value for repeatable flags is always the empty list. It also doesn't apply to flags that |
| 695 | // are null by default, since these flags' default value is not parsed by the converter, so |
| 696 | // there is no guarantee that there exists an accepted user-input value that would also set |
| 697 | // the value to NULL. In these cases, we assume that "unset" is a distinct value that is |
| 698 | // always allowed. |
| 699 | if (!optionDescription.getOptionDefinition().allowsMultiple() |
| 700 | && !optionDescription.getOptionDefinition().isSpecialNullDefault()) { |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 701 | boolean defaultValueAllowed = |
ccalvarin | 0044349 | 2017-08-30 00:23:40 +0200 | [diff] [blame] | 702 | isFlagValueAllowed( |
| 703 | convertedPolicyValues, optionDescription.getOptionDefinition().getDefaultValue()); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 704 | if (!defaultValueAllowed && useDefault) { |
| 705 | throw new OptionsParsingException( |
| 706 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 707 | "%sValues policy disallows the default value '%s' for %s but also specifies to " |
| 708 | + "use the default value", |
| 709 | policyType, optionDefinition.getDefaultValue(), optionDefinition)); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 713 | if (valueDescription == null) { |
| 714 | // Nothing has set the value yet, so check that the default value from the flag's |
| 715 | // definition is allowed. The else case below (i.e. valueDescription is not null) checks for |
| 716 | // the flag allowing multiple values, however, flags that allow multiple values cannot have |
| 717 | // default values, and their value is always the empty list if they haven't been specified, |
| 718 | // which is why new_default_value is not a repeated field. |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 719 | checkDefaultValue( |
| 720 | parser, origin, optionDescription, policyValues, newValue, convertedPolicyValues); |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 721 | } else { |
| 722 | checkUserValue( |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 723 | parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 724 | origin, |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 725 | optionDescription, |
| 726 | valueDescription, |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 727 | policyValues, |
| 728 | newValue, |
| 729 | useDefault, |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 730 | convertedPolicyValues); |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 733 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 734 | void checkDefaultValue( |
| 735 | OptionsParser parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 736 | OptionInstanceOrigin origin, |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 737 | OptionDescription optionDescription, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 738 | List<String> policyValues, |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 739 | String newValue, |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 740 | Set<Object> convertedPolicyValues) |
| 741 | throws OptionsParsingException { |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 742 | |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 743 | OptionDefinition optionDefinition = optionDescription.getOptionDefinition(); |
ccalvarin | 06e6874 | 2018-03-01 07:29:45 -0800 | [diff] [blame] | 744 | if (optionDefinition.isSpecialNullDefault()) { |
| 745 | // Do nothing, the unset value by definition cannot be set. In option filtering operations, |
| 746 | // the value is being filtered, but the value that is `no value` passes any filter. |
| 747 | // Otherwise, there is no way to "usedefault" on one of these options that has no value by |
| 748 | // default. |
| 749 | } else if (!isFlagValueAllowed(convertedPolicyValues, optionDefinition.getDefaultValue())) { |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 750 | if (newValue != null) { |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 751 | // Use the default value from the policy, since the original default is not allowed |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 752 | logger.at(loglevel).log( |
| 753 | "Overriding default value '%s' for %s with value '%s' specified by invocation " |
| 754 | + "policy. %sed values are: %s", |
| 755 | optionDefinition.getDefaultValue(), |
| 756 | optionDefinition, |
| 757 | newValue, |
| 758 | policyType, |
| 759 | policyValues); |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 760 | parser.clearValue(optionDefinition); |
ajurkowski | e88245d | 2021-06-10 18:00:38 -0700 | [diff] [blame] | 761 | parser.setOptionValueAtSpecificPriorityWithoutExpansion( |
| 762 | origin, optionDefinition, newValue); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 763 | } else { |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 764 | // The operation disallows the default value, but doesn't supply a new value. |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 765 | throw new OptionsParsingException( |
| 766 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 767 | "Default flag value '%s' for %s is not allowed by invocation policy, but " |
ccalvarin | 3ab171a | 2017-09-19 16:36:49 +0200 | [diff] [blame] | 768 | + "the policy does not provide a new value. %sed values are: %s", |
ccalvarin | 0044349 | 2017-08-30 00:23:40 +0200 | [diff] [blame] | 769 | optionDescription.getOptionDefinition().getDefaultValue(), |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 770 | optionDefinition, |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 771 | policyType, |
| 772 | policyValues)); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 775 | } |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 776 | |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 777 | void checkUserValue( |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 778 | OptionsParser parser, |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 779 | OptionInstanceOrigin origin, |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 780 | OptionDescription optionDescription, |
| 781 | OptionValueDescription valueDescription, |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 782 | List<String> policyValues, |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 783 | String newValue, |
| 784 | boolean useDefault, |
Janak Ramakrishnan | b92c097 | 2016-03-23 16:47:13 +0000 | [diff] [blame] | 785 | Set<Object> convertedPolicyValues) |
| 786 | throws OptionsParsingException { |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 787 | OptionDefinition option = optionDescription.getOptionDefinition(); |
ccalvarin | 0044349 | 2017-08-30 00:23:40 +0200 | [diff] [blame] | 788 | if (optionDescription.getOptionDefinition().allowsMultiple()) { |
Alex Humesky | c0d2769 | 2016-02-03 00:52:04 +0000 | [diff] [blame] | 789 | // allowMultiple requires that the type of the option be List<T>, so cast from Object |
| 790 | // to List<?>. |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 791 | List<?> optionValues = (List<?>) valueDescription.getValue(); |
| 792 | for (Object value : optionValues) { |
| 793 | if (!isFlagValueAllowed(convertedPolicyValues, value)) { |
| 794 | if (useDefault) { |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 795 | applyUseDefaultOperation(parser, policyType + "Values", option, loglevel); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 796 | } else { |
| 797 | throw new OptionsParsingException( |
| 798 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 799 | "Flag value '%s' for %s is not allowed by invocation policy. %sed values " |
| 800 | + "are: %s", |
| 801 | value, option, policyType, policyValues)); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | } |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 805 | |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 806 | } else { |
| 807 | |
| 808 | if (!isFlagValueAllowed(convertedPolicyValues, valueDescription.getValue())) { |
| 809 | if (newValue != null) { |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 810 | logger.at(loglevel).log( |
| 811 | "Overriding disallowed value '%s' for %s with value '%s' " |
| 812 | + "specified by invocation policy. %sed values are: %s", |
| 813 | valueDescription.getValue(), option, newValue, policyType, policyValues); |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 814 | parser.clearValue(option); |
ajurkowski | e88245d | 2021-06-10 18:00:38 -0700 | [diff] [blame] | 815 | parser.setOptionValueAtSpecificPriorityWithoutExpansion(origin, option, newValue); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 816 | } else if (useDefault) { |
ccalvarin | 334d2f1 | 2017-10-05 16:39:42 +0200 | [diff] [blame] | 817 | applyUseDefaultOperation(parser, policyType + "Values", option, loglevel); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 818 | } else { |
| 819 | throw new OptionsParsingException( |
| 820 | String.format( |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 821 | "Flag value '%s' for %s is not allowed by invocation policy and the " |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 822 | + "policy does not specify a new value. %sed values are: %s", |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 823 | valueDescription.getValue(), option, policyType, policyValues)); |
Alex Humesky | b1f55c8 | 2016-10-25 01:06:39 +0000 | [diff] [blame] | 824 | } |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 829 | } |