Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Jun 13, 2023
2 parents b91f512 + a46ec3a commit 1b70428
Show file tree
Hide file tree
Showing 150 changed files with 7,213 additions and 1,660 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@

import io.ballerina.runtime.internal.configurable.exceptions.ConfigException;
import io.ballerina.runtime.internal.util.exceptions.RuntimeErrors;
import io.ballerina.toml.api.Toml;
import io.ballerina.toml.semantic.ast.TomlTableNode;
import io.ballerina.toml.semantic.ast.TomlTransformer;
import io.ballerina.toml.syntax.tree.DocumentNode;
import io.ballerina.toml.syntax.tree.SyntaxTree;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.ballerina.tools.text.LineRange;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextDocuments;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.runtime.internal.configurable.providers.toml.TomlConstants.CONFIG_DATA_ENV_VARIABLE;
import static io.ballerina.runtime.internal.configurable.providers.toml.Utils.getOneBasedLineRange;
import static io.ballerina.runtime.internal.util.exceptions.RuntimeErrors.CONFIG_TOML_PARSE_FAILED;
import static io.ballerina.runtime.internal.util.exceptions.RuntimeErrors.CONFIG_TOML_READ_FAILED;

/**
Expand All @@ -45,28 +40,43 @@
* @since 2.0.0
*/
public class ConfigToml {
private final Path filePath;
private TextDocument textDocument;
private Path filePath;
private String content;
private TomlTableNode tomlAstNode;
private SyntaxTree syntaxTree;

protected ConfigToml(Path filePath) {
this.filePath = filePath;
}

protected ConfigToml(String content) {
this.content = content;
}

public TomlTableNode tomlAstNode() {
if (tomlAstNode != null) {
return tomlAstNode;
}
if (content != null) {
tomlAstNode = Toml.read(content, CONFIG_DATA_ENV_VARIABLE).rootNode();
} else {
tomlAstNode = getTomlRootFromFile();
}

parseToml();
List<Diagnostic> diagnosticList = getDiagnostics();
if (!diagnosticList.isEmpty()) {
throw new ConfigException(RuntimeErrors.CONFIG_TOML_INVALID_FILE, getErrorMessage(diagnosticList));
}
return tomlAstNode;
}

private TomlTableNode getTomlRootFromFile() {
try {
return Toml.read(filePath).rootNode();
} catch (IOException e) {
throw new ConfigException(CONFIG_TOML_READ_FAILED, filePath, e);
}
}

private String getErrorMessage(List<Diagnostic> diagnosticList) {
StringBuilder errorMessage = new StringBuilder("\n");
for (Diagnostic diagnostic : diagnosticList) {
Expand All @@ -77,39 +87,6 @@ private String getErrorMessage(List<Diagnostic> diagnosticList) {
return errorMessage.toString();
}

private TextDocument textDocument() {
if (textDocument != null) {
return textDocument;
}

try {
textDocument = TextDocuments.from(Files.readString(filePath));
} catch (IOException e) {
throw new ConfigException(CONFIG_TOML_READ_FAILED, filePath, e);
}
return textDocument;
}

private void parseToml() {
TextDocument tomlDocument = textDocument();
try {
syntaxTree = SyntaxTree.from(tomlDocument, getFileName(filePath));
TomlTransformer nodeTransformer = new TomlTransformer();
tomlAstNode = (TomlTableNode) nodeTransformer.transform((DocumentNode) syntaxTree.rootNode());
} catch (RuntimeException e) {
// The toml parser throws runtime exceptions for some cases
throw new ConfigException(CONFIG_TOML_PARSE_FAILED, filePath, e);
}
}

private String getFileName(Path filePath) {
final Path fileNamePath = filePath.getFileName();
if (fileNamePath != null) {
return fileNamePath.toString();
}
return null;
}

private List<Diagnostic> getDiagnostics() {
return new ArrayList<>(tomlAstNode.diagnostics());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
package io.ballerina.runtime.internal.configurable.providers.toml;

import io.ballerina.runtime.api.Module;
import io.ballerina.toml.api.Toml;

import java.util.Set;
import java.util.regex.Pattern;

import static io.ballerina.runtime.internal.configurable.providers.toml.TomlConstants.CONFIG_DATA_ENV_VARIABLE;

/**
* Toml parser that reads from text content for configurable implementation.
*
Expand Down Expand Up @@ -55,7 +52,7 @@ public void initialize() {
if (configContent.isEmpty()) {
return;
}
super.tomlNode = Toml.read(configContent, CONFIG_DATA_ENV_VARIABLE).rootNode();
super.tomlNode = new ConfigToml(configContent).tomlAstNode();
super.initialize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ public void testTomlProviderWithString() {
VariableKey booleanArr = new VariableKey(ROOT_MODULE, "booleanArr", new BIntersectionType(ROOT_MODULE,
new BType[]{}, TypeCreator.createArrayType(PredefinedTypes.TYPE_BOOLEAN), 0, false), true);
configVarMap.put(ROOT_MODULE, new VariableKey[]{intVar, stringVar, stringArr, booleanArr});
String tomlContent = "[rootOrg.test_module] intVar = 33 stringVar = \"xyz\" " +
"stringArr = [\"aa\", \"bb\", \"cc\"] booleanArr = [false, true, true, false]";
String tomlContent = "[rootOrg.test_module]\nintVar = 33\nstringVar = \"xyz\"\n" +
"stringArr = [\"aa\", \"bb\", \"cc\"]\nbooleanArr = [false, true, true, false]";
ConfigResolver configResolver = new ConfigResolver(configVarMap, new RuntimeDiagnosticLog(),
List.of(new TomlContentProvider(ROOT_MODULE, tomlContent, configVarMap.keySet())));
Map<VariableKey, ConfigValue> configValueMap = configResolver.resolveConfigs();
Expand Down Expand Up @@ -753,7 +753,7 @@ public void testTupleDefaultableConfig() {
Map<VariableKey, ConfigValue> configValueMap = configResolver.resolveConfigs();

Object value = configValueMap.get(intVar).getValue();
Assert.assertEquals(12L, value);
Assert.assertEquals(value, 12L);
}

@Test(dataProvider = "array-size-tests")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,16 @@ private void validateTomlProviderErrors(String tomlFileName, String errorMsg,
public void testTomlProviderWithStringNegative() {
Map<Module, VariableKey[]> configVarMap = new HashMap<>();
VariableKey[] keys = getSimpleVariableKeys(ROOT_MODULE);
String tomlContent = "[rootOrg.test_module] intVar = 42.22 floatVar = 3 stringVar = 11";
String tomlContent = "[rootOrg.test_module]\nintVar = 42.22\nfloatVar = 3\nstringVar = 11";
configVarMap.put(ROOT_MODULE, keys);
RuntimeDiagnosticLog diagnosticLog = new RuntimeDiagnosticLog();
ConfigResolver configResolver = new ConfigResolver(configVarMap, diagnosticLog,
List.of(new TomlContentProvider(ROOT_MODULE, tomlContent, configVarMap.keySet())));
configResolver.resolveConfigs();
Assert.assertEquals(diagnosticLog.getErrorCount(), 3);
Assert.assertEquals(diagnosticLog.getDiagnosticList().get(0).toString(), "error: [BAL_CONFIG_DATA:(1:32,1:37)" +
Assert.assertEquals(diagnosticLog.getDiagnosticList().get(0).toString(), "error: [BAL_CONFIG_DATA:(2:10,2:15)" +
"] configurable variable 'intVar' is expected to be of type 'int', but found 'float'");
Assert.assertEquals(diagnosticLog.getDiagnosticList().get(1).toString(), "error: [BAL_CONFIG_DATA:(1:63,1:65)" +
Assert.assertEquals(diagnosticLog.getDiagnosticList().get(1).toString(), "error: [BAL_CONFIG_DATA:(4:13,4:15)" +
"] configurable variable 'stringVar' is expected to be of type 'string', but found 'int'");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public BuildCommand() {
@CommandLine.Option(names = "--enable-cache", description = "enable caches for the compilation", hidden = true)
private Boolean enableCache;

@CommandLine.Option(names = "--native", description = "enable native image generation")
@CommandLine.Option(names = "--graalvm", description = "enable native image generation")
private Boolean nativeImage;

public void execute() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public TestCommand() {
@CommandLine.Option(names = "--enable-cache", description = "enable caches for the compilation", hidden = true)
private Boolean enableCache;

@CommandLine.Option(names = "--native", description = "enable running test suite against native image")
@CommandLine.Option(names = "--graalvm", description = "enable running test suite against native image")
private Boolean nativeImage;

@CommandLine.Option(names = "--excludes", description = "option to exclude source files/folders from code coverage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ OPTIONS
--target-dir <path>
Target directory path.

--native
--graalvm
Generate a GraalVM native image. Native image generation is an
experimental feature which supports only a limited set of
functionality.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ OPTIONS
--test-report
Generate an HTML report containing the test results.

--native
--graalvm
Execute test cases using the GraalVM native image. Native image testing
is an experimental feature which supports only a limited set of
functionality.
Expand All @@ -98,7 +98,7 @@ EXAMPLES
$ bal test

Run all test cases using the GraalVM native image.
$ bal test --native
$ bal test --graalvm

List all the test groups in the current package.
$ bal test --list-groups
Expand All @@ -124,7 +124,7 @@ EXAMPLES
$ bal test main_test.bal

Run a standalone test file using the GraalVM native image.
$ bal test --native main_test.bal
$ bal test --graalvm main_test.bal

Run the tests and generate a test report.
$ bal test --test-report
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import java.util.stream.Stream;

import static io.ballerina.compiler.api.symbols.SymbolKind.CLASS_FIELD;
import static io.ballerina.compiler.api.symbols.SymbolKind.MODULE;
import static io.ballerina.compiler.api.symbols.SymbolKind.OBJECT_FIELD;
import static io.ballerina.compiler.api.symbols.SymbolKind.RECORD_FIELD;
import static io.ballerina.compiler.api.symbols.SymbolKind.TYPE;
Expand Down Expand Up @@ -569,7 +570,7 @@ private void addToCompiledSymbols(Set<Symbol> compiledSymbols, Scope.ScopeEntry
compiledSymbol = symbolFactory.getBCompiledSymbol(symbol, symbol.getOriginalName().getValue());
}

if (compiledSymbol == null || compiledSymbols.contains(compiledSymbol)) {
if (compiledSymbol == null || checkAndUpdateModuleSymbols(compiledSymbols, compiledSymbol, symbol)) {
return;
}

Expand All @@ -589,6 +590,27 @@ private void addToCompiledSymbols(Set<Symbol> compiledSymbols, Scope.ScopeEntry
addToCompiledSymbols(compiledSymbols, scopeEntry.next, cursorPos, name, symbolEnv, states, compUnitName);
}

private boolean checkAndUpdateModuleSymbols(Set<Symbol> compiledSymbols, Symbol evaluatingSymbol, BSymbol symbol) {
boolean symbolExists = compiledSymbols.contains(evaluatingSymbol);

if (!symbolExists) {
return false;
}

if (evaluatingSymbol.kind() != MODULE) {
return true;
}

// If the same module symbol, but without a module alias is already added, then it shall be removed to add
// the new symbol with the import alias.
if (((BPackageSymbol) symbol).importPrefix != null) {
compiledSymbols.remove(evaluatingSymbol);
return false;
}

return true;
}

private boolean isWithinCurrentWorker(long symbolEnvScopeOwnerFlags, SymbolEnv enclEnv, BSymbol symbol) {

if (Symbols.isFlagOn(symbolEnvScopeOwnerFlags, Flags.WORKER)
Expand Down
Loading

0 comments on commit 1b70428

Please sign in to comment.