Skip to content

Commit

Permalink
issues/249 - Fix for nested option types that contain children
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Mar 6, 2024
1 parent db43940 commit 029a18e
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.casper.sdk.model.clvalue;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren;
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeList;
import com.casper.sdk.model.clvalue.cltype.CLTypeOption;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.clvalue.cltype.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
Expand Down Expand Up @@ -41,24 +37,12 @@ public void setClType(final CLTypeOption clType) {
childTypesSet();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public CLValueOption(final Optional<AbstractCLValue<?, ?>> value) throws ValueSerializationException {
setChildTypes(value);
this.setValue(value);
}

@Override
public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {

super.serialize(ser, target);

if (target.equals(Target.BYTE)) {
Optional<AbstractCLValue<?, ?>> child = getValue();
if (child.isPresent()) {
child.get().encodeType(ser);
}
}
}

@Override
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {

Expand All @@ -72,7 +56,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
((AbstractCLTypeWithChildren) value.get().getClType())
.setChildTypes(((AbstractCLTypeWithChildren) clType.getOptionType()).getChildTypes());
}
if (value.isPresent() && isPresent.getValue().equals(Boolean.TRUE)) {
if (Boolean.TRUE.equals(isPresent.getValue()) && value.isPresent()) {
value.get().serialize(serVal);
}

Expand All @@ -96,15 +80,28 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
} else if (child.getClType() instanceof AbstractCLTypeWithChildren) {
((AbstractCLTypeWithChildren) child.getClType())
.setChildTypes(((AbstractCLTypeWithChildren) clType.getOptionType()).getChildTypes());
} else if (child instanceof CLValueByteArray) {
// Byte arrays require their length to be set to correctly deserialize
((CLValueByteArray) child).setClType((CLTypeByteArray) clType.getOptionType());
}

if (isPresent.getValue().equals(Boolean.TRUE)) {
if (Boolean.TRUE.equals(isPresent.getValue())) {
child.deserializeCustom(deser);
}

setValue(Optional.of(child));
}

@Override
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

Optional<AbstractCLValue<?, ?>> child = getValue();
if (child.isPresent()) {
child.get().encodeType(ser);
}
}

@Override
protected void setChildTypes(final Optional<AbstractCLValue<?, ?>> value) {
if (value.isPresent()) {
Expand Down

0 comments on commit 029a18e

Please sign in to comment.