Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Refactor exceptions and yaml parser (#1094)
Browse files Browse the repository at this point in the history
* Refactor exceptions and yaml parser

* Format imports

* Remove var and reason message
  • Loading branch information
pablocasares authored Jul 7, 2023
1 parent 705bab4 commit 9096e9f
Showing 1 changed file with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static java.util.stream.Collectors.toSet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.spotify.styx.serialization.Json;
import flyteidl.core.Interface;
import flyteidl.core.Literals;
import flyteidl.core.Types;
Expand All @@ -45,8 +44,6 @@ public class FlyteInputsUtils {
private static final Set<TypeCase> COMPLEX_TYPES =
Set.of(TypeCase.COLLECTION_TYPE, TypeCase.MAP_VALUE_TYPE);

private static final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

private FlyteInputsUtils() {
throw new UnsupportedOperationException();
}
Expand All @@ -62,16 +59,16 @@ static Literals.Literal literalOf(String key, String value, Types.LiteralType li
} catch (Exception ex) {
throw new UnsupportedOperationException(
String.format(
"Collection could not be parsed for input [%s]. Reason: %s",
key, ex.getMessage()));
"Collection could not be parsed for input [%s].",
key), ex);
}
case MAP_VALUE_TYPE:
try {
return parserMapType(key, value, literalType.getMapValueType());
} catch (Exception ex) {
throw new UnsupportedOperationException(
String.format(
"Map could not be parsed for input [%s]. Reason: %s", key, ex.getMessage()));
"Map could not be parsed for input [%s].", key), ex);
}
default:
String message =
Expand All @@ -87,14 +84,14 @@ private static Literals.Literal parserMapType(String key, String value, LiteralT
throws JsonProcessingException {
var mapBuilder = Literals.LiteralMap.newBuilder();

Map<String, Object> map = mapper.readValue(value, Map.class);
Map<String, Object> map = Json.YAML_MAPPER.readValue(value, Map.class);
var containsComplexTypes = COMPLEX_TYPES.contains(innerType.getTypeCase());

for (var element : map.entrySet()) {
String result;

if (containsComplexTypes) {
result = mapper.writeValueAsString(element.getValue());
result = Json.YAML_MAPPER.writeValueAsString(element.getValue());
} else {
result = element.getValue().toString();
}
Expand All @@ -109,14 +106,14 @@ private static Literals.Literal parseCollectionType(
String key, String value, LiteralType innerType) throws JsonProcessingException {
var collectionBuilder = Literals.LiteralCollection.newBuilder();

List<Object> list = mapper.readValue(value, List.class);
List<Object> list = Json.YAML_MAPPER.readValue(value, List.class);
var containsComplexTypes = COMPLEX_TYPES.contains(innerType.getTypeCase());

for (var element : list) {
String result;

if (containsComplexTypes) {
result = mapper.writeValueAsString(element);
result = Json.YAML_MAPPER.writeValueAsString(element);
} else {
result = element.toString();
}
Expand Down

0 comments on commit 9096e9f

Please sign in to comment.