Search in sources :

Example 1 with RootBuilderMethodSpec

use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateActions.

private List<RootBuilderMethodSpec> generateActions(RootBuilderSpec rootBuilderSpec, ActionSchemaArray actions, String keyClass, String resourceName, List<String> pathKeys, Map<String, String> pathKeyTypes) {
    List<RootBuilderMethodSpec> actionSpecList = new ArrayList<>();
    if (actions != null) {
        for (ActionSchema action : actions) {
            RootBuilderMethodSpec actionSpec = generateActionMethod(rootBuilderSpec, keyClass, action, resourceName, pathKeys, pathKeyTypes);
            actionSpecList.add(actionSpec);
        }
    }
    return actionSpecList;
}
Also used : RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) ArrayList(java.util.ArrayList) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 2 with RootBuilderMethodSpec

use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateRootRequestBuilder.

private RootBuilderSpec generateRootRequestBuilder(RootBuilderSpec parentRootBuilder, ResourceSchema resource, String sourceFile, Map<String, String> pathKeyTypes) throws IOException {
    ValidationResult validationResult = ValidateDataAgainstSchema.validate(resource.data(), resource.schema(), new ValidationOptions());
    if (!validationResult.isValid()) {
        throw new IllegalArgumentException(String.format("Resource validation error.  Resource File '%s', Error Details '%s'", sourceFile, validationResult.toString()));
    }
    String packageName = resource.getNamespace();
    String resourceName = CodeUtil.capitalize(resource.getName());
    String className;
    if (_version == RestliVersion.RESTLI_2_0_0) {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_2_0_0, null, resource.getName(), true);
    } else {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_1_0_0, null, resource.getName(), true);
    }
    RootBuilderSpec rootBuilderSpec = null;
    if (resource.hasCollection()) {
        rootBuilderSpec = new CollectionRootBuilderSpec(resource);
    } else if (resource.hasSimple()) {
        rootBuilderSpec = new SimpleRootBuilderSpec(resource);
    } else if (resource.hasActionsSet()) {
        rootBuilderSpec = new ActionSetRootBuilderSpec(resource);
    } else {
        log.warn("Ignoring unsupported association resource: " + resourceName);
        return null;
    }
    rootBuilderSpec.setNamespace(packageName);
    rootBuilderSpec.setClassName(className);
    if (_version == RestliVersion.RESTLI_2_0_0) {
        rootBuilderSpec.setBaseClassName("BuilderBase");
    }
    rootBuilderSpec.setSourceIdlName(sourceFile);
    String resourcePath = getResourcePath(resource.getPath());
    rootBuilderSpec.setResourcePath(resourcePath);
    List<String> pathKeys = getPathKeys(resourcePath);
    rootBuilderSpec.setPathKeys(pathKeys);
    rootBuilderSpec.setParentRootBuilder(parentRootBuilder);
    StringArray supportsList = null;
    RestMethodSchemaArray restMethods = null;
    FinderSchemaArray finders = null;
    ResourceSchemaArray subresources = null;
    ActionSchemaArray resourceActions = null;
    ActionSchemaArray entityActions = null;
    String keyClass = null;
    if (resource.getCollection() != null) {
        CollectionSchema collection = resource.getCollection();
        String keyName = collection.getIdentifier().getName();
        // Complex key is not supported
        keyClass = collection.getIdentifier().getType();
        pathKeyTypes.put(keyName, collection.getIdentifier().getType());
        supportsList = collection.getSupports();
        restMethods = collection.getMethods();
        finders = collection.getFinders();
        subresources = collection.getEntity().getSubresources();
        resourceActions = collection.getActions();
        entityActions = collection.getEntity().getActions();
    } else if (resource.getSimple() != null) {
        SimpleSchema simpleSchema = resource.getSimple();
        keyClass = "Void";
        supportsList = simpleSchema.getSupports();
        restMethods = simpleSchema.getMethods();
        subresources = simpleSchema.getEntity().getSubresources();
        resourceActions = simpleSchema.getActions();
    } else if (resource.getActionsSet() != null) {
        ActionsSetSchema actionsSet = resource.getActionsSet();
        resourceActions = actionsSet.getActions();
    }
    Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
    if (!supportedMethods.isEmpty()) {
        for (ResourceMethod resourceMethod : supportedMethods) {
            validateResourceMethod(resource, resourceName, resourceMethod);
        }
    }
    List<RootBuilderMethodSpec> restMethodSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> finderSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> resourceActionSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> entityActionSpecs = new ArrayList<>();
    List<RootBuilderSpec> subresourceSpecs = new ArrayList<>();
    String schemaClass = resource.getSchema();
    if (restMethods != null) {
        restMethodSpecs = generateBasicMethods(rootBuilderSpec, keyClass, schemaClass, supportedMethods, restMethods, resourceName, pathKeys, pathKeyTypes);
    }
    if (finders != null) {
        finderSpecs = generateFinders(rootBuilderSpec, finders, keyClass, schemaClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (resourceActions != null) {
        resourceActionSpecs = generateActions(rootBuilderSpec, resourceActions, keyClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (entityActions != null) {
        entityActionSpecs = generateActions(rootBuilderSpec, entityActions, keyClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (subresources != null) {
        subresourceSpecs = generateSubResources(sourceFile, rootBuilderSpec, subresources, pathKeyTypes);
    }
    // assign to rootBuilderClass
    if (rootBuilderSpec instanceof CollectionRootBuilderSpec) {
        CollectionRootBuilderSpec rootBuilder = (CollectionRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setRestMethods(restMethodSpecs);
        rootBuilder.setFinders(finderSpecs);
        rootBuilder.setResourceActions(resourceActionSpecs);
        rootBuilder.setEntityActions(entityActionSpecs);
        rootBuilder.setSubresources(subresourceSpecs);
    } else if (rootBuilderSpec instanceof SimpleRootBuilderSpec) {
        SimpleRootBuilderSpec rootBuilder = (SimpleRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setRestMethods(restMethodSpecs);
        rootBuilder.setResourceActions(resourceActionSpecs);
        rootBuilder.setSubresources(subresourceSpecs);
    } else if (rootBuilderSpec instanceof ActionSetRootBuilderSpec) {
        ActionSetRootBuilderSpec rootBuilder = (ActionSetRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setResourceActions(resourceActionSpecs);
    }
    registerBuilderSpec(rootBuilderSpec);
    return rootBuilderSpec;
}
Also used : RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) ArrayList(java.util.ArrayList) ResourceSchemaArray(com.linkedin.restli.restspec.ResourceSchemaArray) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ActionsSetSchema(com.linkedin.restli.restspec.ActionsSetSchema) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) StringArray(com.linkedin.data.template.StringArray) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 3 with RootBuilderMethodSpec

use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec in project rest.li by linkedin.

the class TestRequestBuilderSpecGenerator method testSimpleResource.

@Test
public void testSimpleResource() throws Exception {
    String idl = moduleDir + FS + IDLS_DIR + FS + "testSimple.restspec.json";
    Set<BuilderSpec> builderSpecs = generateBuilderSpec(new String[] { idl });
    Assert.assertNotNull(builderSpecs);
    Assert.assertTrue(builderSpecs.size() == 6);
    Map<String, String> methodMap = new HashMap<>();
    methodMap.put("get", "Gets the greeting.");
    methodMap.put("delete", "Deletes the greeting.");
    methodMap.put("update", "Updates the greeting.");
    for (BuilderSpec spec : builderSpecs) {
        Assert.assertTrue(spec instanceof RootBuilderSpec || spec instanceof RestMethodBuilderSpec);
        if (spec instanceof RootBuilderSpec) {
            Assert.assertTrue(spec instanceof SimpleRootBuilderSpec);
            SimpleRootBuilderSpec simpleSpec = (SimpleRootBuilderSpec) spec;
            if (simpleSpec.getResourcePath().indexOf('/') >= 0) {
                Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
                Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple/testSimpleSub");
                Assert.assertNotNull(simpleSpec.getRestMethods());
                Assert.assertTrue(simpleSpec.getRestMethods().size() == 1);
                Assert.assertEquals("get", simpleSpec.getRestMethods().get(0).getName());
                Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
                Assert.assertTrue(simpleSpec.getSubresources().isEmpty());
            } else {
                Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
                Assert.assertTrue(simpleSpec.getSubresources().size() == 1);
                Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
                Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple");
                Assert.assertNotNull(simpleSpec.getRestMethods());
                Assert.assertTrue(simpleSpec.getRestMethods().size() == 3);
                List<RootBuilderMethodSpec> restMethods = simpleSpec.getRestMethods();
                for (RootBuilderMethodSpec method : restMethods) {
                    Assert.assertTrue(method.getReturn() instanceof RestMethodBuilderSpec);
                    Assert.assertEquals(method.getReturn().getRootBuilderMethod(), method);
                    Assert.assertTrue(methodMap.containsKey(method.getName()));
                    Assert.assertEquals(methodMap.get(method.getName()), method.getDoc());
                }
            }
        } else if (spec instanceof RestMethodBuilderSpec) {
            RestMethodBuilderSpec builderSpec = (RestMethodBuilderSpec) spec;
            ResourceMethod method = builderSpec.getResourceMethod();
            Assert.assertTrue(methodMap.containsKey(method.toString()));
            Assert.assertFalse(builderSpec.hasBindingMethods());
        }
    }
}
Also used : HashMap(java.util.HashMap) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) RestMethodBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) BuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.BuilderSpec) RestMethodBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) ActionBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) FinderBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 4 with RootBuilderMethodSpec

use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateActionMethod.

private RootBuilderMethodSpec generateActionMethod(RootBuilderSpec rootBuilderSpec, String keyClass, ActionSchema action, String resourceName, List<String> pathKeys, Map<String, String> pathKeyTypes) {
    String actionName = action.getName();
    String returnType = action.getReturns();
    String actionBuilderClassName = CodeUtil.capitalize(resourceName) + "Do" + CodeUtil.capitalize(actionName) + getMethodBuilderSuffix();
    ActionBuilderSpec actionBuilderClass = generateActionRequestBuilder(rootBuilderSpec.getResource(), getBuilderBase(ResourceMethod.ACTION), keyClass, returnType, actionBuilderClassName, rootBuilderSpec.getNamespace(), action);
    if (action.hasParameters()) {
        generateActionParamBindingMethods(action.getParameters(), actionBuilderClass);
    }
    generatePathKeyBindingMethods(pathKeys, actionBuilderClass, pathKeyTypes);
    String actionMethodName = "action" + CodeUtil.capitalize(actionName);
    RootBuilderMethodSpec actionMethod = new RootBuilderMethodSpec(actionMethodName, action.getDoc(), actionBuilderClass, rootBuilderSpec);
    actionBuilderClass.setRootBuilderMethod(actionMethod);
    return actionMethod;
}
Also used : ActionBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec)

Example 5 with RootBuilderMethodSpec

use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateFinders.

private List<RootBuilderMethodSpec> generateFinders(RootBuilderSpec rootBuilderSpec, FinderSchemaArray finderSchemas, String keyClass, String valueClass, String resourceName, List<String> pathKeys, Map<String, String> pathKeyTypes) {
    List<RootBuilderMethodSpec> finderSpecList = new ArrayList<>();
    if (finderSchemas != null) {
        String baseBuilderClass = getBuilderBase(ResourceMethod.FINDER);
        for (FinderSchema finder : finderSchemas) {
            String finderName = finder.getName();
            String builderName = CodeUtil.capitalize(resourceName) + "FindBy" + CodeUtil.capitalize(finderName) + getMethodBuilderSuffix();
            FinderBuilderSpec finderBuilderClass = generateFinderRequestBuilder(rootBuilderSpec.getResource(), baseBuilderClass, keyClass, valueClass, builderName, rootBuilderSpec.getNamespace(), finderName, finder);
            generatePathKeyBindingMethods(pathKeys, finderBuilderClass, pathKeyTypes);
            if (finder.getParameters() != null) {
                generateQueryParamBindingMethods(finder.getParameters(), finderBuilderClass);
            }
            // process custom metadata
            if (finder.getMetadata() != null) {
                String metadataClass = finder.getMetadata().getType();
                ClassTemplateSpec metadataClassSpec = classToTemplateSpec(metadataClass);
                finderBuilderClass.setMetadataType(metadataClassSpec);
            }
            String finderMethod = "findBy" + CodeUtil.capitalize(finderName);
            RootBuilderMethodSpec methodSpec = new RootBuilderMethodSpec(finderMethod, finder.getDoc(), finderBuilderClass, rootBuilderSpec);
            finderBuilderClass.setRootBuilderMethod(methodSpec);
            finderSpecList.add(methodSpec);
        }
    }
    return finderSpecList;
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) ArrayList(java.util.ArrayList) FinderSchema(com.linkedin.restli.restspec.FinderSchema) FinderBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec)

Aggregations

RootBuilderMethodSpec (com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec)8 ArrayList (java.util.ArrayList)6 ActionBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec)4 ActionSetRootBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec)4 CollectionRootBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec)4 FinderBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec)4 RestMethodBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec)4 RootBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec)4 SimpleRootBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec)4 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3 BuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.BuilderSpec)3 Test (org.testng.annotations.Test)3 HashMap (java.util.HashMap)2 DataMap (com.linkedin.data.DataMap)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 StringArray (com.linkedin.data.template.StringArray)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 ActionSchema (com.linkedin.restli.restspec.ActionSchema)1 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)1