forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_plugin.go
executable file
·72 lines (58 loc) · 1.69 KB
/
custom_plugin.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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// 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 is the plugin package.
package plugin
import (
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/plugin"
)
const (
pluginName = "custom"
pluginType = "custom"
)
func init() {
plugin.Register(pluginName, &customPlugin{})
}
// customPlugin struct implements plugin.Factory interface.
type customPlugin struct {
config customConfig
}
var c customPlugin
// customConfig plugin config
type customConfig struct {
Test string `yaml:"test"`
TestObj struct {
Key1 string `yaml:"key1"`
Key2 bool `yaml:"key2"`
Key3 int32 `yaml:"key3"`
} `yaml:"test_obj"`
}
// Type return plugin type
func (custom *customPlugin) Type() string {
return pluginType
}
// Setup init plugin
// trpc will call Setup function to init plugin.
func (custom *customPlugin) Setup(name string, decoder plugin.Decoder) error {
if err := decoder.Decode(&c.config); err != nil {
return err
}
log.Infof("[plugin] init customPlugin success, config: %v", c.config)
return nil
}
// Record is a custom plugin function
// you can call this function in your code print plugin config.
func Record() {
log.Infof("[plugin] call key1 : %s, key2 : %t, key3 : %d",
c.config.TestObj.Key1, c.config.TestObj.Key2, c.config.TestObj.Key3)
}