-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (61 loc) · 1.62 KB
/
config.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
package main
import (
"errors"
"fmt"
xds "github.com/cncf/xds/go/xds/type/v3"
"github.com/envoyproxy/envoy/contrib/golang/filters/http/source/go/pkg/api"
"github.com/envoyproxy/envoy/contrib/golang/filters/http/source/go/pkg/http"
"google.golang.org/protobuf/types/known/anypb"
)
const Name = "simple"
func init() {
http.RegisterHttpFilterConfigFactory(Name, ConfigFactory)
http.RegisterHttpFilterConfigParser(&parser{})
}
type config struct {
echoBody string
// other fields
}
type parser struct {
}
func (p *parser) Parse(any *anypb.Any) (interface{}, error) {
configStruct := &xds.TypedStruct{}
if err := any.UnmarshalTo(configStruct); err != nil {
return nil, err
}
v := configStruct.Value
conf := &config{}
prefix, ok := v.AsMap()["prefix_localreply_body"]
if !ok {
return nil, errors.New("missing prefix_localreply_body")
}
if str, ok := prefix.(string); ok {
conf.echoBody = str
} else {
return nil, fmt.Errorf("prefix_localreply_body: expect string while got %T", prefix)
}
return conf, nil
}
func (p *parser) Merge(parent interface{}, child interface{}) interface{} {
parentConfig := parent.(*config)
childConfig := child.(*config)
// copy one, do not update parentConfig directly.
newConfig := *parentConfig
if childConfig.echoBody != "" {
newConfig.echoBody = childConfig.echoBody
}
return &newConfig
}
func ConfigFactory(c interface{}) api.StreamFilterFactory {
conf, ok := c.(*config)
if !ok {
panic("unexpected config type")
}
return func(callbacks api.FilterCallbackHandler) api.StreamFilter {
return &filter{
callbacks: callbacks,
config: conf,
}
}
}
func main() {}