-
Notifications
You must be signed in to change notification settings - Fork 917
/
definition.go
137 lines (121 loc) · 4.14 KB
/
definition.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
/*
* 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 definition
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
)
// ServiceDefiner is a interface of service's definition
type ServiceDefiner interface {
ToBytes() ([]byte, error)
}
// ServiceDefinition is the describer of service definition
type ServiceDefinition struct {
CanonicalName string
CodeSource string
Methods []MethodDefinition
Types []TypeDefinition
}
// ToBytes convert ServiceDefinition to json string
func (def *ServiceDefinition) ToBytes() ([]byte, error) {
return json.Marshal(def)
}
// String will iterate all methods and parameters and convert them to json string
func (def *ServiceDefinition) String() string {
var methodStr strings.Builder
for _, m := range def.Methods {
var paramType strings.Builder
for _, p := range m.ParameterTypes {
paramType.WriteString(fmt.Sprintf("{type:%v}", p))
}
var param strings.Builder
for _, d := range m.Parameters {
param.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName))
}
methodStr.WriteString(fmt.Sprintf("{name:%v,parameterTypes:[%v],returnType:%v,params:[%v] }", m.Name, paramType.String(), m.ReturnType, param.String()))
}
var types strings.Builder
for _, d := range def.Types {
types.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName))
}
return fmt.Sprintf("{canonicalName:%v, codeSource:%v, methods:[%v], types:[%v]}", def.CanonicalName, def.CodeSource, methodStr.String(), types.String())
}
// FullServiceDefinition is the describer of service definition with parameters
type FullServiceDefinition struct {
ServiceDefinition
Params map[string]string
}
// MethodDefinition is the describer of method definition
type MethodDefinition struct {
Name string
ParameterTypes []string
ReturnType string
Parameters []TypeDefinition
}
// TypeDefinition is the describer of type definition
type TypeDefinition struct {
ID string
Type string
Items []TypeDefinition
Enums []string
Properties map[string]TypeDefinition
TypeBuilderName string
}
// BuildServiceDefinition can build service definition which will be used to describe a service
func BuildServiceDefinition(service common.Service, url *common.URL) *ServiceDefinition {
sd := &ServiceDefinition{}
sd.CanonicalName = url.Service()
for k, m := range service.Method() {
var paramTypes []string
if len(m.ArgsType()) > 0 {
for _, t := range m.ArgsType() {
paramTypes = append(paramTypes, t.Kind().String())
}
}
var returnType string
if m.ReplyType() != nil {
returnType = m.ReplyType().Kind().String()
}
methodD := MethodDefinition{
Name: k,
ParameterTypes: paramTypes,
ReturnType: returnType,
}
sd.Methods = append(sd.Methods, methodD)
}
return sd
}
// ServiceDescriperBuild: build the service key, format is `group/serviceName:version` which be same as URL's service key
func ServiceDescriperBuild(serviceName string, group string, version string) string {
buf := &bytes.Buffer{}
if group != "" {
buf.WriteString(group)
buf.WriteString(constant.PathSeparator)
}
buf.WriteString(serviceName)
if version != "" && version != "0.0.0" {
buf.WriteString(constant.KeySeparator)
buf.WriteString(version)
}
return buf.String()
}