Skip to content

Commit

Permalink
refactor: just use null state
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 committed Oct 10, 2023
1 parent f21c308 commit 7d8127e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public GetThingShadowResponse handleRequest(GetThingShadowRequest request, Strin
}

ObjectNode responseNode = ResponseMessageBuilder.builder()
.withState(currentShadowDocument.get().getState().toJsonWithDelta())
.withState(currentShadowDocument.get().getState() == null ? JsonUtil.createEmptyObject()
: currentShadowDocument.get().getState().toJsonWithDelta())
.withMetadata(currentShadowDocument.get().getMetadata().toJson())
.withVersion(currentShadowDocument.get().getVersion())
.withTimestamp(Instant.now()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,13 @@ public UpdateThingShadowHandlerResponse handleRequest(UpdateThingShadowRequest r
// to avoid double serialization, but DB stores the single document
int desiredLength = 0;
int reportedLength = 0;
if (!isNullOrMissing(updatedDocument.getState().getDesired())) {
desiredLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getDesired()).length;
}
if (!isNullOrMissing(updatedDocument.getState().getReported())) {
reportedLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getReported()).length;
if (updatedDocument.getState() != null) {
if (!isNullOrMissing(updatedDocument.getState().getDesired())) {
desiredLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getDesired()).length;
}
if (!isNullOrMissing(updatedDocument.getState().getReported())) {
reportedLength = JsonUtil.getPayloadBytes(updatedDocument.getState().getReported()).length;
}
}

// Make sure new document is not too big
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.aws.greengrass.util.SerializerFactory;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Getter;
Expand All @@ -29,9 +28,8 @@
* Class for managing operations on the Shadow Document.
*/
@Getter
@JsonDeserialize(using = ShadowDocumentDeserializer.class)
public class ShadowDocument {
@JsonProperty(value = SHADOW_DOCUMENT_STATE, required = true)
@JsonProperty(SHADOW_DOCUMENT_STATE)
private ShadowState state;

@JsonProperty(SHADOW_DOCUMENT_METADATA)
Expand Down Expand Up @@ -136,7 +134,7 @@ private void setFields(ShadowDocument shadowDocument, Long versionOverride) {
}

private void setFields(ShadowState state, ShadowStateMetadata metadata, Long version) {
this.state = state == null ? new ShadowState() : state;
this.state = state;
this.metadata = metadata == null ? new ShadowStateMetadata() : metadata;
this.version = version;
}
Expand All @@ -158,7 +156,9 @@ public boolean isNewDocument() {
*/
public JsonNode update(JsonNode updateDocumentRequest) {
JsonNode updatedStateNode = updateDocumentRequest.get(SHADOW_DOCUMENT_STATE);

if (this.state == null) {
this.state = new ShadowState();
}
this.state.update(updatedStateNode);
JsonNode patchMetadata = this.metadata.update(updatedStateNode, this.state);
// Incrementing the version here since we are creating a new version of the shadow document.
Expand All @@ -174,8 +174,8 @@ public JsonNode update(JsonNode updateDocumentRequest) {
* @return a JSON node containing the shadow document.
*/
public JsonNode toJson(boolean withVersion) {
final ObjectNode result = JsonUtil.OBJECT_MAPPER.createObjectNode();
result.set(SHADOW_DOCUMENT_STATE, this.state.toJson());
final ObjectNode result = JsonUtil.createEmptyObject();
result.set(SHADOW_DOCUMENT_STATE, this.state == null ? JsonUtil.createNull() : this.state.toJson());
if (this.metadata != null) {
result.set(SHADOW_DOCUMENT_METADATA, this.metadata.toJson());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,12 @@ public static boolean hasVersion(JsonNode document) {
public static long getVersion(JsonNode document) {
return document.get(SHADOW_DOCUMENT_VERSION).asLong();
}

public static ObjectNode createEmptyObject() {
return OBJECT_MAPPER.createObjectNode();
}

public static JsonNode createNull() {
return OBJECT_MAPPER.nullNode();
}
}

0 comments on commit 7d8127e

Please sign in to comment.