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

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


The following commit(s) were added to refs/heads/master by this push:
     new 49df1be  Factorize CSV map string parsing
49df1be is described below

commit 49df1be1c6096202e8ce90451f9243803e9fa940
Author: Antonin Stefanutti <[email protected]>
AuthorDate: Thu Jan 31 15:47:14 2019 +0100

    Factorize CSV map string parsing
---
 pkg/trait/jolokia.go    | 23 ++---------------------
 pkg/trait/prometheus.go | 22 +---------------------
 pkg/trait/util.go       | 25 +++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 42 deletions(-)

diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go
index 7a29a6f..a851215 100644
--- a/pkg/trait/jolokia.go
+++ b/pkg/trait/jolokia.go
@@ -19,7 +19,6 @@ package trait
 
 import (
        "errors"
-       "fmt"
        "strconv"
        "strings"
 
@@ -61,7 +60,7 @@ func newJolokiaTrait() *jolokiaTrait {
 }
 
 func (t *jolokiaTrait) Configure(e *Environment) (bool, error) {
-       options, err := parseJolokiaOptions(t.Options)
+       options, err := parseCsvMap(t.Options)
        if err != nil {
                return false, err
        }
@@ -93,7 +92,7 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
 
        // Configure the Jolokia Java agent
        // Populate first with the extra options
-       options, err := parseJolokiaOptions(t.Options)
+       options, err := parseCsvMap(t.Options)
        if err != nil {
                return err
        }
@@ -182,21 +181,3 @@ func addToJolokiaOptions(options map[string]string, key 
string, value interface{
                }
        }
 }
-
-func parseJolokiaOptions(options *string) (map[string]string, error) {
-       m := make(map[string]string)
-
-       if options == nil || len(*options) == 0 {
-               return m, nil
-       }
-
-       for _, option := range strings.Split(*options, ",") {
-               kv := strings.Split(option, "=")
-               if len(kv) != 2 {
-                       return nil, fmt.Errorf("invalid option [%s] in options 
[%s]", option, *options)
-               }
-               m[kv[0]] = kv[1]
-       }
-
-       return m, nil
-}
diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go
index d0a494e..eedf7f4 100644
--- a/pkg/trait/prometheus.go
+++ b/pkg/trait/prometheus.go
@@ -19,9 +19,7 @@ package trait
 
 import (
        "errors"
-       "fmt"
        "strconv"
-       "strings"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/util/envvar"
@@ -117,7 +115,7 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) 
{
 }
 
 func (t *prometheusTrait) getServiceMonitorFor(e *Environment) 
(*monitoringv1.ServiceMonitor, error) {
-       labels, err := parseLabels(t.Labels)
+       labels, err := parseCsvMap(&t.Labels)
        if err != nil {
                return nil, err
        }
@@ -148,21 +146,3 @@ func (t *prometheusTrait) getServiceMonitorFor(e 
*Environment) (*monitoringv1.Se
        }
        return &smt, nil
 }
-
-func parseLabels(labels string) (map[string]string, error) {
-       m := make(map[string]string)
-
-       if len(labels) == 0 {
-               return m, nil
-       }
-
-       for _, label := range strings.Split(labels, ",") {
-               kv := strings.Split(label, "=")
-               if len(kv) != 2 {
-                       return nil, fmt.Errorf("invalid label [%s] in labels 
[%s]", label, labels)
-               }
-               m[kv[0]] = kv[1]
-       }
-
-       return m, nil
-}
diff --git a/pkg/trait/util.go b/pkg/trait/util.go
index 14554e7..3d58f74 100644
--- a/pkg/trait/util.go
+++ b/pkg/trait/util.go
@@ -19,6 +19,8 @@ package trait
 
 import (
        "context"
+       "fmt"
+       "regexp"
        "strings"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -99,3 +101,26 @@ func VisitKeyValConfigurations(
                }
        }
 }
+
+var (
+       csvMapRegexp = regexp.MustCompile(`^(\w+)=([^,]+)(?:,(\w+)=([^,]+))*$`)
+)
+
+func parseCsvMap(csvMap *string) (map[string]string, error) {
+       m := make(map[string]string)
+
+       if csvMap == nil || len(*csvMap) == 0 {
+               return m, nil
+       }
+
+       if !csvMapRegexp.MatchString(*csvMap) {
+               return nil, fmt.Errorf("cannot parse [%s] as CSV map", *csvMap)
+       }
+
+       matches := csvMapRegexp.FindStringSubmatch(*csvMap)[2:]
+       for i := range matches[:len(matches)-1] {
+               m[matches[i]] = matches[i+1]
+       }
+
+       return m, nil
+}

Reply via email to