@@ -10,8 +10,6 @@ import (
1010 "time"
1111
1212 "github.com/incubator4/go-resty-expr/expr"
13-
14- "github.com/api7/api7-ingress-controller/api/common"
1513)
1614
1715const (
@@ -115,16 +113,16 @@ type Service struct {
115113type Route struct {
116114 Metadata `json:",inline" yaml:",inline"`
117115
118- EnableWebsocket * bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
119- FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
120- Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
121- Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
122- Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
123- Priority * int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
124- RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
125- Timeout * Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
126- Uris []string `json:"uris" yaml:"uris"`
127- Vars common. Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
116+ EnableWebsocket * bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
117+ FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
118+ Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
119+ Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
120+ Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
121+ Priority * int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
122+ RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
123+ Timeout * Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
124+ Uris []string `json:"uris" yaml:"uris"`
125+ Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
128126}
129127
130128type Timeout struct {
@@ -502,3 +500,49 @@ type ResponseData struct {
502500 Value map [string ]any `json:"value"`
503501 ErrorMsg string `json:"error_msg"`
504502}
503+
504+ // Vars represents the route match expressions of APISIX.
505+ type Vars [][]StringOrSlice
506+
507+ // UnmarshalJSON implements json.Unmarshaler interface.
508+ // lua-cjson doesn't distinguish empty array and table,
509+ // and by default empty array will be encoded as '{}'.
510+ // We have to maintain the compatibility.
511+ func (vars * Vars ) UnmarshalJSON (p []byte ) error {
512+ if p [0 ] == '{' {
513+ if len (p ) != 2 {
514+ return errors .New ("unexpected non-empty object" )
515+ }
516+ return nil
517+ }
518+ var data [][]StringOrSlice
519+ if err := json .Unmarshal (p , & data ); err != nil {
520+ return err
521+ }
522+ * vars = data
523+ return nil
524+ }
525+
526+ // StringOrSlice represents a string or a string slice.
527+ // TODO Do not use interface{} to avoid the reflection overheads.
528+ type StringOrSlice struct {
529+ StrVal string `json:"-"`
530+ SliceVal []StringOrSlice `json:"-"`
531+ }
532+
533+ func (s * StringOrSlice ) MarshalJSON () ([]byte , error ) {
534+ if s .SliceVal != nil {
535+ return json .Marshal (s .SliceVal )
536+ }
537+ return json .Marshal (s .StrVal )
538+ }
539+
540+ func (s * StringOrSlice ) UnmarshalJSON (p []byte ) error {
541+ if len (p ) == 0 {
542+ return errors .New ("empty object" )
543+ }
544+ if p [0 ] == '[' {
545+ return json .Unmarshal (p , & s .SliceVal )
546+ }
547+ return json .Unmarshal (p , & s .StrVal )
548+ }
0 commit comments