Skip to content

Commit

Permalink
Expose options toml node locations in ToolContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ShammiL committed Feb 15, 2024
1 parent 45ab4cd commit fd24a64
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.projects.buildtools;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -20,6 +20,8 @@
import io.ballerina.projects.Package;
import io.ballerina.projects.PackageManifest;
import io.ballerina.toml.semantic.ast.TomlTableNode;
import io.ballerina.toml.semantic.ast.TopLevelNode;
import io.ballerina.toml.semantic.diagnostics.TomlNodeLocation;
import io.ballerina.tools.diagnostics.Diagnostic;

import java.nio.file.Path;
Expand All @@ -43,7 +45,7 @@ public class ToolContext {
private final String toolId;
private final String filePath;
private final String targetModule;
private final Map<String, Object> options;
private final Map<String, Option> options;
private final List<Diagnostic> diagnostics = new ArrayList<>();

ToolContext(Package currentPackage, String toolId, String filePath,
Expand Down Expand Up @@ -93,7 +95,7 @@ public String targetModule() {
*
* @return a map of the optional tool configurations.
*/
public Map<String, Object> options() {
public Map<String, Option> options() {
return this.options;
}

Expand Down Expand Up @@ -148,15 +150,48 @@ public void reportDiagnostic(Diagnostic diagnostic) {
diagnostics.add(diagnostic);
}

private Map<String, Object> getOptions(TomlTableNode optionsTable) {
Map<String, Object> options = new HashMap<>();
private Map<String, Option> getOptions(TomlTableNode optionsTable) {
Map<String, Option> options = new HashMap<>();
if (null == optionsTable) {
return options;
}
for (String option: optionsTable.entries().keySet()) {
options.put(option, optionsTable.entries().get(option).toNativeObject());
options.put(option, new Option(optionsTable.entries().get(option)));
}
return options;
}

/**
* Represents a single option Toml node in Ballerina.toml file.
*
* @since 2201.9.0
*/
public static class Option {
private final Object value;
private final TomlNodeLocation location;

public Option(TopLevelNode optionNode) {
this.value = optionNode.toNativeObject();
this.location = optionNode.location();
}

/**
* Returns the value of the option.
*
* @return the option value.
*/
public Object value() {
return value;

Check warning on line 184 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/buildtools/ToolContext.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/buildtools/ToolContext.java#L184

Added line #L184 was not covered by tests
}

/**
* Returns the location of the option node in Ballerina.toml.
*
* @return the option location.
*/
public TomlNodeLocation location() {
return location;

Check warning on line 193 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/buildtools/ToolContext.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/buildtools/ToolContext.java#L193

Added line #L193 was not covered by tests
}
}
}

0 comments on commit fd24a64

Please sign in to comment.