Skip to content

Commit 67bffff

Browse files
committed
update
1 parent f3c2a40 commit 67bffff

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

context_x_response.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111
"unicode"
1212

13-
"github.com/admpub/xencoding/filter"
1413
json "github.com/admpub/xencoding/json/standard"
1514
xml "github.com/admpub/xencoding/xml/standard"
1615
"github.com/webx-top/echo/engine"
@@ -100,11 +99,11 @@ func (c *XContext) JSON(i interface{}, codes ...int) (err error) {
10099
}
101100
}
102101
var b []byte
103-
if ft, ok := c.route.Get(metaKeyEncodingFilter).(EncodingFilter); ok {
102+
if ft, ok := c.route.Get(metaKeyEncodingConfig).(EncodingConfig); ok {
104103
b, err = json.MarshalWithOption(
105104
i,
106-
json.OptionFilter(filter.Exclude(ft.OmitFields...)),
107-
json.OptionSelector(filter.Include(ft.OnlyFields...)),
105+
json.OptionFilter(ft.filter),
106+
json.OptionSelector(ft.selector),
108107
)
109108
} else {
110109
b, err = json.Marshal(i)
@@ -132,11 +131,11 @@ func (c *XContext) JSONP(callback string, i interface{}, codes ...int) (err erro
132131
}
133132
}
134133
var b []byte
135-
if ft, ok := c.route.Get(metaKeyEncodingFilter).(EncodingFilter); ok {
134+
if ft, ok := c.route.Get(metaKeyEncodingConfig).(EncodingConfig); ok {
136135
b, err = json.MarshalWithOption(
137136
i,
138-
json.OptionFilter(filter.Exclude(ft.OmitFields...)),
139-
json.OptionSelector(filter.Include(ft.OnlyFields...)),
137+
json.OptionFilter(ft.filter),
138+
json.OptionSelector(ft.selector),
140139
)
141140
} else {
142141
b, err = json.Marshal(i)
@@ -159,11 +158,11 @@ func (c *XContext) XML(i interface{}, codes ...int) (err error) {
159158
}
160159
}
161160
var b []byte
162-
if ft, ok := c.route.Get(metaKeyEncodingFilter).(EncodingFilter); ok {
161+
if ft, ok := c.route.Get(metaKeyEncodingConfig).(EncodingConfig); ok {
163162
b, err = xml.MarshalWithOption(
164163
i,
165-
xml.OptionFilter(filter.Exclude(ft.OmitFields...)),
166-
xml.OptionSelector(filter.Include(ft.OnlyFields...)),
164+
xml.OptionFilter(ft.filter),
165+
xml.OptionSelector(ft.selector),
167166
)
168167
} else {
169168
b, err = xml.Marshal(i)

echo.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/admpub/log"
1313
"github.com/admpub/realip"
14+
"github.com/admpub/xencoding/filter"
1415

1516
"github.com/webx-top/echo/engine"
1617
"github.com/webx-top/echo/logger"
@@ -100,9 +101,9 @@ type (
100101
Template(Context) (string, error)
101102
}
102103

103-
EncodingFilter struct {
104-
OmitFields []string
105-
OnlyFields []string
104+
EncodingConfig struct {
105+
filter filter.Filter
106+
selector filter.Selector
106107
}
107108
)
108109

@@ -136,6 +137,24 @@ func (h HandlerFuncWithArg[R, W]) Handle(c Context) error {
136137
return c.Render(``, w)
137138
}
138139

140+
func (c EncodingConfig) SetFilter(f filter.Filter) EncodingConfig {
141+
c.filter = f
142+
return c
143+
}
144+
145+
func (c EncodingConfig) SetSelector(f filter.Selector) EncodingConfig {
146+
c.selector = f
147+
return c
148+
}
149+
150+
func (c EncodingConfig) Filter() filter.Filter {
151+
return c.filter
152+
}
153+
154+
func (c EncodingConfig) Selector() filter.Selector {
155+
return c.selector
156+
}
157+
139158
// New creates an instance of Echo.
140159
func New() (e *Echo) {
141160
return NewWithContext(func(e *Echo) Context {

router.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strings"
1010

11+
"github.com/admpub/xencoding/filter"
1112
"github.com/webx-top/echo/param"
1213
)
1314

@@ -147,13 +148,53 @@ func (r *Route) SetMetaKV(key string, value interface{}) IRouter {
147148
return r
148149
}
149150

150-
const metaKeyEncodingFilter = `encodingFilter`
151+
const metaKeyEncodingConfig = `encodingConfig`
151152

152-
func (r *Route) SetEncodingFilter(ef EncodingFilter) IRouter {
153+
func (r *Route) SetEncodingConfig(ef EncodingConfig) IRouter {
153154
if r.Meta == nil {
154155
r.Meta = H{}
155156
}
156-
r.Meta[metaKeyEncodingFilter] = ef
157+
r.Meta[metaKeyEncodingConfig] = ef
158+
return r
159+
}
160+
161+
func (r *Route) SetEncodingOmitFields(names ...string) IRouter {
162+
if r.Meta == nil {
163+
r.Meta = H{}
164+
r.Meta[metaKeyEncodingConfig] = EncodingConfig{
165+
filter: filter.Exclude(names...),
166+
}
167+
return r
168+
}
169+
cfg, ok := r.Meta[metaKeyEncodingConfig].(EncodingConfig)
170+
if !ok {
171+
cfg = EncodingConfig{
172+
filter: filter.Exclude(names...),
173+
}
174+
} else {
175+
cfg.filter = filter.Exclude(names...)
176+
}
177+
r.Meta[metaKeyEncodingConfig] = cfg
178+
return r
179+
}
180+
181+
func (r *Route) SetEncodingOnlyFields(names ...string) IRouter {
182+
if r.Meta == nil {
183+
r.Meta = H{}
184+
r.Meta[metaKeyEncodingConfig] = EncodingConfig{
185+
selector: filter.Include(names...),
186+
}
187+
return r
188+
}
189+
cfg, ok := r.Meta[metaKeyEncodingConfig].(EncodingConfig)
190+
if !ok {
191+
cfg = EncodingConfig{
192+
selector: filter.Include(names...),
193+
}
194+
} else {
195+
cfg.selector = filter.Include(names...)
196+
}
197+
r.Meta[metaKeyEncodingConfig] = cfg
157198
return r
158199
}
159200

0 commit comments

Comments
 (0)