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 c175405  feat: add Destination yaml tranformer (#9)
c175405 is described below

commit c175405ce3eb4ca01b4439389b8e152caf1a3978
Author: kv <[email protected]>
AuthorDate: Thu Aug 13 14:25:11 2020 +0800

    feat: add Destination yaml tranformer (#9)
---
 doc/yaml_struct.md    |  7 +++----
 pkg/yml/model.go      | 21 +++++++++++++++++++--
 pkg/yml/trans.go      | 18 ++++++++++++++++++
 pkg/yml/trans_test.go | 29 +++++++++++++++++++++++++++--
 4 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/doc/yaml_struct.md b/doc/yaml_struct.md
index 56527e1..2579a75 100644
--- a/doc/yaml_struct.md
+++ b/doc/yaml_struct.md
@@ -96,18 +96,17 @@ http:
 ### Define the target service
 
 ```yaml
-kind: destinations
+kind: Destination
 name: foo-dest
 host: foo-server
 subsets:
 - name: foo-v1
-  ips:
+  ips:
   - 127.0.0.1
   - 127.0.0.2
 - name: v2
   selector:
-    labels:
-      tag: v2
+       tag: v2
 
 ```
 
diff --git a/pkg/yml/model.go b/pkg/yml/model.go
index e27382f..4d381da 100644
--- a/pkg/yml/model.go
+++ b/pkg/yml/model.go
@@ -49,14 +49,14 @@ type Rule struct {
        Gateways []string `json:"gateways"`
        HTTP     []HTTP   `json:"http"`
 }
-type Destination struct {
+type RouteDestination struct {
        Port   int64  `json:"port"`
        Host   string `json:"host"`
        Subset string `json:"subset"`
        Weight int64  `json:"weight"`
 }
 type Route struct {
-       Destination Destination `json:"destination"`
+       Destination RouteDestination `json:"destination"`
 }
 type Label map[string]string
 
@@ -74,3 +74,20 @@ type HTTP struct {
 func (r *Rule) ToMem() string {
        return "Rule"
 }
+
+type Destination struct {
+       Kind    string   `json:"kind"`
+       Name    string   `json:"name"`
+       Host    string   `json:"host"`
+       Subsets []Subset `json:"subsets"`
+}
+
+type Subset struct {
+       Name     string            `json:"name"`
+       Ips      []string          `json:"ips,omitempty"`
+       Selector map[string]string `json:"selector,omitempty"`
+}
+
+func (g *Destination) ToMem() string {
+       return "destination"
+}
diff --git a/pkg/yml/trans.go b/pkg/yml/trans.go
index 2aa1977..4e81fc7 100644
--- a/pkg/yml/trans.go
+++ b/pkg/yml/trans.go
@@ -40,6 +40,14 @@ func Trans(b []byte, y []byte) YmlModel {
                        } else {
                                return g
                        }
+               case "Destination":
+                       // trans to Destination
+                       if g, err := ToDestination(y); err != nil {
+                               fmt.Println("trans to destination error ", err)
+                               return nil
+                       } else {
+                               return g
+                       }
                case "Rule":
                        // trans to Rule
                        if r, err := ToRule(y); err != nil {
@@ -74,3 +82,13 @@ func ToRule(y []byte) (*Rule, error) {
                return r, nil
        }
 }
+
+func ToDestination(y []byte) (*Destination, error) {
+       var g *Destination
+       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 29c085d..ee06609 100644
--- a/pkg/yml/trans_test.go
+++ b/pkg/yml/trans_test.go
@@ -46,7 +46,6 @@ servers:
                                ym := yml.Trans(y, b)
                                Expect(err).NotTo(HaveOccurred())
                                v := typeof(ym)
-                               fmt.Println(v)
                                Expect(v).To(Equal("*yml.Gateway"))
                                g, ok := ym.(*yml.Gateway)
                                Expect(ok).To(Equal(true))
@@ -84,18 +83,44 @@ http:
     app: foo
     version: v2
 `)
+
                                y, err := yaml.YAMLToJSON(b)
                                fmt.Println(string(y))
                                ym := yml.Trans(y, b)
                                Expect(err).NotTo(HaveOccurred())
                                v := typeof(ym)
-                               fmt.Println(v)
                                Expect(v).To(Equal("*yml.Rule"))
                                r, ok := ym.(*yml.Rule)
                                Expect(ok).To(Equal(true))
                                Expect(r.Kind).To(Equal("Rule"))
                                Expect(r.Kind).To(Equal("Rule"))
                        })
+
+                       It("trans to destination no error", func() {
+                               b := []byte(`
+kind: Destination
+name: foo-dest
+host: foo-server
+subsets:
+- name: foo-v1
+  ips:
+  - 127.0.0.1
+  - 127.0.0.2
+- name: v2
+  selector:
+    tag: v2
+`)
+                               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.Destination"))
+                               g, ok := ym.(*yml.Destination)
+                               Expect(ok).To(Equal(true))
+                               Expect(g.Kind).To(Equal("Destination"))
+                               Expect(g.Host).To(Equal("foo-server"))
+                       })
                })
        })
 })

Reply via email to