-
Notifications
You must be signed in to change notification settings - Fork 917
/
rest_config_reader.go
158 lines (142 loc) · 5.33 KB
/
rest_config_reader.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reader
import (
"bytes"
"strconv"
"strings"
)
import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config/interfaces"
"dubbo.apache.org/dubbo-go/v3/protocol/rest/config"
)
const REST = "rest"
func init() {
extension.SetConfigReaders(REST, NewRestConfigReader)
extension.SetDefaultConfigReader(REST, REST)
}
type RestConfigReader struct{}
func NewRestConfigReader() interfaces.ConfigReader {
return &RestConfigReader{}
}
// ReadConsumerConfig read consumer config for rest protocol
func (cr *RestConfigReader) ReadConsumerConfig(reader *bytes.Buffer) error {
restConsumerConfig := &config.RestConsumerConfig{}
err := yaml.Unmarshal(reader.Bytes(), restConsumerConfig)
if err != nil {
return perrors.Errorf("[Rest ShutdownConfig] unmarshal Consumer error %#v", perrors.WithStack(err))
}
restConsumerServiceConfigMap := make(map[string]*config.RestServiceConfig, len(restConsumerConfig.RestServiceConfigsMap))
for key, rc := range restConsumerConfig.RestServiceConfigsMap {
rc.Client = getNotEmptyStr(rc.Client, restConsumerConfig.Client, constant.DEFAULT_REST_CLIENT)
rc.RestMethodConfigsMap = initMethodConfigMap(rc, restConsumerConfig.Consumes, restConsumerConfig.Produces)
restConsumerServiceConfigMap[key] = rc
}
config.SetRestConsumerServiceConfigMap(restConsumerServiceConfigMap)
return nil
}
// ReadProviderConfig read provider config for rest protocol
func (cr *RestConfigReader) ReadProviderConfig(reader *bytes.Buffer) error {
restProviderConfig := &config.RestProviderConfig{}
err := yaml.Unmarshal(reader.Bytes(), restProviderConfig)
if err != nil {
return perrors.Errorf("[Rest ShutdownConfig] unmarshal Provider error %#v", perrors.WithStack(err))
}
restProviderServiceConfigMap := make(map[string]*config.RestServiceConfig, len(restProviderConfig.RestServiceConfigsMap))
for key, rc := range restProviderConfig.RestServiceConfigsMap {
rc.Server = getNotEmptyStr(rc.Server, restProviderConfig.Server, constant.DEFAULT_REST_SERVER)
rc.RestMethodConfigsMap = initMethodConfigMap(rc, restProviderConfig.Consumes, restProviderConfig.Produces)
restProviderServiceConfigMap[key] = rc
}
config.SetRestProviderServiceConfigMap(restProviderServiceConfigMap)
return nil
}
// initProviderRestConfig ...
func initMethodConfigMap(rc *config.RestServiceConfig, consumes string, produces string) map[string]*config.RestMethodConfig {
mcm := make(map[string]*config.RestMethodConfig, len(rc.RestMethodConfigs))
for _, mc := range rc.RestMethodConfigs {
mc.InterfaceName = rc.InterfaceName
mc.Path = rc.Path + mc.Path
mc.Consumes = getNotEmptyStr(mc.Consumes, rc.Consumes, consumes)
mc.Produces = getNotEmptyStr(mc.Produces, rc.Produces, produces)
mc.MethodType = getNotEmptyStr(mc.MethodType, rc.MethodType)
mc = transformMethodConfig(mc)
mcm[mc.MethodName] = mc
}
return mcm
}
// function will return first not empty string ..
func getNotEmptyStr(args ...string) string {
var r string
for _, t := range args {
if len(t) > 0 {
r = t
break
}
}
return r
}
// transformMethodConfig
func transformMethodConfig(methodConfig *config.RestMethodConfig) *config.RestMethodConfig {
if len(methodConfig.PathParamsMap) == 0 && len(methodConfig.PathParams) > 0 {
paramsMap, err := parseParamsString2Map(methodConfig.PathParams)
if err != nil {
logger.Warnf("[Rest ShutdownConfig] Path Param parse error:%v", err)
} else {
methodConfig.PathParamsMap = paramsMap
}
}
if len(methodConfig.QueryParamsMap) == 0 && len(methodConfig.QueryParams) > 0 {
paramsMap, err := parseParamsString2Map(methodConfig.QueryParams)
if err != nil {
logger.Warnf("[Rest ShutdownConfig] Argument Param parse error:%v", err)
} else {
methodConfig.QueryParamsMap = paramsMap
}
}
if len(methodConfig.HeadersMap) == 0 && len(methodConfig.Headers) > 0 {
headersMap, err := parseParamsString2Map(methodConfig.Headers)
if err != nil {
logger.Warnf("[Rest ShutdownConfig] Argument Param parse error:%v", err)
} else {
methodConfig.HeadersMap = headersMap
}
}
return methodConfig
}
// transform a string to a map
// for example:
// string "0:id,1:name" => map [0:id,1:name]
func parseParamsString2Map(params string) (map[int]string, error) {
m := make(map[int]string, 8)
for _, p := range strings.Split(params, ",") {
pa := strings.Split(p, ":")
key, err := strconv.Atoi(pa[0])
if err != nil {
return nil, err
}
m[key] = pa[1]
}
return m, nil
}