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

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new d293b4f0 [operator] Supplemental setpath map logic
d293b4f0 is described below

commit d293b4f0a03412683d2a23f33478ef503bf946f4
Author: mfordjody <[email protected]>
AuthorDate: Fri Dec 13 19:04:46 2024 +0800

    [operator] Supplemental setpath map logic
---
 operator/pkg/render/manifest.go |  1 +
 operator/pkg/values/map.go      | 86 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/operator/pkg/render/manifest.go b/operator/pkg/render/manifest.go
index 93d11f05..e60af13d 100644
--- a/operator/pkg/render/manifest.go
+++ b/operator/pkg/render/manifest.go
@@ -45,6 +45,7 @@ func MergeInputs(filenames []string, flags []string) 
([]values.Map, error) {
                if m["spec"] == nil {
                        delete(m, "spec")
                }
+               ConfigBase.MergeFrom(m)
        }
        return nil, nil
 }
diff --git a/operator/pkg/values/map.go b/operator/pkg/values/map.go
index ab961509..3cd7c5cf 100644
--- a/operator/pkg/values/map.go
+++ b/operator/pkg/values/map.go
@@ -255,3 +255,89 @@ func (m Map) MergeFrom(other Map) {
                m[k] = v
        }
 }
+
+func (m Map) SetPath(paths string, value any) error {
+       path := splitPath(paths)
+       base := m
+       if err := setPathRecurse(base, path, value); err != nil {
+               return err
+       }
+       return nil
+}
+
+func (m Map) SetSpecPaths(paths ...string) error {
+       for _, path := range paths {
+               if err := m.SetPaths("spec." + path); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+func setPathRecurse(base map[string]any, paths []string, value any) error {
+       seg := paths[0]
+       last := len(paths) == 1
+       nextIsArray := len(paths) >= 2 && strings.HasPrefix(paths[1], "[")
+       if nextIsArray {
+               last = len(paths) == 2
+               // Find or create target list
+               if _, f := base[seg]; !f {
+                       base[seg] = []any{}
+               }
+               var index int
+               if k, v, ok := extractKV(paths[1]); ok {
+                       index = -1
+                       for idx, cm := range base[seg].([]any) {
+                               if MustCastAsMap(cm)[k] == v {
+                                       index = idx
+                                       break
+                               }
+                       }
+                       if index == -1 {
+                               return fmt.Errorf("element %v not found", 
paths[1])
+                       }
+               } else if idx, ok := extractIndex(paths[1]); ok {
+                       index = idx
+               } else {
+                       return fmt.Errorf("unknown segment %v", paths[1])
+               }
+               l := base[seg].([]any)
+               if index < 0 || index >= len(l) {
+                       // Index is greater, we need to append
+                       if last {
+                               l = append(l, value)
+                       } else {
+                               nm := Map{}
+                               if err := setPathRecurse(nm, paths[2:], value); 
err != nil {
+                                       return err
+                               }
+                               l = append(l, nm)
+                       }
+                       base[seg] = l
+               } else {
+                       v := MustCastAsMap(l[index])
+                       if err := setPathRecurse(v, paths[2:], value); err != 
nil {
+                               return err
+                       }
+                       l[index] = v
+               }
+       } else {
+               if _, f := base[seg]; !f {
+                       base[seg] = map[string]any{}
+               }
+               if last {
+                       base[seg] = value
+               } else {
+                       return setPathRecurse(MustCastAsMap(base[seg]), 
paths[1:], value)
+               }
+       }
+       return nil
+}
+
+func extractKV(seg string) (string, string, bool) {
+       if !strings.HasPrefix(seg, "[") || !strings.HasSuffix(seg, "]") {
+               return "", "", false
+       }
+       sanitized := seg[1 : len(seg)-1]
+       return strings.Cut(sanitized, ":")
+}

Reply via email to