Skip to content

Commit

Permalink
fix: 新增服务契约摘要 & 修复服务契约多个更新相关的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewshan committed Dec 7, 2024
1 parent 801c790 commit 7100dc6
Show file tree
Hide file tree
Showing 10 changed files with 567 additions and 218 deletions.
9 changes: 9 additions & 0 deletions common/api/v1/naming_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ func NewRoutingResponse(code apimodel.Code, routing *apitraffic.Routing) *apiser
}
}

// NewServiceContractResponse create the response with data with any type
func NewServiceContractResponse(code apimodel.Code, contract *apiservice.ServiceContract) *apiservice.Response {
return &apiservice.Response{
Code: &wrappers.UInt32Value{Value: uint32(code)},
Info: &wrappers.StringValue{Value: code2info[uint32(code)]},
ServiceContract: contract,
}
}

// NewAnyDataResponse create the response with data with any type
func NewAnyDataResponse(code apimodel.Code, msg proto.Message) *apiservice.Response {
ret, err := anypb.New(proto.MessageV2(msg))
Expand Down
45 changes: 42 additions & 3 deletions common/model/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type ServiceContract struct {
Revision string
// 额外描述
Content string
// 内容摘要
ContentDigest string
// 服务接口标签信息
Metadata map[string]string
// 服务接口标签序列化的字符串
MetadataStr string
// 创建时间
CreateTime time.Time
// 更新时间
Expand All @@ -59,6 +65,14 @@ type EnrichServiceContract struct {
ManualInterfaces map[string]*InterfaceDescriptor
}

// IsManual 是否手动添加的契约
func (e *EnrichServiceContract) IsManual() bool {
if len(e.ClientInterfaces) == 0 && len(e.ManualInterfaces) == 0 {
return false
}
return len(e.ManualInterfaces) > 0
}

func (e *EnrichServiceContract) Format() {
if e.isFormated {
return
Expand Down Expand Up @@ -89,9 +103,6 @@ func (e *EnrichServiceContract) Format() {
}
e.Interfaces = append(e.Interfaces, e.ClientInterfaces[k])
}
// 格式化完毕之后,清空暂存的 ClientInterface 以及 ManualInterface 数据
e.ClientInterfaces = nil
e.ManualInterfaces = nil
}

func (e *EnrichServiceContract) ToSpec() *apiservice.ServiceContract {
Expand Down Expand Up @@ -135,6 +146,12 @@ func (s *ServiceContract) GetCacheKey() string {
return fmt.Sprintf("%s/%s/%s/%s/%s", s.Namespace, s.Service, s.Type, s.Protocol, s.Version)
}

// GetServiceContractCacheKey 生成契约唯一KEY
func GetServiceContractCacheKey(contract *apiservice.ServiceContract) string {
return fmt.Sprintf("%s/%s/%s/%s/%s", contract.Namespace, contract.Service, contract.Type,
contract.Protocol, contract.Version)
}

type InterfaceDescriptor struct {
// ID
ID string
Expand All @@ -156,6 +173,8 @@ type InterfaceDescriptor struct {
Path string
// 接口描述信息
Content string
// 接口内容摘要
ContentDigest string
// 接口信息摘要
Revision string
// 创建来源
Expand All @@ -167,3 +186,23 @@ type InterfaceDescriptor struct {
// Valid
Valid bool
}

func (i *InterfaceDescriptor) ToSpec() *apiservice.InterfaceDescriptor {
return &apiservice.InterfaceDescriptor{
Id: i.ID,
Method: i.Method,
Path: i.Path,
Content: i.Content,
ContentDigest: i.ContentDigest,
Source: i.Source,
Revision: i.Revision,
Ctime: commontime.Time2String(i.CreateTime),
Mtime: commontime.Time2String(i.CreateTime),
Name: i.Type,
Namespace: i.Namespace,
Service: i.Service,
Protocol: i.Protocol,
Version: i.Version,
Type: i.Type,
}
}
51 changes: 51 additions & 0 deletions common/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -579,6 +581,19 @@ func CheckContractTetrad(req *apiservice.ServiceContract) (string, *apiservice.R
return hex.EncodeToString(h.Sum(nil)), nil
}

// BuildSha1Digest 构建SHA1摘要
func BuildSha1Digest(value string) (string, error) {
if len(value) == 0 {
return "", nil
}
h := sha1.New()
if _, err := io.WriteString(h, value); err != nil {
return "", err
}
out := hex.EncodeToString(h.Sum(nil))
return out, nil
}

func CheckContractInterfaceTetrad(contractId string, source apiservice.InterfaceDescriptor_Source,
req *apiservice.InterfaceDescriptor) (string, *apiservice.Response) {
if contractId == "" {
Expand Down Expand Up @@ -611,3 +626,39 @@ func CalculateContractID(namespace, service, name, protocol, version string) (st
out := hex.EncodeToString(h.Sum(nil))
return out, nil
}

// ConvertMetadataToStringValue 将Metadata转换为可序列化字符串
func ConvertMetadataToStringValue(metadata map[string]string) (string, error) {
if metadata == nil {
return "", nil
}
v, err := json.Marshal(metadata)
if err != nil {
return "", err
}
return string(v), nil
}

// ConvertStringValueToMetadata 将字符串反序列为metadata
func ConvertStringValueToMetadata(str string) (map[string]string, error) {
if str == "" {
return nil, nil
}
v := make(map[string]string)
err := json.Unmarshal([]byte(str), &v)
if err != nil {
return nil, err
}
return v, nil
}

// NeedUpdateMetadata 判断是否出现了metadata的变更
func NeedUpdateMetadata(metadata map[string]string, inMetadata map[string]string) bool {
if inMetadata == nil {
return false
}
if len(metadata) != len(inMetadata) {
return true
}
return !reflect.DeepEqual(metadata, inMetadata)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require (

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/polarismesh/specification v1.5.3-alpha.2
github.com/polarismesh/specification v1.5.5-alpha.1
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219 h1:XnFyNUWnciM6zgXaz6tm+Egs35rhoD0KGMmKh4gCdi0=
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk=
github.com/polarismesh/specification v1.5.3-alpha.2 h1:QSgpGmx5VfPcDPAq7qnTOkMVFNpmBMgLSDhtyMlS6/g=
github.com/polarismesh/specification v1.5.3-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
github.com/polarismesh/specification v1.5.5-alpha.1 h1:lGLaj+I6iD25F0FuQnR83sR+1SJ8KqykS0vCnGx2ZAQ=
github.com/polarismesh/specification v1.5.5-alpha.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
Expand Down
Loading

0 comments on commit 7100dc6

Please sign in to comment.