Search in sources :

Example 16 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class PackageJsonValidatorExtension method checkIsDependenciesSection.

/**
 * Checks whether the given {@code sectionValue} is a structurally valid package.json dependency section (including
 * the version constraints).
 */
private void checkIsDependenciesSection(JSONValue sectionValue) {
    if (!checkIsType(sectionValue, JSONPackage.Literals.JSON_OBJECT, "as list of dependencies")) {
        return;
    }
    final JSONObject dependenciesObject = (JSONObject) sectionValue;
    for (NameValuePair entry : dependenciesObject.getNameValuePairs()) {
        final JSONValue versionRequirement = entry.getValue();
        if (checkIsType(versionRequirement, JSONPackage.Literals.JSON_STRING_LITERAL, "as version specifier")) {
            JSONStringLiteral jsonStringVersionRequirement = (JSONStringLiteral) versionRequirement;
            String constraintValue = jsonStringVersionRequirement.getValue();
            validateSemver(jsonStringVersionRequirement, constraintValue);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 17 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONCodeActionService method createInstallNpmCommand.

private Command createInstallNpmCommand(Options options, Diagnostic diag) {
    EObject element = getEObject(options, diag);
    if (element instanceof NameValuePair) {
        NameValuePair pair = (NameValuePair) element;
        String projectName = pair.getName();
        String version = JSONModelUtils.asStringOrNull(pair.getValue());
        return new Command("Install npm package to workspace", INSTALL_NPM, Arrays.asList(projectName, version, options.getCodeActionParams().getTextDocument().getUri()));
    }
    return null;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) Command(org.eclipse.lsp4j.Command) EObject(org.eclipse.emf.ecore.EObject)

Example 18 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONModelUtils method setPath.

private static <V extends JSONValue> V setPath(JSONObject object, List<String> currentPath, List<String> fullPath, V value) {
    if (currentPath.size() == 0) {
        return null;
    }
    final String currentProperty = currentPath.get(0);
    final int pathLength = currentPath.size();
    // if we are at the end of the path
    if (pathLength == 1) {
        // set the value on 'object'
        setProperty(object, currentProperty, value);
        return value;
    }
    // obtain NameValuePair that matches the first segment in propertyPath
    final Optional<NameValuePair> pair = getNameValuePair(object, currentProperty);
    // if pair already exists
    if (pair.isPresent()) {
        final JSONValue pathValue = pair.get().getValue();
        // check whether the value is an object
        if (!(pathValue instanceof JSONObject)) {
            // if not, the property path is invalid
            throw new JSONPropertyPathException("Cannot resolve JSON property path further then " + fullPath.subList(0, fullPath.size() - pathLength).stream().collect(Collectors.joining(".")) + ". " + pathValue + " is not a JSONObject.", null);
        }
        // setPath recursively on the (object) value of the existing pair
        return setPath((JSONObject) pathValue, currentPath.subList(1, pathLength), fullPath, value);
    } else {
        // add new object name-value-pair for current property
        final JSONObject nextObject = addProperty(object, currentProperty, JSONFactory.eINSTANCE.createJSONObject());
        return setPath(nextObject, currentPath.subList(1, pathLength), fullPath, value);
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 19 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONModelUtils method createNameValuePair.

/**
 * Creates a new {@link NameValuePair}.
 */
public static NameValuePair createNameValuePair(String name, JSONValue value) {
    NameValuePair result = JSONFactory.eINSTANCE.createNameValuePair();
    result.setName(name);
    result.setValue(value);
    return result;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair)

Example 20 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONModelUtils method addProperty.

/**
 * Adds a new {@link NameValuePair} to the {@code object}, with given {@code name} and {@code value}.
 *
 * Does not check {@code object} for duplicate {@link NameValuePair} with the same name.
 *
 * @returns The newly set value.
 */
public static <V extends JSONValue> V addProperty(JSONObject object, String name, V value) {
    final NameValuePair nameValuePair = JSONFactory.eINSTANCE.createNameValuePair();
    nameValuePair.setName(name);
    nameValuePair.setValue(value);
    object.getNameValuePairs().add(nameValuePair);
    return value;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair)

Aggregations

NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)28 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)17 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 EObject (org.eclipse.emf.ecore.EObject)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)4 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)2 NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Check (org.eclipse.xtext.validation.Check)2 Optional (com.google.common.base.Optional)1