Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions doc/yaml_struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -236,7 +238,7 @@ kind:Plugin
selector:
labels:
app: foo
sort:
sets:
- name: proxy-rewrite
conf:
uri: "/test/home.html"
Expand Down
15 changes: 15 additions & 0 deletions pkg/yml/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
18 changes: 18 additions & 0 deletions pkg/yml/trans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
31 changes: 31 additions & 0 deletions pkg/yml/trans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
})
})
Expand Down