From 4ae6c2cb29a8fa9c313557cd17cdb0c58f1df312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Le=C3=B3n?= Date: Tue, 11 Jun 2019 19:34:46 +0200 Subject: [PATCH] feat(pipeline/expressions): support yaml parsing (#2946) 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 --- orca-core/orca-core.gradle | 1 + .../expressions/ExpressionsSupport.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/orca-core/orca-core.gradle b/orca-core/orca-core.gradle index 34b08abe5c..f1f6752b48 100644 --- a/orca-core/orca-core.gradle +++ b/orca-core/orca-core.gradle @@ -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") diff --git a/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java b/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java index 81e5031b08..fcbeeb48d6 100644 --- a/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java +++ b/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java @@ -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 @@ -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)); @@ -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); @@ -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 * @@ -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 *