Skip to content
Snippets Groups Projects
definition.go 3.89 KiB
Newer Older
/*
 * 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

vito.he's avatar
vito.he committed
import (
	"bytes"
vito.he's avatar
vito.he committed
	"strings"
vito.he's avatar
vito.he committed
)

import (
vito.he's avatar
vito.he committed
	"github.com/apache/dubbo-go/common"
vito.he's avatar
vito.he committed
	"github.com/apache/dubbo-go/common/constant"
vito.he's avatar
vito.he committed
)

vito.he's avatar
vito.he committed
// ServiceDefiner is a interface of service's definition
type ServiceDefiner interface {
	ToBytes() ([]byte, error)
}

vito.he's avatar
vito.he committed
// ServiceDefinition is the describer of service definition
type ServiceDefinition struct {
	CanonicalName string
	CodeSource    string
	Methods       []MethodDefinition
	Types         []TypeDefinition
}

vito.he's avatar
vito.he committed
func (def *ServiceDefinition) ToBytes() ([]byte, error) {
	return json.Marshal(def)
}

vito.he's avatar
vito.he committed
func (def *ServiceDefinition) String() string {
vito.he's avatar
vito.he committed
	var methodStr strings.Builder
	for _, m := range def.Methods {
vito.he's avatar
vito.he committed
		var paramType strings.Builder
		for _, p := range m.ParameterTypes {
vito.he's avatar
vito.he committed
			paramType.WriteString(fmt.Sprintf("{type:%v}", p))
vito.he's avatar
vito.he committed
		var param strings.Builder
		for _, d := range m.Parameters {
vito.he's avatar
vito.he committed
			param.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.Id, d.Type, d.TypeBuilderName))
vito.he's avatar
vito.he committed
		methodStr.WriteString(fmt.Sprintf("{name:%v,parameterTypes:[%v],returnType:%v,params:[%v] }", m.Name, paramType.String(), m.ReturnType, param.String()))
vito.he's avatar
vito.he committed
	var types strings.Builder
	for _, d := range def.Types {
vito.he's avatar
vito.he committed
		types.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.Id, d.Type, d.TypeBuilderName))
vito.he's avatar
vito.he committed
	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
}

vito.he's avatar
vito.he committed
// MethodDefinition is the describer of method definition
type MethodDefinition struct {
	Name           string
	ParameterTypes []string
	ReturnType     string
	Parameters     []TypeDefinition
}

vito.he's avatar
vito.he committed
// TypeDefinition is the describer of type definition
type TypeDefinition struct {
	Id              string
	Type            string
	Items           []TypeDefinition
	Enums           []string
	Properties      map[string]TypeDefinition
	TypeBuilderName string
}
vito.he's avatar
vito.he committed

vito.he's avatar
vito.he committed
// 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
		for _, t := range m.ArgsType() {
			paramTypes = append(paramTypes, t.Kind().String())
		}
		methodD := MethodDefinition{
			Name:           k,
			ParameterTypes: paramTypes,
			ReturnType:     m.ReplyType().Kind().String(),
		}
		sd.Methods = append(sd.Methods, methodD)
	}
vito.he's avatar
vito.he committed

	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)
vito.he's avatar
vito.he committed
		buf.WriteString(constant.PATH_SEPARATOR)
vito.he's avatar
vito.he committed
	}
	buf.WriteString(serviceName)
	if version != "" && version != "0.0.0" {
vito.he's avatar
vito.he committed
		buf.WriteString(constant.KEY_SEPARATOR)
vito.he's avatar
vito.he committed
		buf.WriteString(version)
	}
	return buf.String()
}