Skip to content

Commit

Permalink
Add fromProtoEnumStream method to ProtobufUtils.
Browse files Browse the repository at this point in the history
Skip not supported Feature proto enum values to support future updates fo the feature list.
  • Loading branch information
HenrikJannsen committed Mar 25, 2024
1 parent 62d616c commit bcb0ac1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
42 changes: 35 additions & 7 deletions common/src/main/java/bisq/common/util/ProtobufUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@
import com.google.protobuf.Any;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
public class ProtobufUtils {
@Nullable
public static <E extends Enum<E>> E enumFromProto(Class<E> enumType, String name) {
String info = "Enum type= " + enumType.getSimpleName() + "; name=" + name;
checkNotNull(name, "Enum name must not be null. "+info);
checkArgument(!name.endsWith("_UNSPECIFIED"), "Unspecified enum. "+info);
checkNotNull(name, "Enum name must not be null. " + info);
checkArgument(!name.endsWith("_UNSPECIFIED"), "Unspecified enum. " + info);

//Remove prefix from enum name. Since enum is based on the enum's class name, we use that to extract the prefix
String enumName = name.replace(ProtoEnum.getProtobufEnumPrefix(enumType),"");
String enumName = name.replace(ProtoEnum.getProtobufEnumPrefix(enumType), "");
E result = Enums.getIfPresent(enumType, enumName).orNull();
checkNotNull(result, "Enum could not be resolved. "+info);
checkNotNull(result, "Enum could not be resolved. " + info);
return result;
}

Expand All @@ -55,7 +61,7 @@ public static <E extends Enum<E>> E enumFromProto(Class<E> enumType, String name
return fallBack;
}
//Remove prefix from enum name. Since enum is based on the enum's class name, we use that to extract the prefix
String enumName = name.replace(ProtoEnum.getProtobufEnumPrefix(enumType),"");
String enumName = name.replace(ProtoEnum.getProtobufEnumPrefix(enumType), "");
E result = Enums.getIfPresent(enumType, enumName).orNull();
if (result == null) {
log.error("Enum could not be resolved. We use the fallback value instead. {}", info);
Expand All @@ -81,4 +87,26 @@ public static Any toAny(byte[] bytes) throws IOException {
return Any.parseDelimitedFrom(inputStream);
}
}

// Convert list of proto enums to a stream of Enums. If the enumFromProto fails we skip the enum.
public static <E extends Enum<E>, P> Stream<E> fromProtoEnumStream(Class<E> enumType, List<P> proto) {
return proto.stream()
.map(enumProto -> {
try {
return enumFromProto(enumType, ((Enum<?>) enumProto).name());
} catch (Exception e) {
log.warn("Could not resolve enum for proto {}.", enumProto, e);
return null;
}
})
.filter(Objects::nonNull);
}

public static <E extends Enum<E>, P> List<E> fromProtoEnumList(Class<E> enumType, List<P> proto) {
return fromProtoEnumStream(enumType, proto).collect(Collectors.toList());
}

public static <E extends Enum<E>, P> Set<E> fromProtoEnumSet(Class<E> enumType, List<P> proto) {
return fromProtoEnumStream(enumType, proto).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ public static Capability fromProto(bisq.network.protobuf.Capability proto) {
List<TransportType> supportedTransportTypes = proto.getSupportedTransportTypesList().stream()
.map(e -> ProtobufUtils.enumFromProto(TransportType.class, e))
.collect(Collectors.toList());
List<Feature> features = proto.getFeaturesList().stream()
.map(Feature::fromProto)
.collect(Collectors.toList());
return new Capability(Address.fromProto(proto.getAddress()),
supportedTransportTypes,
features);
ProtobufUtils.fromProtoEnumList(Feature.class, proto.getFeaturesList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public bisq.network.protobuf.Feature toProto() {
return bisq.network.protobuf.Feature.valueOf(getProtobufEnumPrefix() + name());
}

// Not used. Feature is used as list in Capability thus we use ProtobufUtils.fromProtoEnumList.
// Still keep fromProto for following convention and potential future usage.
public static Feature fromProto(bisq.network.protobuf.Feature proto) {
return ProtobufUtils.enumFromProto(Feature.class, proto.name());
}
Expand Down

0 comments on commit bcb0ac1

Please sign in to comment.