Skip to content

Commit

Permalink
Merge pull request #42978 from ravinperera00/issue_42859
Browse files Browse the repository at this point in the history
Allow not providing values to selected configurable variables through CLI
  • Loading branch information
warunalakshitha authored Jul 22, 2024
2 parents 1b113f2 + 8380e37 commit 240ef31
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
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)) {
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"]

0 comments on commit 240ef31

Please sign in to comment.