Skip to content

Commit

Permalink
feat(pipeline/expressions): support yaml parsing (#2946)
Browse files Browse the repository at this point in the history
This patch adds two helper functions `readYaml` and
`yamlFromUrl` to transfrom yaml strings (either
embedded or from retrieved from an URL) to their
object representation.

DNMY: missing tests
  • Loading branch information
Xavi León authored and marchello2000 committed Jun 11, 2019
1 parent 2c887f2 commit 4ae6c2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions orca-core/orca-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
implementation("org.apache.commons:commons-lang3")
implementation("javax.servlet:javax.servlet-api:4.0.1")
implementation("com.jayway.jsonpath:json-path:2.2.0")
implementation("org.yaml:snakeyaml")

compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.yaml.snakeyaml.Yaml;

/**
* Provides utility support for SPEL integration Supports registering SPEL functions, ACLs to
Expand All @@ -60,6 +61,7 @@ public class ExpressionsSupport {
registeredHelperFunctions.put("alphanumerical", Collections.singletonList(String.class));
registeredHelperFunctions.put("toJson", Collections.singletonList(Object.class));
registeredHelperFunctions.put("readJson", Collections.singletonList(String.class));
registeredHelperFunctions.put("readYaml", Collections.singletonList(String.class));
registeredHelperFunctions.put("toInt", Collections.singletonList(String.class));
registeredHelperFunctions.put("toFloat", Collections.singletonList(String.class));
registeredHelperFunctions.put("toBoolean", Collections.singletonList(String.class));
Expand Down Expand Up @@ -99,6 +101,7 @@ public static StandardEvaluationContext newEvaluationContext(
// lazily function registering
registerFunction(evaluationContext, "fromUrl", String.class);
registerFunction(evaluationContext, "jsonFromUrl", String.class);
registerFunction(evaluationContext, "yamlFromUrl", String.class);
registerFunction(evaluationContext, "propertiesFromUrl", String.class);
registerFunction(evaluationContext, "stage", Object.class, String.class);
registerFunction(evaluationContext, "stageExists", Object.class, String.class);
Expand Down Expand Up @@ -254,6 +257,20 @@ static Object readJson(String text) {
}
}

/**
* Attempts to read yaml from a text String. Will throw a parsing exception on bad yaml
*
* @param text text to read as yaml
* @return the object representation of the yaml text
*/
static Object readYaml(String text) {
try {
return new Yaml().load(text);
} catch (Exception e) {
throw new SpelHelperFunctionException(format("#readYaml(%s) failed", text), e);
}
}

/**
* Reads a json text
*
Expand All @@ -264,6 +281,16 @@ static Object jsonFromUrl(String url) {
return readJson(fromUrl(url));
}

/**
* Reads a yaml text
*
* @param url url to get the json text
* @return an object representing the yaml object
*/
static Object yamlFromUrl(String url) {
return readYaml(fromUrl(url));
}

/**
* Reads a properties file stored at a url
*
Expand Down

0 comments on commit 4ae6c2c

Please sign in to comment.