feat: add Rule yaml tranformer (#8)
diff --git a/doc/yaml_struct.md b/doc/yaml_struct.md
index ad48339..56527e1 100644
--- a/doc/yaml_struct.md
+++ b/doc/yaml_struct.md
@@ -1,3 +1,22 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
## Currently, 4 types are defined, namely
1.Gateway
@@ -41,12 +60,12 @@
- foo-gw
http:
- route:
- - destination:
+ - destination:
port: 28002
host: foo-server
subset: foo-v1
weight: 10
- label:
+ label:
app: foo
version: v1
match:
diff --git a/pkg/yml/model.go b/pkg/yml/model.go
index f8c73ed..e27382f 100644
--- a/pkg/yml/model.go
+++ b/pkg/yml/model.go
@@ -19,7 +19,6 @@
type YmlModel interface {
ToMem() string
- Type() string
}
type Gateway struct {
@@ -43,6 +42,35 @@
return "gateway"
}
-func (g *Gateway) Type() string {
- return "Gateway"
+type Rule struct {
+ Kind string `json:"kind"`
+ Name string `json:"name"`
+ Hosts []string `json:"hosts"`
+ Gateways []string `json:"gateways"`
+ HTTP []HTTP `json:"http"`
+}
+type Destination struct {
+ Port int64 `json:"port"`
+ Host string `json:"host"`
+ Subset string `json:"subset"`
+ Weight int64 `json:"weight"`
+}
+type Route struct {
+ Destination Destination `json:"destination"`
+}
+type Label map[string]string
+
+type Headers map[string]interface{}
+
+type Match struct {
+ Headers Headers `json:"headers"`
+}
+type HTTP struct {
+ Route []Route `json:"route"`
+ Label Label `json:"label"`
+ Match []Match `json:"match,omitempty"`
+}
+
+func (r *Rule) ToMem() string {
+ return "Rule"
}
diff --git a/pkg/yml/trans.go b/pkg/yml/trans.go
index 696cd40..2aa1977 100644
--- a/pkg/yml/trans.go
+++ b/pkg/yml/trans.go
@@ -38,9 +38,16 @@
fmt.Println("trans to Gateway error ", err)
return nil
} else {
- fmt.Println(g)
return g
}
+ case "Rule":
+ // trans to Rule
+ if r, err := ToRule(y); err != nil {
+ fmt.Println("trans to Rule error ", err)
+ return nil
+ } else {
+ return r
+ }
default:
fmt.Println("nil")
return nil
@@ -57,3 +64,13 @@
return g, nil
}
}
+
+func ToRule(y []byte) (*Rule, error) {
+ var r *Rule
+ if err := yaml.Unmarshal(y, &r); err != nil {
+ fmt.Println(err)
+ return nil, err
+ } else {
+ return r, nil
+ }
+}
diff --git a/pkg/yml/trans_test.go b/pkg/yml/trans_test.go
index d5eca5e..29c085d 100644
--- a/pkg/yml/trans_test.go
+++ b/pkg/yml/trans_test.go
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package yml_test
import (
@@ -10,9 +27,9 @@
var _ = Describe("Trans", func() {
Describe("trans to model", func() {
- var b []byte
- BeforeEach(func() {
- b = []byte(`
+ Context("trans", func() {
+ It("trans to gateway no error", func() {
+ b := []byte(`
kind: Gateway
name: foo-gw
servers:
@@ -24,19 +41,65 @@
- "a.foo.com"
- "b.foo.com"
`)
- fmt.Println("BeforeEach executed")
- })
- Context("trans", func() {
- It("trans to gateway no error", func() {
y, err := yaml.YAMLToJSON(b)
fmt.Println(string(y))
ym := yml.Trans(y, b)
Expect(err).NotTo(HaveOccurred())
- Expect(ym.Type()).To(Equal("Gateway"))
+ v := typeof(ym)
+ fmt.Println(v)
+ Expect(v).To(Equal("*yml.Gateway"))
g, ok := ym.(*yml.Gateway)
Expect(ok).To(Equal(true))
Expect(len(g.Servers[0].Hosts)).To(Equal(2))
})
+
+ It("trans to rule no error", func() {
+ b := []byte(`
+kind: Rule
+name: xxx-rules
+hosts:
+- "a.foo.com"
+gateways:
+- foo-gw
+http:
+- route:
+ - destination:
+ port: 28002
+ host: foo-server
+ subset: foo-v1
+ weight: 10
+ label:
+ app: foo
+ version: v1
+ match:
+ - headers:
+ product_id:
+ exact: v1
+- route:
+ - destination:
+ port: 28002
+ host: foo-server
+ subset: v2
+ label:
+ 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"))
+ })
})
})
})
+
+func typeof(v interface{}) string {
+ return fmt.Sprintf("%T", v)
+}