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;
}
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);
}
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());
}
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);
}
}
}
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());
}
Aggregations