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")) + }) }) }) })