Skip to content
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

Allow not providing values to selected configurable variables through CLI #42978

Merged
merged 11 commits into from
Jul 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public static Object getFiniteBalValue(String strValue, BFiniteType finiteType,

public static boolean containsUnsupportedMembers(BUnionType unionType) {
for (Type memberType : unionType.getMemberTypes()) {
if (!isSimpleSequenceType(TypeUtils.getImpliedType(memberType).getTag())) {
if (!isPrimitiveOrSequenceType(TypeUtils.getImpliedType(memberType).getTag())) {
return true;
}
}
return false;
}

private static boolean isSimpleSequenceType(int tag) {
return tag <= TypeTags.BOOLEAN_TAG || TypeTags.isXMLTypeTag(tag);
private static boolean isPrimitiveOrSequenceType(int tag) {
return tag == TypeTags.NULL_TAG || tag <= TypeTags.BOOLEAN_TAG || TypeTags.isXMLTypeTag(tag);
}

public static Object getUnionValue(VariableKey key, BUnionType unionType, String value, String arg) {
Expand All @@ -104,6 +104,8 @@ private static List<Object> getConvertibleMemberValues(String value, UnionType u
List<Object> matchingValues = new ArrayList<>();
for (Type type : unionType.getMemberTypes()) {
switch (TypeUtils.getImpliedType(type).getTag()) {
case TypeTags.NULL_TAG:
break;
case TypeTags.BYTE_TAG:
convertAndGetValuesFromString(matchingValues, TypeConverter::stringToByte, value);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ public Optional<ConfigValue> getAsUnionAndMark(Module module, VariableKey key) {
CliArg cliArg = getCliArg(module, key);
BUnionType unionType = (BUnionType) ((BIntersectionType) key.type).getEffectiveType();
boolean isEnum = SymbolFlags.isFlagOn(unionType.getFlags(), SymbolFlags.ENUM);
if (!isEnum && ConfigUtils.containsUnsupportedMembers(unionType)) {
throw new ConfigException(CONFIG_CLI_TYPE_NOT_SUPPORTED, key.variable, unionType);
}
if (cliArg.value == null) {
return Optional.empty();
}
if (!isEnum && ConfigUtils.containsUnsupportedMembers(unionType)) {
ravinperera00 marked this conversation as resolved.
Show resolved Hide resolved
throw new ConfigException(CONFIG_CLI_TYPE_NOT_SUPPORTED, key.variable, unionType);
}
if (isEnum) {
return getCliConfigValue(ConfigUtils.getFiniteValue(key, unionType, cliArg.value, cliArg.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,19 @@ public Object[][] unionDataProvider() {
PredefinedTypes.TYPE_DECIMAL), true);
UnionType floatBoolen = TypeCreator.createUnionType(List.of(PredefinedTypes.TYPE_FLOAT,
PredefinedTypes.TYPE_BOOLEAN), true);
UnionType intNull = TypeCreator.createUnionType(List.of(PredefinedTypes.TYPE_INT,
PredefinedTypes.TYPE_NULL), true);
UnionType boolenNull = TypeCreator.createUnionType(List.of(PredefinedTypes.TYPE_BOOLEAN,
PredefinedTypes.TYPE_NULL), true);
return new Object[][]{
{"stringInt", getIntersectionType(ROOT_MODULE, stringInt), strVal, "-CstringInt=test"},
{"intFloat", getIntersectionType(ROOT_MODULE, intFloat), 2.2d, "-CintFloat=2.2"},
{"byteBoolean", getIntersectionType(ROOT_MODULE, byteBoolean), 222, "-CbyteBoolean=222"},
{"intBoolean", getIntersectionType(ROOT_MODULE, intBoolean), 2L, "-CintBoolean=2"},
{"intDecimal", getIntersectionType(ROOT_MODULE, intDecimal), decimalVal, "-CintDecimal=3.23"},
{"floatBoolen", getIntersectionType(ROOT_MODULE, floatBoolen), true, "-CfloatBoolen=true"},
{"intNull", getIntersectionType(ROOT_MODULE, intNull), 87L, "-CintNull=87"},
{"boolenNull", getIntersectionType(ROOT_MODULE, boolenNull), true, "-CboolenNull=true"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,23 @@ public void testEnvVarUnsupportedErrors(String variableName, Type type, String e
}

}

@Test
public void testCliWhenUnsupportedTypesWithinToml() {
ArrayType arrayType = TypeCreator.createArrayType(TYPE_STRING);
VariableKey v1 = new VariableKey(module, "v1", PredefinedTypes.TYPE_INT, true);
Type v2Type = new BIntersectionType(module, new Type[]{arrayType, PredefinedTypes.TYPE_READONLY},
arrayType, 0, true);
VariableKey v2 = new VariableKey(module, "v2", v2Type, true);
RuntimeDiagnosticLog diagnosticLog = new RuntimeDiagnosticLog();
ConfigResolver configResolver =
new ConfigResolver(Map.ofEntries(Map.entry(module, new VariableKey[]{v1, v2})), diagnosticLog,
List.of(new CliProvider(ROOT_MODULE, "-CmyOrg.test_module.v1=87"),
new TomlFileProvider(ROOT_MODULE,
getConfigPath("UnsupportedCLITypeConfig.toml"), Set.of(module))));
Map<VariableKey, ConfigValue> configValueMap = configResolver.resolveConfigs();
Assert.assertEquals(configValueMap.get(v1).getValue(), 87L);
Assert.assertEquals(configValueMap.get(v2).getValue().toString(), "[\"hello\",\"world\"]");
Assert.assertEquals(0, diagnosticLog.getErrorCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[myOrg.test_module]
v2 = ["hello", "world"]
Loading