Skip to content

Commit

Permalink
feat(core): add urldecode pebble function
Browse files Browse the repository at this point in the history
close #2117
  • Loading branch information
tchiotludo committed Sep 14, 2023
1 parent db995d4 commit 5fc293e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Map<String, Filter> getFilters() {
filters.put("json", new JsonFilter());
filters.put("keys", new KeysFilter());
filters.put("number", new NumberFilter());
filters.put("urldecode", new UrlDecoderFilter());
filters.put("slugify", new SlugifyFilter());
filters.put("substringBefore", new SubstringBeforeFilter());
filters.put("substringBeforeLast", new SubstringBeforeLastFilter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of Pebble.
*
* Copyright (c) 2014 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package io.kestra.core.runners.pebble.filters;

import io.pebbletemplates.pebble.extension.Filter;
import io.pebbletemplates.pebble.template.EvaluationContext;
import io.pebbletemplates.pebble.template.PebbleTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

public class UrlDecoderFilter implements Filter {

@Override
public List<String> getArgumentNames() {
return null;
}

@Override
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self,
EvaluationContext context, int lineNumber) {
if (input == null) {
return null;
}
String arg = (String) input;
try {
arg = URLDecoder.decode(arg, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
return arg;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.kestra.core.runners.pebble.filters;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.runners.VariableRenderer;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@MicronautTest
class UrlDecodeFilter {
@Inject
VariableRenderer variableRenderer;

@Test
void urldecode() throws IllegalVariableEvaluationException {
String render = variableRenderer.render("{{ 'Kestra rulez !' | urlencode | urldecode }}", Map.of());
assertThat(render, is("Kestra rulez !"));
}
}

0 comments on commit 5fc293e

Please sign in to comment.