Skip to content

Commit

Permalink
feat(core): remove deprecated properties and reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jan 29, 2025
1 parent b846f11 commit afabdf8
Showing 1 changed file with 3 additions and 167 deletions.
170 changes: 3 additions & 167 deletions core/src/main/java/io/kestra/core/models/property/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ public static <V> Property<V> of(V value) {
* @see io.kestra.core.runners.RunContextProperty#as(Class)
*/
public static <T> T as(Property<T> property, RunContext runContext, Class<T> clazz) throws IllegalVariableEvaluationException {
if (property.value == null) {
String rendered = runContext.render(property.expression);
// special case for duration as they should be serialized as double but are not always
property.value = MAPPER.convertValue(rendered, clazz);
}

return property.value;
return as(property, runContext, clazz, Map.of());
}

/**
Expand All @@ -132,17 +126,7 @@ public static <T> T as(Property<T> property, RunContext runContext, Class<T> cla
* @see io.kestra.core.runners.RunContextProperty#asList(Class)
*/
public static <T, I> T asList(Property<T> property, RunContext runContext, Class<I> itemClazz) throws IllegalVariableEvaluationException {
if (property.value == null) {
String rendered = runContext.render(property.expression);
JavaType type = MAPPER.getTypeFactory().constructCollectionLikeType(List.class, itemClazz);
try {
property.value = MAPPER.readValue(rendered, type);
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return property.value;
return asList(property, runContext, itemClazz, Map.of());
}

/**
Expand Down Expand Up @@ -174,27 +158,7 @@ public static <T, I> T asList(Property<T> property, RunContext runContext, Class
* @see io.kestra.core.runners.RunContextProperty#asMap(Class, Class)
*/
public static <T, K,V> T asMap(Property<T> property, RunContext runContext, Class<K> keyClass, Class<V> valueClass) throws IllegalVariableEvaluationException {
if (property.value == null) {
JavaType targetMapType = MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);

try {
String trimmedExpression = property.expression.trim();
// We need to detect if the expression is already a map or if it's a pebble expression (for eg. referencing a variable containing a map).
// Doing that allows us to, if it's an expression, first render then read it as a map.
if (trimmedExpression.startsWith("{{") && trimmedExpression.endsWith("}}")) {
property.value = MAPPER.readValue(runContext.render(property.expression), targetMapType);
}
// Otherwise if it's already a map we read it as a map first then render it from run context which handle map rendering by rendering each entry of the map (otherwise it will fail with nested expressions in values for eg.)
else {
Map asRawMap = MAPPER.readValue(property.expression, Map.class);
property.value = MAPPER.convertValue(runContext.render(asRawMap), targetMapType);
}
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return property.value;
return asMap(property, runContext, keyClass, valueClass, Map.of());
}

/**
Expand Down Expand Up @@ -229,134 +193,6 @@ public static <T, K,V> T asMap(Property<T> property, RunContext runContext, Clas
return property.value;
}

/**
* Render a property then convert it to its target type.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public T as(RunContext runContext, Class<T> clazz) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression);
this.value = MAPPER.convertValue(rendered, clazz);
}

return this.value;
}

/**
* Render a property with additional variables, then convert it to its target type.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public T as(RunContext runContext, Class<T> clazz, Map<String, Object> variables) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression, variables);
this.value = MAPPER.convertValue(rendered, clazz);
}

return this.value;
}

/**
* Render a property then convert it as a list of target type.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public <I> T asList(RunContext runContext, Class<I> itemClazz) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression);
JavaType type = MAPPER.getTypeFactory().constructCollectionLikeType(List.class, itemClazz);
try {
this.value = MAPPER.readValue(rendered, type);
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return this.value;
}

/**
* Render a property with additional variables, then convert it as a list of target type.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public <I> T asList(RunContext runContext, Class<I> itemClazz, Map<String, Object> variables) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression, variables);
JavaType type = MAPPER.getTypeFactory().constructCollectionLikeType(List.class, itemClazz);
try {
this.value = MAPPER.readValue(rendered, type);
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return this.value;
}

/**
* Render a property then convert it as a map of target types.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public <K,V> T asMap(RunContext runContext, Class<K> keyClass, Class<V> valueClass) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression);
JavaType type = MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
try {
this.value = MAPPER.readValue(rendered, type);
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return this.value;
}

/**
* Render a property with additional variables, then convert it as a map of target types.<br>
*
* This method is safe to be used as many times as you want as the rendering and conversion will be cached.
* Warning, due to the caching mechanism, this method is not thread-safe.
*
* @deprecated use RunContext.render(Property) instead.
*/
@Deprecated(forRemoval = true)
public <K,V> T asMap(RunContext runContext, Class<K> keyClass, Class<V> valueClass, Map<String, Object> variables) throws IllegalVariableEvaluationException {
if (this.value == null) {
String rendered = runContext.render(expression, variables);
JavaType type = MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
try {
this.value = MAPPER.readValue(rendered, type);
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
}

return this.value;
}

@Override
public String toString() {
return value != null ? value.toString() : expression;
Expand Down

0 comments on commit afabdf8

Please sign in to comment.