-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplugin.go
More file actions
65 lines (57 loc) · 1.79 KB
/
Copy pathplugin.go
File metadata and controls
65 lines (57 loc) · 1.79 KB
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 Tencent.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
// Package plugin defines gateway plugins.
package plugin
import (
"errors"
"reflect"
"gopkg.in/yaml.v3"
gerrs "trpc.group/trpc-go/trpc-gateway/common/errs"
"trpc.group/trpc-go/trpc-go/plugin"
)
//go:generate mockgen -destination ./mock/gatewayplugin.go . GatewayPlugin
//go:generate mockgen -destination ./mock/trpcplugin.go trpc.group/trpc-go/trpc-go/plugin Factory
//go:generate mockgen -destination ./mock/decoder.go trpc.group/trpc-go/trpc-go/plugin Decoder
// GatewayPlugin 网关插件
type GatewayPlugin interface {
// Factory embed tRPC plugin
plugin.Factory
// CheckConfig 网关插件配置校验,并进行默认值的初始化
CheckConfig(name string, dec plugin.Decoder) error
}
// PropsDecoder 解析插件配置
type PropsDecoder struct {
// 原始 props,类型为 map[string]interface{}
Props interface{}
// decode 之后的 props,类型为插件里定义的
DecodedProps interface{}
}
// Decode 解析插件配置
func (d *PropsDecoder) Decode(cfg interface{}) error {
if d.Props == nil {
d.Props = make(map[string]interface{})
}
// 判断,需要是指针类型
if reflect.ValueOf(cfg).Kind() != reflect.Ptr {
return errors.New("need pointer cfg")
}
props, err := yaml.Marshal(d.Props)
if err != nil {
return gerrs.Wrap(err, "marshal plugin props err")
}
if err = yaml.Unmarshal(props, cfg); err != nil {
return gerrs.Wrap(err, "unmarshal plugin props err")
}
d.DecodedProps = cfg
return nil
}