use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class QueryParamsUtil method convertToDataMap.
/**
* Converts a String -> Object based representation of query params into a {@link DataMap}
* @param queryParams
* @param queryParamClasses
* @param version
* @return
*/
public static DataMap convertToDataMap(Map<String, Object> queryParams, Map<String, Class<?>> queryParamClasses, ProtocolVersion version) {
DataMap result = new DataMap(queryParams.size());
for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (RestConstants.PROJECTION_PARAMETERS.contains(key)) {
@SuppressWarnings("unchecked") List<PathSpec> pathSpecs = (List<PathSpec>) value;
result.put(key, MaskCreator.createPositiveMask(pathSpecs).getDataMap());
} else {
result.put(key, paramToDataObject(value, queryParamClasses.get(key), version));
}
}
result.makeReadOnly();
return result;
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class TestCreateResponseBuilder method testProjectionInBuildRestliResponseData.
@Test
public void testProjectionInBuildRestliResponseData() throws URISyntaxException {
MaskTree maskTree = new MaskTree();
maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.replay(mockContext);
RoutingResult routingResult = new RoutingResult(mockContext, null);
Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
CreateKVResponse<Integer, Foo> values = new CreateKVResponse<Integer, Foo>(null, value);
CreateResponseBuilder responseBuilder = new CreateResponseBuilder();
RestLiResponseData envelope = responseBuilder.buildRestLiResponseData(new RestRequestBuilder(new URI("/foo")).build(), routingResult, values, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
RecordTemplate record = envelope.getRecordResponseEnvelope().getRecord();
Assert.assertEquals(record.data().size(), 1);
Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
Assert.assertTrue(envelope.getCreateResponseEnvelope().isGetAfterCreate());
EasyMock.verify(mockContext);
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class TestResLiValidationWithProjection method provideProjectionWithValidFieldsBuilders.
@DataProvider
private Object[][] provideProjectionWithValidFieldsBuilders() throws DataProcessingException {
List<PathSpec> spec = Arrays.asList(ValidationDemo.fields().stringB(), ValidationDemo.fields().includedB(), ValidationDemo.fields().UnionFieldWithInlineRecord().MyRecord().foo2(), ValidationDemo.fields().ArrayWithInlineRecord().items().bar1(), ValidationDemo.fields().MapWithTyperefs().values().id(), ValidationDemo.fields().validationDemoNext().intB());
RootBuilderWrapper<Integer, ValidationDemo> wrapper = new RootBuilderWrapper<Integer, ValidationDemo>(new AutoValidationWithProjectionBuilders());
Request<CollectionResponse<ValidationDemo>> findRequest = wrapper.findBy("searchWithProjection").fields(spec.toArray(new PathSpec[spec.size()])).build();
Request<ValidationDemo> getRequest = wrapper.get().id(1).fields(spec.toArray(new PathSpec[spec.size()])).build();
Request<CollectionResponse<ValidationDemo>> getAllRequest = wrapper.getAll().fields(spec.toArray(new PathSpec[spec.size()])).build();
// Valid input for CreateAndGet
ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
unionField.setMyEnum(myEnum.FOOFOO);
ValidationDemo validDemo = new ValidationDemo().setStringB("b").setUnionFieldWithInlineRecord(unionField);
Request<IdEntityResponse<Integer, ValidationDemo>> createAndGetRequest = wrapper.createAndGet().input(validDemo).fields(spec.toArray(new PathSpec[spec.size()])).build();
Request<BatchCreateIdEntityResponse<Integer, ValidationDemo>> batchCreateAndGetRequest = wrapper.batchCreateAndGet().inputs(Arrays.asList(validDemo)).fields(spec.toArray(new PathSpec[spec.size()])).build();
return new Object[][] { { findRequest, HttpStatus.S_200_OK }, { getRequest, HttpStatus.S_200_OK }, { getAllRequest, HttpStatus.S_200_OK }, { createAndGetRequest, HttpStatus.S_201_CREATED }, { batchCreateAndGetRequest, HttpStatus.S_200_OK } };
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class MaskCreator method createPositiveMask.
/**
* Create a positive mask for the given set.
*
* @param pathSpecSet the set that should be in the mask
* @return a {@link MaskTree}
*/
public static MaskTree createPositiveMask(PathSpecSet pathSpecSet) {
if (pathSpecSet.isAllInclusive()) {
MaskTree maskTree = new MaskTree();
maskTree.addOperation(new PathSpec(PathSpec.WILDCARD), MaskOperation.POSITIVE_MASK_OP);
return maskTree;
} else {
return createMaskTree(pathSpecSet.getPathSpecs(), MaskOperation.POSITIVE_MASK_OP);
}
}
use of com.linkedin.data.schema.PathSpec in project rest.li by linkedin.
the class MaskTree method getOperationsImpl.
private void getOperationsImpl(DataMap data, PathSpec path, Map<PathSpec, MaskOperation> result) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
String segment = Escaper.unescapePathSegment(entry.getKey());
// Ignore if the segment is $start or $count, as we have already taken care of the array ranges
if (FilterConstants.START.equals(segment) || FilterConstants.COUNT.equals(segment)) {
continue;
}
PathSpec subpath = new PathSpec(path.getPathComponents(), segment);
Object value = entry.getValue();
if (value instanceof Integer) {
if (value.equals(MaskOperation.NEGATIVE_MASK_OP.getRepresentation())) {
result.put(subpath, MaskOperation.NEGATIVE_MASK_OP);
} else if (value.equals(MaskOperation.POSITIVE_MASK_OP.getRepresentation())) {
result.put(subpath, MaskOperation.POSITIVE_MASK_OP);
} else {
throw new IllegalStateException("invalid mask tree");
}
} else if (value.getClass() == DataMap.class) {
DataMap subMask = (DataMap) value;
Optional<PathSpec> pathWithAttributes = addArrayRangeAttributes(subMask, subpath);
pathWithAttributes.ifPresent(p -> result.put(p, MaskOperation.POSITIVE_MASK_OP));
getOperationsImpl(subMask, subpath, result);
} else {
throw new IllegalStateException("invalid mask tree");
}
}
}
Aggregations