-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
76 lines (64 loc) · 1.63 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gors
import (
"context"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
)
type Options struct {
Tag string
DisableDefaultTag bool
ResponseWrapper func(resp any) any
ErrorHandler func(ctx context.Context, err error) error
IncomingHeaderMatcher func(key string) (string, bool)
OutgoingHeaderMatcher func(key string) (string, bool)
MetadataAnnotators []func(ctx context.Context) metadata.MD
ProtoJSONMarshalOptions protojson.MarshalOptions
}
type Option func(o *Options)
func NewOptions(opts ...Option) *Options {
o := &Options{}
for _, opt := range opts {
opt(o)
}
return o
}
func Tag(tag string) Option {
return func(o *Options) {
o.Tag = tag
}
}
func DisableDefaultTag() Option {
return func(o *Options) {
o.DisableDefaultTag = true
}
}
func ResponseWrapper(w func(resp any) any) Option {
return func(o *Options) {
o.ResponseWrapper = w
}
}
func ErrorHandler(h func(ctx context.Context, err error) error) Option {
return func(o *Options) {
o.ErrorHandler = h
}
}
func IncomingHeaderMatcher(m func(key string) (string, bool)) Option {
return func(o *Options) {
o.IncomingHeaderMatcher = m
}
}
func OutgoingHeaderMatcher(m func(key string) (string, bool)) Option {
return func(o *Options) {
o.OutgoingHeaderMatcher = m
}
}
func MetadataAnnotator(a ...func(ctx context.Context) metadata.MD) Option {
return func(o *Options) {
o.MetadataAnnotators = append(o.MetadataAnnotators, a...)
}
}
func ProtoJSONMarshalOptions(mo protojson.MarshalOptions) Option {
return func(o *Options) {
o.ProtoJSONMarshalOptions = mo
}
}