You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/08/04 04:48:26 UTC

[GitHub] [dubbo-go] AlexStocks commented on a diff in pull request #1963: [Ftr] Enhance ServiceDefinition in MetadataService

AlexStocks commented on code in PR #1963:
URL: https://github.com/apache/dubbo-go/pull/1963#discussion_r937343210


##########
metadata/definition/definition.go:
##########
@@ -121,6 +122,55 @@ func BuildServiceDefinition(service common.Service, url *common.URL) *ServiceDef
 	return sd
 }
 
+// BuildFullDefinition can build service definition with full url parameters
+func BuildFullDefinition(service common.Service, url *common.URL) *FullServiceDefinition {
+	fsd := &FullServiceDefinition{}
+	sd := BuildServiceDefinition(service, url)
+	fsd.ServiceDefinition = *sd
+	fsd.Parameters = make(map[string]string)
+	for k, v := range url.GetParams() {
+		fsd.Parameters[k] = strings.Join(v, ",")
+	}
+	return fsd
+}
+
+// ToBytes convert ServiceDefinition to json string
+func (def *FullServiceDefinition) ToBytes() ([]byte, error) {
+	return json.Marshal(def)
+}
+
+// String will iterate all methods and parameters and convert them to json string
+func (def *FullServiceDefinition) 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))
+	}
+
+	sortSlice := make([]string, 0)
+	var parameters strings.Builder
+	for k := range def.Parameters {
+		sortSlice = append(sortSlice, k)
+	}
+	sort.Slice(sortSlice, func(i, j int) bool { return sortSlice[i] < sortSlice[j] })
+	for _, k := range sortSlice {
+		parameters.WriteString(fmt.Sprintf("%v:%v,", k, def.Parameters[k]))
+	}
+
+	return fmt.Sprintf("{parameters:{%v}, canonicalName:%v, codeSource:%v, methods:[%v], types:[%v]}", strings.TrimRight(parameters.String(), ","), def.CanonicalName, def.CodeSource, methodStr.String(), types.String())

Review Comment:
   so long, pls split it into two lines.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org