/** * This updates the state of the task. Note that we are merging information from the PATCH into * the current task. Because we are merging into the current task (it's the same object), we do * not need to explicitly save the state: that will happen when we call patch.complete() */ protected void updateState(T currentTask, T patchBody) { Utils.mergeWithState(getStateDescription(), currentTask, patchBody); // NOTE: If patchBody provides a new expiration, Utils.mergeWithState will take care of it }
private State applyPatch(State patchState, State currentState) { Utils.mergeWithState(getStateDescription(), currentState, patchState); currentState.latestSourceUpdateTimeMicros = Math.max( Optional.ofNullable(currentState.latestSourceUpdateTimeMicros).orElse(0L), Optional.ofNullable(patchState.latestSourceUpdateTimeMicros).orElse(0L)); return currentState; }
/** * Performs automatic task state merge based on state annotations. */ protected void autoMergeState(Operation patch, T patchBody, T currentState) { // use default merging for AUTO_MERGE_IF_NOT_NULL fields Utils.mergeWithState(getStateDescription(), currentState, patchBody); }
@Override public void handlePatch(Operation patch) { LocalFileServiceState currentTask = getState(patch); LocalFileServiceState patchBody = getBody(patch); Utils.mergeWithState(getStateDescription(), currentTask, patchBody); patch.complete(); } }
@Override public void handlePatch(Operation patch) { Employee state = getState(patch); Employee patchBody = getBody(patch); Utils.mergeWithState(getStateDescription(), state, patchBody); patch.setBody(state); patch.complete(); } }
@Override public void handlePatch(Operation patch) { Employee state = getState(patch); Employee patchBody = getBody(patch); Utils.mergeWithState(getStateDescription(), state, patchBody); patch.setBody(state); patch.complete(); logInfo("Successfully patched %s. New body is:%s", state.documentSelfLink, state); } }
@Override public void handlePatch(Operation patch) { Employee state = getState(patch); Employee patchBody = getBody(patch); Utils.mergeWithState(getStateDescription(), state, patchBody); patch.setBody(state); patch.complete(); // Added for 'add-logging' project. logInfo("Successfully patched %s. New body is:%s", state.documentSelfLink, state); } }
protected ExampleServiceState updateState(Operation update) { // A DCP service handler is state-less: Everything it needs is provided as part of the // of the operation. The body and latest state associated with the service are retrieved // below. ExampleServiceState body = getBody(update); ExampleServiceState currentState = getState(update); // use helper that will merge automatically current state, with state supplied in body. // Note the usage option PropertyUsageOption.AUTO_MERGE_IF_NOT_NULL has been set on the // "name" field. boolean hasStateChanged = Utils.mergeWithState(getStateDescription(), currentState, body); updateCounter(body, currentState, hasStateChanged); if (body.documentExpirationTimeMicros != currentState.documentExpirationTimeMicros) { currentState.documentExpirationTimeMicros = body.documentExpirationTimeMicros; } // response has latest, updated state update.setBody(currentState); return currentState; }
@Override public void handlePatch(Operation patch) { if (!patch.hasBody()) { patch.fail(new IllegalArgumentException("body is required")); return; } AuthCredentialsServiceState currentState = getState(patch); try { if (!Utils.mergeWithState(currentState, patch)) { AuthCredentialsServiceState patchBody = patch.getBody(AuthCredentialsServiceState.class); updateState(patchBody, currentState); } } catch (NoSuchFieldException | IllegalAccessException e) { patch.fail(e); return; } patch.setBody(currentState).complete(); }
@Override public void handlePatch(Operation patch) { ResourceOperationSpec currentState = getState(patch); ResourceOperationSpec body = patch.getBody(ResourceOperationSpec.class); validate(body, patch.getRefererAsString()); boolean merged = Utils.mergeWithState(getStateDescription(), currentState, body); if (merged) { logFine("%s updated.", currentState); } else { patch.setStatusCode(Operation.STATUS_CODE_NOT_MODIFIED); } patch.complete(); }
@Override public void handlePatch(Operation patch) { TestState body = getBody(patch); TestState state = getState(patch); Utils.mergeWithState(getStateDescription(), state, body); if (body.intValue != null) { state.intValues.add(body.intValue); } if (body.documentExpirationTimeMicros > 0) { state.documentExpirationTimeMicros = body.documentExpirationTimeMicros; } patch.setBodyNoCloning(state).complete(); }
@Override public void handlePatch(Operation op) { ExampleService.ExampleServiceState state = getState(op); ExampleService.ExampleServiceState body = getBody(op); Utils.mergeWithState(getStateDescription(), state, body); state.keyValues = body.keyValues; op.complete(); }
@Override public void handlePatch(Operation op) { ExampleService.ExampleServiceState state = getState(op); ExampleService.ExampleServiceState body = getBody(op); Utils.mergeWithState(getStateDescription(), state, body); state.keyValues = body.keyValues; op.complete(); }
@Override public void handlePatch(Operation patch) { // logInfo("handlePatch called"); TestPeriodicState body = getBody(patch); TestPeriodicState state = getState(patch); Utils.mergeWithState(getStateDescription(), state, body); state.calls += body.calls; if (body.documentExpirationTimeMicros > 0) { state.documentExpirationTimeMicros = body.documentExpirationTimeMicros; } if (body.intValue != null) { state.intValues.add(body.intValue); } patch.setBodyNoCloning(state).complete(); }
@Override public void handlePatch(Operation patch) { try { ScheduledTaskState patchBody = getBody(patch); Utils.validateState(getStateDescription(), patchBody); ScheduledTaskState currentState = getState(patch); boolean hasStateChanged = Utils.mergeWithState(getStateDescription(), currentState, patchBody); if (!hasStateChanged) { patch.setStatusCode(Operation.STATUS_CODE_NOT_MODIFIED); } else { patch.setBody(currentState); } patch.complete(); } catch (Throwable e) { patch.fail(e); } }
/** * Test merging in an empty patch. */ @Test public void testEmptyPatch() { MergeTest source = new MergeTest(); source.s = SOME_STRING_VALUE; source.x = SOME_INT_VALUE; source.ignore = SOME_IGNORE_VALUE; MergeTest patch = new MergeTest(); ServiceDocumentDescription d = ServiceDocumentDescription.Builder.create() .buildDescription(MergeTest.class); Assert.assertFalse("There should be no changes", Utils.mergeWithState(d, source, patch)); Assert.assertEquals("Annotated s field", source.s, SOME_STRING_VALUE); Assert.assertEquals("Annotated x field", source.x, SOME_INT_VALUE); Assert.assertEquals("Non-annotated ignore field", source.ignore, SOME_IGNORE_VALUE); }
/** * Test merging in an empty patch. */ @Test public void testEmptyPatch() { MergeTest source = new MergeTest(); source.s = SOME_STRING_VALUE; source.x = SOME_INT_VALUE; source.ignore = SOME_IGNORE_VALUE; MergeTest patch = new MergeTest(); ServiceDocumentDescription d = ServiceDocumentDescription.Builder.create() .buildDescription(MergeTest.class); Assert.assertFalse("There should be no changes", Utils.mergeWithState(d, source, patch)); Assert.assertEquals("Annotated s field", source.s, SOME_STRING_VALUE); Assert.assertEquals("Annotated x field", source.x, SOME_INT_VALUE); Assert.assertEquals("Non-annotated ignore field", source.ignore, SOME_IGNORE_VALUE); }
/** * Test merging patch with same values as existing value. */ @Test public void testEqualsMerge() { MergeTest source = new MergeTest(); source.s = SOME_STRING_VALUE; source.x = SOME_INT_VALUE; source.ignore = SOME_IGNORE_VALUE; MergeTest patch = new MergeTest(); patch.s = source.s; patch.x = source.x; patch.ignore = source.ignore; ServiceDocumentDescription d = ServiceDocumentDescription.Builder.create() .buildDescription(MergeTest.class); Assert.assertFalse("There should be no changes", Utils.mergeWithState(d, source, patch)); Assert.assertEquals("Annotated s field", source.s, SOME_STRING_VALUE); Assert.assertEquals("Annotated x field", source.x, SOME_INT_VALUE); Assert.assertEquals("Non-annotated ignore field", source.ignore, SOME_IGNORE_VALUE); }
/** * Test merging partially defined patch object. */ @Test public void testPartialMerge() { MergeTest source = new MergeTest(); source.s = SOME_STRING_VALUE; source.x = SOME_INT_VALUE; source.ignore = SOME_IGNORE_VALUE; MergeTest patch = new MergeTest(); patch.x = SOME_OTHER_INT_VALUE; ServiceDocumentDescription d = ServiceDocumentDescription.Builder.create() .buildDescription(MergeTest.class); Assert.assertTrue("There should be changes", Utils.mergeWithState(d, source, patch)); Assert.assertEquals("Annotated s field", source.s, SOME_STRING_VALUE); Assert.assertEquals("Annotated x field", source.x, SOME_OTHER_INT_VALUE); Assert.assertEquals("Non-annotated ignore field", source.ignore, SOME_IGNORE_VALUE); }
/** * Test merging partially defined patch object. */ @Test public void testPartialMerge() { MergeTest source = new MergeTest(); source.s = SOME_STRING_VALUE; source.x = SOME_INT_VALUE; source.ignore = SOME_IGNORE_VALUE; MergeTest patch = new MergeTest(); patch.x = SOME_OTHER_INT_VALUE; ServiceDocumentDescription d = ServiceDocumentDescription.Builder.create() .buildDescription(MergeTest.class); Assert.assertTrue("There should be changes", Utils.mergeWithState(d, source, patch)); Assert.assertEquals("Annotated s field", source.s, SOME_STRING_VALUE); Assert.assertEquals("Annotated x field", source.x, SOME_OTHER_INT_VALUE); Assert.assertEquals("Non-annotated ignore field", source.ignore, SOME_IGNORE_VALUE); }