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

kvn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-control-plane.git


The following commit(s) were added to refs/heads/master by this push:
     new 8585d67  feat: add plugin yaml transform (#10)
8585d67 is described below

commit 8585d674b9bc1aa63f7301bfd09c3d7fc5113049
Author: kv <[email protected]>
AuthorDate: Thu Aug 13 18:54:03 2020 +0800

    feat: add plugin yaml transform (#10)
---
 doc/yaml_struct.md    | 24 +++++++++++++-----------
 pkg/yml/model.go      | 15 +++++++++++++++
 pkg/yml/trans.go      | 18 ++++++++++++++++++
 pkg/yml/trans_test.go | 31 +++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 11 deletions(-)

diff --git a/doc/yaml_struct.md b/doc/yaml_struct.md
index 2579a75..5e46d01 100644
--- a/doc/yaml_struct.md
+++ b/doc/yaml_struct.md
@@ -121,18 +121,20 @@ subsets:
 The logic of the plugin itself is not defined here, only known plugins will be 
referenced here
 
 ```yaml
-kind:Plugin
-selector:
-  labels:
-     app: foo
-sort:
-- name: limit-count
+kind: Plugin
+selector:
+  app: foo
+sets:
+- name: proxy-rewrite
   conf:
-      max:100
+    uri: "/test/home.html"
+    scheme: "http"
+    host: "foo.com"
+    headers:
+      X-Api-Version: "v1"
+      X-Api-Engine: "apisix"
+      X-Api-useless: ""
 - name: prometheus
-  conf:
-     ...schema..
-
 ```
 
 ### A complete demo
@@ -236,7 +238,7 @@ kind:Plugin
 selector:
   labels:
      app: foo
-sort:
+sets:
 - name: proxy-rewrite
   conf:
     uri: "/test/home.html"
diff --git a/pkg/yml/model.go b/pkg/yml/model.go
index 4d381da..1920b35 100644
--- a/pkg/yml/model.go
+++ b/pkg/yml/model.go
@@ -91,3 +91,18 @@ type Subset struct {
 func (g *Destination) ToMem() string {
        return "destination"
 }
+
+type Plugin struct {
+       Kind     string            `yaml:"kind"`
+       Selector map[string]string `yaml:"selector"`
+       Sets     []Set             `yaml:"sets"`
+}
+
+type Set struct {
+       Name string                 `yaml:"name"`
+       Conf map[string]interface{} `yaml:"conf,omitempty"`
+}
+
+func (g *Plugin) ToMem() string {
+       return "plugin"
+}
diff --git a/pkg/yml/trans.go b/pkg/yml/trans.go
index 4e81fc7..b3a3390 100644
--- a/pkg/yml/trans.go
+++ b/pkg/yml/trans.go
@@ -56,6 +56,14 @@ func Trans(b []byte, y []byte) YmlModel {
                        } else {
                                return r
                        }
+               case "Plugin":
+                       // trans to Plugin
+                       if r, err := ToPlugin(y); err != nil {
+                               fmt.Println("trans to Plugin error ", err)
+                               return nil
+                       } else {
+                               return r
+                       }
                default:
                        fmt.Println("nil")
                        return nil
@@ -92,3 +100,13 @@ func ToDestination(y []byte) (*Destination, error) {
                return g, nil
        }
 }
+
+func ToPlugin(y []byte) (*Plugin, error) {
+       var g *Plugin
+       if err := yaml.Unmarshal(y, &g); err != nil {
+               fmt.Println(err)
+               return nil, err
+       } else {
+               return g, nil
+       }
+}
diff --git a/pkg/yml/trans_test.go b/pkg/yml/trans_test.go
index ee06609..3c5caa9 100644
--- a/pkg/yml/trans_test.go
+++ b/pkg/yml/trans_test.go
@@ -121,6 +121,37 @@ subsets:
                                Expect(g.Kind).To(Equal("Destination"))
                                Expect(g.Host).To(Equal("foo-server"))
                        })
+
+                       It("trans to plugin no error", func() {
+                               b := []byte(`
+kind: Plugin
+selector:
+  app: foo
+sets:
+- name: proxy-rewrite
+  conf:
+    uri: "/test/home.html"
+    scheme: "http"
+    host: "foo.com"
+    headers:
+      X-Api-Version: "v1"
+      X-Api-Engine: "apisix"
+      X-Api-useless: ""
+- name: prometheus
+`)
+                               y, err := yaml.YAMLToJSON(b)
+                               fmt.Println(string(y))
+                               ym := yml.Trans(y, b)
+                               Expect(err).NotTo(HaveOccurred())
+                               v := typeof(ym)
+                               Expect(v).To(Equal("*yml.Plugin"))
+                               g, ok := ym.(*yml.Plugin)
+                               Expect(ok).To(Equal(true))
+                               Expect(g.Kind).To(Equal("Plugin"))
+                               fmt.Println(g.Sets)
+                               Expect(len(g.Sets)).To(Equal(2))
+                               Expect(g.Selector["app"]).To(Equal("foo"))
+                       })
                })
        })
 })

Reply via email to