Skip to content

Commit

Permalink
Merge pull request #1 from gimantha/rpjayasekara-fix-#31701
Browse files Browse the repository at this point in the history
Rpjayasekara fix ballerina-platform#31701
  • Loading branch information
rpjayasekara authored Oct 22, 2021
2 parents c627bf3 + c09baa8 commit 1d017f1
Show file tree
Hide file tree
Showing 269 changed files with 32,677 additions and 2,990 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "misc/grammar/ballerina-grammar"]
path = misc/grammar/ballerina-grammar
url = https://github.com/ballerina-platform/ballerina-grammar.git
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ private Map<Identifier, GlobalVariable> createGlobalVariables(
}

// Determine the type information
boolean isAssignableToAny = typeSymbol.assignableTo(anyTypeSymbol);
boolean isAssignableToAny = typeSymbol.subtypeOf(anyTypeSymbol);
// Find the variable type
Set<Identifier> requiredImports = new HashSet<>();
String variableType = importsManager.extractImportsFromType(typeSymbol, requiredImports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function __stmts() returns any {
{{#lastStmts}}
// Statement evaluation
{{#statement}}
if (true) {
if (0 < 1) {
{{{code}}}
}
{{/statement}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ private static class StateMachine {
private static final char BACKSPACE = 0x0008;
private static final char FORMFEED = 0x000C;
private static final char QUOTES = '"';
private static final char SINGLE_QUOTES = '\'';
private static final char REV_SOL = '\\';
private static final char SOL = '/';
private static final char EOF = (char) -1;
Expand Down Expand Up @@ -206,7 +205,7 @@ private static boolean isWhitespace(char ch) {
}

private static void throwExpected(String... chars) throws JsonParserException {
throw new JsonParserException("expected " + String.join(" or ", chars));
throw new JsonParserException("expected '" + String.join("' or '", chars) + "'");
}

private void processLocation(char ch) {
Expand Down Expand Up @@ -328,7 +327,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
} else if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
sm.currentQuoteChar = ch;
state = STRING_VALUE_STATE;
} else if (ch == EOF) {
Expand Down Expand Up @@ -385,7 +384,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
for (; i < count; i++) {
ch = buff[i];
sm.processLocation(ch);
if (ch == QUOTES || ch == SINGLE_QUOTES) {
if (ch == QUOTES) {
state = FIELD_NAME_STATE;
sm.currentQuoteChar = ch;
} else if (StateMachine.isWhitespace(ch)) {
Expand Down Expand Up @@ -419,7 +418,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_ARRAY_ELEMENT_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down Expand Up @@ -455,7 +454,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
for (; i < count; i++) {
ch = buff[i];
sm.processLocation(ch);
if (ch == QUOTES || ch == SINGLE_QUOTES) {
if (ch == QUOTES) {
sm.currentQuoteChar = ch;
state = FIELD_NAME_STATE;
} else if (StateMachine.isWhitespace(ch)) {
Expand Down Expand Up @@ -487,7 +486,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_ARRAY_ELEMENT_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down Expand Up @@ -594,7 +593,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_FIELD_VALUE_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ private static class StateMachine {
private static final char BACKSPACE = 0x0008;
private static final char FORMFEED = 0x000C;
private static final char QUOTES = '"';
private static final char SINGLE_QUOTES = '\'';
private static final char REV_SOL = '\\';
private static final char SOL = '/';
private static final char EOF = (char) -1;
Expand Down Expand Up @@ -252,7 +251,7 @@ private static boolean isWhitespace(char ch) {
}

private static void throwExpected(String... chars) throws JsonParserException {
throw new JsonParserException("expected " + String.join(" or ", chars));
throw new JsonParserException("expected '" + String.join("' or '", chars) + "'");
}

private void processLocation(char ch) {
Expand Down Expand Up @@ -375,7 +374,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
} else if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
sm.currentQuoteChar = ch;
state = STRING_VALUE_STATE;
} else if (ch == EOF) {
Expand Down Expand Up @@ -431,7 +430,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
for (; i < count; i++) {
ch = buff[i];
sm.processLocation(ch);
if (ch == QUOTES || ch == SINGLE_QUOTES) {
if (ch == QUOTES) {
state = FIELD_NAME_STATE;
sm.currentQuoteChar = ch;
} else if (StateMachine.isWhitespace(ch)) {
Expand Down Expand Up @@ -465,7 +464,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_ARRAY_ELEMENT_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down Expand Up @@ -501,7 +500,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
for (; i < count; i++) {
ch = buff[i];
sm.processLocation(ch);
if (ch == QUOTES || ch == SINGLE_QUOTES) {
if (ch == QUOTES) {
sm.currentQuoteChar = ch;
state = FIELD_NAME_STATE;
} else if (StateMachine.isWhitespace(ch)) {
Expand Down Expand Up @@ -533,7 +532,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_ARRAY_ELEMENT_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down Expand Up @@ -640,7 +639,7 @@ public State transition(StateMachine sm, char[] buff, int i, int count) throws J
if (StateMachine.isWhitespace(ch)) {
state = this;
continue;
} else if (ch == QUOTES || ch == SINGLE_QUOTES) {
} else if (ch == QUOTES) {
state = STRING_FIELD_VALUE_STATE;
sm.currentQuoteChar = ch;
} else if (ch == '{') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.ballerina.runtime.internal.values.MappingInitialValueEntry;
import io.ballerina.runtime.internal.values.ReadOnlyUtils;
import io.ballerina.runtime.internal.values.TableValueImpl;
import io.ballerina.toml.semantic.TomlType;
import io.ballerina.toml.semantic.ast.TomlArrayValueNode;
import io.ballerina.toml.semantic.ast.TomlBasicValueNode;
import io.ballerina.toml.semantic.ast.TomlKeyValueNode;
Expand Down Expand Up @@ -147,8 +148,12 @@ private BArray getNonSimpleTypeArray(TomlNode tomlValue, ArrayType arrayType,
return getMapValueArray(tomlValue, arrayType, elementType);
case TypeTags.ANYDATA_TAG:
case TypeTags.UNION_TAG:
valueNode = ((TomlKeyValueNode) tomlValue).value();
return createArrayFromSimpleTomlValue((TomlArrayValueNode) valueNode, arrayType, elementType);
if (tomlValue.kind() == TomlType.TABLE_ARRAY) {
return getMapValueArray(tomlValue, arrayType, elementType);
} else {
valueNode = ((TomlKeyValueNode) tomlValue).value();
return createArrayFromSimpleTomlValue((TomlArrayValueNode) valueNode, arrayType, elementType);
}
default:
return getNonSimpleTypeArray(tomlValue, arrayType, ((IntersectionType) elementType).getEffectiveType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private void validateNonPrimitiveArray(TomlNode tomlValue, String variableName,
break;
case TypeTags.ANYDATA_TAG:
case TypeTags.UNION_TAG:
validateUnionValueArray(tomlValue, variableName, arrayType, elementType);
validateUnionValueArray(tomlValue, variableName, arrayType, (BUnionType) elementType);
break;
default:
Type effectiveType = ((IntersectionType) elementType).getEffectiveType();
Expand All @@ -648,7 +648,11 @@ private void validateMapValueArray(TomlNode tomlValue, String variableName, Arra
}

private void validateUnionValueArray(TomlNode tomlValue, String variableName, ArrayType arrayType,
Type elementType) {
BUnionType elementType) {
if (tomlValue.kind() == TomlType.TABLE_ARRAY) {
validateMapUnionArray((TomlTableArrayNode) tomlValue, variableName, arrayType, elementType);
return;
}
TomlValueNode valueNode = ((TomlKeyValueNode) tomlValue).value();
if (valueNode.kind() != getEffectiveTomlType(arrayType, variableName)) {
invalidTomlLines.add(tomlValue.location().lineRange());
Expand All @@ -659,6 +663,19 @@ private void validateUnionValueArray(TomlNode tomlValue, String variableName, Ar
validateArrayElements(variableName, ((TomlArrayValueNode) valueNode).elements(), elementType);
}

private void validateMapUnionArray(TomlTableArrayNode tomlValue, String variableName, ArrayType arrayType,
BUnionType elementType) {
if (!Utils.containsMapType(elementType.getMemberTypes())) {
invalidTomlLines.add(tomlValue.location().lineRange());
throw new ConfigException(CONFIG_INCOMPATIBLE_TYPE, getLineRange(tomlValue), variableName, arrayType,
getTomlTypeString(tomlValue));
}
visitedNodes.add(tomlValue);
for (TomlNode tomlValueNode : tomlValue.children()) {
validateUnionValue(tomlValueNode, variableName, elementType);
}
}

private void validatePrimitiveArray(TomlNode tomlValue, String variableName, ArrayType arrayType) {
if (tomlValue.kind() != getEffectiveTomlType(arrayType, variableName)) {
invalidTomlLines.add(tomlValue.location().lineRange());
Expand All @@ -669,8 +686,7 @@ private void validatePrimitiveArray(TomlNode tomlValue, String variableName, Arr
validateArrayElements(variableName, arrayList, arrayType.getElementType());
}

private void validateArrayElements(String variableName, List<TomlValueNode> arrayList,
Type elementType) {
private void validateArrayElements(String variableName, List<TomlValueNode> arrayList, Type elementType) {
int arraySize = arrayList.size();
for (int i = 0; i < arraySize; i++) {
String elementName = variableName + "[" + i + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,13 @@ private static boolean isMappingType(int typeTag) {
return typeTag == TypeTags.MAP_TAG || typeTag == TypeTags.RECORD_TYPE_TAG;
}

static boolean containsMapType(List<Type> memberTypes) {
for (Type type : memberTypes) {
if (isMappingType(getEffectiveType(type).getTag())) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,20 @@ public void testRestFieldInvalidType() {
new VariableKey[]{recordVar})), 1, 1);
}

@Test
public void testUnionMapInvalidType() {
UnionType unionType =
TypeCreator.createUnionType(List.of(PredefinedTypes.TYPE_INT, PredefinedTypes.TYPE_STRING),
true);
ArrayType arrayType = TypeCreator.createArrayType(unionType, true);
VariableKey arrayVar = new VariableKey(ROOT_MODULE, "arrayVar", new BIntersectionType(ROOT_MODULE,
new Type[]{arrayType, PredefinedTypes.TYPE_READONLY}, unionType, 1, true), true);
String error = "[UnionMapInvalidType.toml:(1:1,3:14)] configurable variable 'arrayVar' is expected to be of " +
"type '(int|string)', but found 'table'";
validateTomlProviderErrors("UnionMapInvalidType", error, Map.ofEntries(Map.entry(ROOT_MODULE,
new VariableKey[]{arrayVar})), 1, 0);
}

@Test(dataProvider = "union-ambiguity-provider")
public void testTomlValueAmbiguityForUnionType(String errorMsg, VariableKey variableKey) {
validateTomlProviderErrors("UnionAmbiguousType", errorMsg, Map.ofEntries(Map.entry(ROOT_MODULE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[arrayVar]]
age = 26
name = "Jack"
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ public void execute() {
return;
}

if (!ProjectUtils.validateUnderscoresOfName(moduleName)) {
CommandUtil.printError(errStream,
"invalid module name : '" + moduleName + "' :\n" +
ProjectUtils.getValidateUnderscoreError(moduleName, "Module"),
null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}

// Check if the module already exists
if (ProjectUtils.isModuleExist(projectPath, moduleName)) {
CommandUtil.printError(errStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.ballerina.toml.semantic.TomlType;
import io.ballerina.toml.semantic.ast.TomlTableNode;
import org.ballerinalang.toml.exceptions.SettingsTomlException;
import org.wso2.ballerinalang.util.RepoUtils;
import picocli.CommandLine;

import java.io.PrintStream;
Expand All @@ -49,7 +50,6 @@
import java.util.List;

import static io.ballerina.cli.cmd.Constants.BUILD_COMMAND;
import static io.ballerina.cli.utils.CentralUtils.readSettings;
import static io.ballerina.projects.internal.ManifestBuilder.getStringValueFromTomlTableNode;
import static io.ballerina.runtime.api.constants.RuntimeConstants.SYSTEM_PROP_BAL_DEBUG;
import static org.ballerinalang.test.runtime.util.TesterinaConstants.JACOCO_XML_FORMAT;
Expand All @@ -67,13 +67,15 @@ public class BuildCommand implements BLauncherCmd {
private final PrintStream errStream;
private boolean exitWhenFinish;
private boolean skipCopyLibsFromDist;
private Boolean skipTests;

public BuildCommand() {
this.projectPath = Paths.get(System.getProperty(ProjectConstants.USER_DIR));
this.outStream = System.out;
this.errStream = System.err;
this.exitWhenFinish = true;
this.skipCopyLibsFromDist = false;
this.skipTests = true;
}

public BuildCommand(Path projectPath, PrintStream outStream, PrintStream errStream, boolean dumpBuildTime) {
Expand Down Expand Up @@ -140,8 +142,8 @@ public BuildCommand(Path projectPath, PrintStream outStream, PrintStream errStre
"dependencies.")
private Boolean offline;

@CommandLine.Option(names = {"--skip-tests"}, description = "Skip test compilation and execution.")
private Boolean skipTests;
@CommandLine.Option(names = {"--with-tests"}, description = "Run test compilation and execution.")
private Boolean withTests;

@CommandLine.Parameters (arity = "0..1")
private final Path projectPath;
Expand All @@ -167,7 +169,7 @@ public BuildCommand(Path projectPath, PrintStream outStream, PrintStream errStre
@CommandLine.Option(names = "--debug", description = "run tests in remote debugging mode")
private String debugPort;

private static final String buildCmd = "bal build [-o <output>] [--offline] [--skip-tests] [--taint-check]\n" +
private static final String buildCmd = "bal build [-o <output>] [--offline] [--with-tests] [--taint-check]\n" +
" [<ballerina-file | package-path>]";

@CommandLine.Option(names = "--test-report", description = "enable test report generation")
Expand Down Expand Up @@ -224,6 +226,11 @@ public void execute() {
sticky = false;
}

// If withTests flag is not provided, we change the skipTests flag accordingly
if (withTests != null) {
this.skipTests = !withTests;
}

BuildOptions buildOptions = constructBuildOptions();

boolean isSingleFileBuild = false;
Expand Down Expand Up @@ -375,7 +382,7 @@ public void execute() {
}
// Validate Settings.toml file
try {
readSettings();
RepoUtils.readSettings();
} catch (SettingsTomlException e) {
this.outStream.println("warning: " + e.getMessage());
}
Expand Down Expand Up @@ -454,7 +461,7 @@ public void printLongDesc(StringBuilder out) {

@Override
public void printUsage(StringBuilder out) {
out.append(" bal build [-o <output>] [--offline] [--skip-tests]\\n\" +\n" +
out.append(" bal build [-o <output>] [--offline] [--with-tests]\\n\" +\n" +
" \" [<ballerina-file | package-path>]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public void execute() {
return;
}

if (!ProjectUtils.validateUnderscoresOfName(packageName)) {
CommandUtil.printError(errStream,
"invalid package name : '" + packageName + "' :\n" +
ProjectUtils.getValidateUnderscoreError(packageName, "Package"),
null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}

if (!ProjectUtils.validatePackageName(packageName)) {
errStream.println("unallowed characters in the project name were replaced by " +
"underscores when deriving the package name. Edit the Ballerina.toml to change it.");
Expand Down
Loading

0 comments on commit 1d017f1

Please sign in to comment.