dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/identifier/base_metadata_identifier.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package identifier
    19  
    20  import (
    21  	"net/url"
    22  )
    23  
    24  import (
    25  	"github.com/dubbogo/gost/log/logger"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    30  )
    31  
    32  // IMetadataIdentifier is the interface which describes the Metadata base identify
    33  type IMetadataIdentifier interface {
    34  	GetFilePathKey() string
    35  	GetIdentifierKey() string
    36  }
    37  
    38  // BaseMetadataIdentifier is the base implement of BaseMetadataIdentifier interface
    39  type BaseMetadataIdentifier struct {
    40  	ServiceInterface string
    41  	Version          string
    42  	Group            string
    43  	Side             string
    44  }
    45  
    46  // joinParams will join the specified char in slice, and build as string
    47  func joinParams(joinChar string, params []string) string {
    48  	var joinedStr string
    49  	for _, param := range params {
    50  		if param == "" {
    51  			logger.Info("[Metadata report] Break loop in `joinParams` to avoid invalid path when meeting empty param")
    52  			break
    53  		}
    54  		joinedStr += joinChar
    55  		joinedStr += param
    56  	}
    57  	return joinedStr
    58  }
    59  
    60  // getIdentifierKey returns string that format is service:Version:Group:Side:param1:param2...
    61  func (mdi *BaseMetadataIdentifier) getIdentifierKey(params ...string) string {
    62  	return mdi.ServiceInterface +
    63  		constant.KeySeparator + mdi.Version +
    64  		constant.KeySeparator + mdi.Group +
    65  		constant.KeySeparator + mdi.Side +
    66  		joinParams(constant.KeySeparator, params)
    67  }
    68  
    69  // getFilePathKey returns string that format is metadata/path/Version/Group/Side/param1/param2...
    70  func (mdi *BaseMetadataIdentifier) getFilePathKey(params ...string) string {
    71  	path := serviceToPath(mdi.ServiceInterface)
    72  
    73  	return constant.DefaultPathTag +
    74  		withPathSeparator(path) +
    75  		withPathSeparator(mdi.Version) +
    76  		withPathSeparator(mdi.Group) +
    77  		withPathSeparator(mdi.Side) +
    78  		joinParams(constant.PathSeparator, params)
    79  }
    80  
    81  // serviceToPath uss URL encode to decode the @serviceInterface
    82  func serviceToPath(serviceInterface string) string {
    83  	if serviceInterface == constant.AnyValue {
    84  		return ""
    85  	}
    86  	return url.PathEscape(serviceInterface)
    87  }
    88  
    89  // withPathSeparator return "/" + @path
    90  func withPathSeparator(path string) string {
    91  	if len(path) != 0 {
    92  		path = constant.PathSeparator + path
    93  	}
    94  	return path
    95  }
    96  
    97  // BaseApplicationMetadataIdentifier is the base implement of BaseApplicationMetadataIdentifier interface
    98  type BaseApplicationMetadataIdentifier struct {
    99  	Application string
   100  	Group       string
   101  }
   102  
   103  // getIdentifierKey returns string that format is application/param
   104  func (madi *BaseApplicationMetadataIdentifier) getIdentifierKey(params ...string) string {
   105  	return madi.Application + joinParams(constant.KeySeparator, params)
   106  }
   107  
   108  // getFilePathKey returns string that format is metadata/application/revision
   109  func (madi *BaseApplicationMetadataIdentifier) getFilePathKey(params ...string) string {
   110  	return constant.DefaultPathTag +
   111  		withPathSeparator(madi.Application) +
   112  		joinParams(constant.PathSeparator, params)
   113  }