Search in sources :

Example 1 with AssocKeySchema

use of com.linkedin.restli.restspec.AssocKeySchema in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateAssociationKey.

private Map<String, AssocKeyTypeInfo> generateAssociationKey(JDefinedClass facadeClass, AssociationSchema associationSchema) throws JClassAlreadyExistsException {
    final JDefinedClass typesafeKeyClass = facadeClass._class(JMod.PUBLIC | JMod.STATIC, "Key");
    typesafeKeyClass._extends(CompoundKey.class);
    final Map<String, AssocKeyTypeInfo> assocKeyTypeInfos = new HashMap<String, AssocKeyTypeInfo>();
    for (AssocKeySchema assocKey : associationSchema.getAssocKeys()) {
        final String name = assocKey.getName();
        final JClass clazz = getJavaBindingType(assocKey.getType(), facadeClass).valueClass;
        final JClass declaredClass = getClassRefForSchema(RestSpecCodec.textToSchema(assocKey.getType(), _schemaResolver), facadeClass);
        final JMethod typesafeSetter = typesafeKeyClass.method(JMod.PUBLIC, typesafeKeyClass, "set" + RestLiToolsUtils.nameCapsCase(name));
        final JVar setterParam = typesafeSetter.param(clazz, name);
        typesafeSetter.body().add(JExpr.invoke("append").arg(JExpr.lit(name)).arg(setterParam));
        typesafeSetter.body()._return(JExpr._this());
        final JMethod typesafeGetter = typesafeKeyClass.method(JMod.PUBLIC, clazz, "get" + RestLiToolsUtils.nameCapsCase(name));
        typesafeGetter.body()._return(JExpr.cast(clazz, JExpr.invoke("getPart").arg(name)));
        assocKeyTypeInfos.put(name, new AssocKeyTypeInfo(clazz, declaredClass));
    }
    // default constructor
    typesafeKeyClass.constructor(JMod.PUBLIC);
    return assocKeyTypeInfos;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) HashMap(java.util.HashMap) JClass(com.sun.codemodel.JClass) JMethod(com.sun.codemodel.JMethod) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) JVar(com.sun.codemodel.JVar)

Example 2 with AssocKeySchema

use of com.linkedin.restli.restspec.AssocKeySchema in project rest.li by linkedin.

the class ResourceSchemaToResourceSpecTranslator method associationToResourceSpec.

private ResourceSpec associationToResourceSpec(ResourceSchema resourceSchema, AssociationSchema association) {
    ActionSchemaArray actions = null, entityActions = null;
    StringArray supports = association.getSupports();
    if (association.hasActions()) {
        actions = association.getActions();
    }
    if (association.getEntity().hasActions()) {
        entityActions = association.getEntity().getActions();
    }
    String schema = resourceSchema.getSchema();
    AssocKeySchemaArray assocKeys = association.getAssocKeys();
    Map<String, CompoundKey.TypeInfo> keyParts = new HashMap<String, CompoundKey.TypeInfo>();
    for (AssocKeySchema assocKey : assocKeys) {
        TypeSpec<?> type = toTypeSpec(RestSpecCodec.textToSchema(assocKey.getType(), _schemaResolver));
        keyParts.put(assocKey.getName(), new CompoundKey.TypeInfo(type, type));
    }
    return buildResourceSpec(supports, new TypeSpec<CompoundKey>(CompoundKey.class, null), null, keyParts, schema, actions, entityActions);
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) StringArray(com.linkedin.data.template.StringArray) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 3 with AssocKeySchema

use of com.linkedin.restli.restspec.AssocKeySchema in project rest.li by linkedin.

the class ResourceModelEncoder method appendKeys.

private void appendKeys(final AssociationSchema associationSchema, final ResourceModel collectionModel) {
    AssocKeySchemaArray assocKeySchemaArray = new AssocKeySchemaArray();
    List<Key> sortedKeys = new ArrayList<Key>(collectionModel.getKeys());
    Collections.sort(sortedKeys, new Comparator<Key>() {

        @Override
        public int compare(final Key o1, final Key o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (Key key : sortedKeys) {
        AssocKeySchema assocKeySchema = new AssocKeySchema();
        assocKeySchema.setName(key.getName());
        assocKeySchema.setType(buildDataSchemaType(key.getType(), key.getDataSchema()));
        assocKeySchemaArray.add(assocKeySchema);
    }
    associationSchema.setAssocKeys(assocKeySchemaArray);
    associationSchema.setIdentifier(collectionModel.getKeyName());
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) ArrayList(java.util.ArrayList) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) AlternativeKey(com.linkedin.restli.server.AlternativeKey) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 4 with AssocKeySchema

use of com.linkedin.restli.restspec.AssocKeySchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsAssocation.

private void findModelsAssocation(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    AssociationSchema association = resourceSchema.getAssociation();
    if (association != null) {
        for (AssocKeySchema assocKeySchema : association.getAssocKeys()) {
            String type = assocKeySchema.getType();
            recordType(type, foundTypes, typeOrder);
        }
        if (association.hasFinders()) {
            for (FinderSchema restMethodSchema : association.getFinders()) {
                findModelsFinder(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasMethods()) {
            for (RestMethodSchema restMethodSchema : association.getMethods()) {
                findModelsMethod(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasActions()) {
            for (ActionSchema actionSchema : association.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasEntity()) {
            EntitySchema entitySchema = association.getEntity();
            findModelsEntity(entitySchema, foundTypes, typeOrder);
        }
    }
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 5 with AssocKeySchema

use of com.linkedin.restli.restspec.AssocKeySchema in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailAssociationFile.

@Test
public void testFailAssociationFile() throws IOException {
    final AssocKeySchema prevAssocKey = new AssocKeySchema();
    prevAssocKey.setName("key1");
    prevAssocKey.setType("string");
    final Collection<CompatibilityInfo> testErrors = new HashSet<CompatibilityInfo>();
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "assocKeys"), CompatibilityInfo.Type.ARRAY_NOT_EQUAL, new AssocKeySchemaArray(Arrays.asList(prevAssocKey))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("create", "get"))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods", "create", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "data"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "get"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "entity", "path"), CompatibilityInfo.Type.VALUE_NOT_EQUAL, "/greetings/assoc/{greetingsId}", "/greetings/association/{greetingsId}"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_ASSOC_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_ASSOC_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, prevSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> incompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getIncompatibles());
    for (CompatibilityInfo te : testErrors) {
        Assert.assertTrue(incompatibles.contains(te), "Reported incompatibles should contain: " + te.toString());
        incompatibles.remove(te);
    }
    Assert.assertTrue(incompatibles.isEmpty());
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

AssocKeySchema (com.linkedin.restli.restspec.AssocKeySchema)6 StringArray (com.linkedin.data.template.StringArray)3 AssocKeySchemaArray (com.linkedin.restli.restspec.AssocKeySchemaArray)3 ActionSchema (com.linkedin.restli.restspec.ActionSchema)2 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)2 FinderSchema (com.linkedin.restli.restspec.FinderSchema)2 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)2 HashMap (java.util.HashMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 CompoundKey (com.linkedin.restli.common.CompoundKey)1 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)1 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)1 EntitySchema (com.linkedin.restli.restspec.EntitySchema)1