You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2021/07/01 15:16:58 UTC

[camel-k] branch main updated: Fix #2468: parse all YAML nodes in the tree

This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 874e86b  Fix #2468: parse all YAML nodes in the tree
874e86b is described below

commit 874e86bf27da091983c5174ff2e521739cd3cadd
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Thu Jul 1 14:48:13 2021 +0200

    Fix #2468: parse all YAML nodes in the tree
---
 pkg/util/source/inspector_yaml.go      | 98 +++++++++++++++++-----------------
 pkg/util/source/inspector_yaml_test.go | 20 +++++++
 2 files changed, 69 insertions(+), 49 deletions(-)

diff --git a/pkg/util/source/inspector_yaml.go b/pkg/util/source/inspector_yaml.go
index 9fe8b11..4f2b95b 100644
--- a/pkg/util/source/inspector_yaml.go
+++ b/pkg/util/source/inspector_yaml.go
@@ -59,15 +59,6 @@ func (i YAMLInspector) Extract(source v1.SourceSpec, meta *Metadata) error {
 
 func (i YAMLInspector) parseStep(key string, content interface{}, meta *Metadata) error {
 	switch key {
-	case "route":
-		// process steps if they are defined in the route
-		if routeSteps, ok := content.(map[interface{}]interface{}); ok {
-			if steps, stepsFormatOk := routeSteps["steps"].([]interface{}); stepsFormatOk {
-				if err := i.parseStepsParam(steps, meta); err != nil {
-					return err
-				}
-			}
-		}
 	case "rest":
 		meta.ExposesHTTPServices = true
 		meta.RequiredCapabilities.Add(v1.CapabilityRest)
@@ -104,53 +95,62 @@ func (i YAMLInspector) parseStep(key string, content interface{}, meta *Metadata
 	case string:
 		maybeURI = t
 	case map[interface{}]interface{}:
-		if u, ok := t["rest"]; ok {
-			return i.parseStep("rest", u, meta)
-		} else if u, ok := t["from"]; ok {
-			return i.parseStep("from", u, meta)
-		} else if u, ok := t["steps"]; ok {
-			if steps, stepsFormatOk := u.([]interface{}); stepsFormatOk {
-				if err := i.parseStepsParam(steps, meta); err != nil {
-					return err
+		for k, v := range t {
+
+			if s, ok := k.(string); ok {
+				if dependency, ok := i.catalog.GetLanguageDependency(s); ok {
+					i.addDependency(dependency, meta)
 				}
 			}
-		}
 
-		if u, ok := t["uri"]; ok {
-			if v, isString := u.(string); isString {
-				builtURI := v
-				// Inject parameters into URIs to allow other parts of the operator to inspect them
-				if params, pok := t["parameters"]; pok {
-					if paramMap, pmok := params.(map[interface{}]interface{}); pmok {
-						params := make(map[string]string, len(paramMap))
-						for k, v := range paramMap {
-							ks := fmt.Sprintf("%v", k)
-							vs := fmt.Sprintf("%v", v)
-							params[ks] = vs
-						}
-						builtURI = uri.AppendParameters(builtURI, params)
+			switch k {
+			case "steps":
+				if steps, stepsFormatOk := v.([]interface{}); stepsFormatOk {
+					if err := i.parseStepsParam(steps, meta); err != nil {
+						return err
 					}
 				}
-				maybeURI = builtURI
-			}
-		}
-
-		if _, ok := t["language"]; ok {
-			if s, ok := t["language"].(string); ok {
-				if dependency, ok := i.catalog.GetLanguageDependency(s); ok {
-					i.addDependency(dependency, meta)
+			case "uri":
+				if vv, isString := v.(string); isString {
+					builtURI := vv
+					// Inject parameters into URIs to allow other parts of the operator to inspect them
+					if params, pok := t["parameters"]; pok {
+						if paramMap, pmok := params.(map[interface{}]interface{}); pmok {
+							params := make(map[string]string, len(paramMap))
+							for k, v := range paramMap {
+								ks := fmt.Sprintf("%v", k)
+								vs := fmt.Sprintf("%v", v)
+								params[ks] = vs
+							}
+							builtURI = uri.AppendParameters(builtURI, params)
+						}
+					}
+					maybeURI = builtURI
 				}
-			} else if m, ok := t["language"].(map[interface{}]interface{}); ok {
-				if err := i.parseStep("language", m, meta); err != nil {
-					return err
+			case "language":
+				if s, ok := v.(string); ok {
+					if dependency, ok := i.catalog.GetLanguageDependency(s); ok {
+						i.addDependency(dependency, meta)
+					}
+				} else if m, ok := v.(map[interface{}]interface{}); ok {
+					if err := i.parseStep("language", m, meta); err != nil {
+						return err
+					}
 				}
-			}
-		}
-
-		for k := range t {
-			if s, ok := k.(string); ok {
-				if dependency, ok := i.catalog.GetLanguageDependency(s); ok {
-					i.addDependency(dependency, meta)
+			default:
+				// Always follow children because from/to uris can be nested
+				if ks, ok := k.(string); ok {
+					if _, ok := v.(map[interface{}]interface{}); ok {
+						if err := i.parseStep(ks, v, meta); err != nil {
+							return err
+						}
+					} else if ls, ok := v.([]interface{}); ok {
+						for _, el := range ls {
+							if err := i.parseStep(ks, el, meta); err != nil {
+								return err
+							}
+						}
+					}
 				}
 			}
 		}
diff --git a/pkg/util/source/inspector_yaml_test.go b/pkg/util/source/inspector_yaml_test.go
index 692d4ed..d8881e6 100644
--- a/pkg/util/source/inspector_yaml_test.go
+++ b/pkg/util/source/inspector_yaml_test.go
@@ -69,6 +69,18 @@ const YAMLInvalid = `
       - "log:out"
 `
 
+const YAMLInDepthChannel = `
+- from:
+    uri: knative:channel/mychannel
+    steps:
+      - choice:
+          when:
+          - simple: "${body}"
+            steps:
+            - to:
+                uri: knative:endpoint/service
+`
+
 func TestYAMLDependencies(t *testing.T) {
 	tests := []struct {
 		name                string
@@ -103,6 +115,14 @@ func TestYAMLDependencies(t *testing.T) {
 				`mvn:org.apache.camel.k:camel-k-knative-consumer`,
 			},
 		},
+		{
+			name:   "in-depth",
+			source: YAMLInDepthChannel,
+			dependencies: []string{
+				`mvn:org.apache.camel.k:camel-k-knative-producer`,
+				`mvn:org.apache.camel.k:camel-k-knative-consumer`,
+			},
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {