Search in sources :

Example 1 with PathSpec

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;
}
Also used : DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) List(java.util.List) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap)

Example 2 with PathSpec

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);
}
Also used : Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) PathSpec(com.linkedin.data.schema.PathSpec) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) Test(org.testng.annotations.Test)

Example 3 with PathSpec

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 } };
}
Also used : IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) AutoValidationWithProjectionBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationWithProjectionBuilders) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) PathSpec(com.linkedin.data.schema.PathSpec) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) DataProvider(org.testng.annotations.DataProvider)

Example 4 with PathSpec

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);
    }
}
Also used : PathSpec(com.linkedin.data.schema.PathSpec)

Example 5 with PathSpec

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");
        }
    }
}
Also used : MaskMap(com.linkedin.data.schema.MaskMap) DataMap(com.linkedin.data.DataMap) List(java.util.List) Escaper(com.linkedin.data.transform.Escaper) FilterConstants(com.linkedin.data.transform.filter.FilterConstants) MaskComposition(com.linkedin.data.transform.filter.MaskComposition) Map(java.util.Map) PathSpec(com.linkedin.data.schema.PathSpec) DataComplexProcessor(com.linkedin.data.transform.DataComplexProcessor) Optional(java.util.Optional) HashMap(java.util.HashMap) DataProcessingException(com.linkedin.data.transform.DataProcessingException) Optional(java.util.Optional) MaskMap(com.linkedin.data.schema.MaskMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) HashMap(java.util.HashMap) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap)

Aggregations

PathSpec (com.linkedin.data.schema.PathSpec)101 Test (org.testng.annotations.Test)74 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)40 DataMap (com.linkedin.data.DataMap)31 HashSet (java.util.HashSet)16 RecordTemplate (com.linkedin.data.template.RecordTemplate)11 Map (java.util.Map)10 Set (java.util.Set)10 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)9 HashMap (java.util.HashMap)8 DataList (com.linkedin.data.DataList)7 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)7 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)7 List (java.util.List)7 ByteString (com.linkedin.data.ByteString)6 DataSchema (com.linkedin.data.schema.DataSchema)5 MaskOperation (com.linkedin.data.transform.filter.request.MaskOperation)5 Foo (com.linkedin.pegasus.generator.examples.Foo)5 RequestContext (com.linkedin.r2.message.RequestContext)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5