Skip to content

Commit 7100dc6

Browse files
committed
fix: 新增服务契约摘要 & 修复服务契约多个更新相关的问题
1 parent 801c790 commit 7100dc6

File tree

10 files changed

+567
-218
lines changed

10 files changed

+567
-218
lines changed

common/api/v1/naming_response.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ func NewRoutingResponse(code apimodel.Code, routing *apitraffic.Routing) *apiser
188188
}
189189
}
190190

191+
// NewServiceContractResponse create the response with data with any type
192+
func NewServiceContractResponse(code apimodel.Code, contract *apiservice.ServiceContract) *apiservice.Response {
193+
return &apiservice.Response{
194+
Code: &wrappers.UInt32Value{Value: uint32(code)},
195+
Info: &wrappers.StringValue{Value: code2info[uint32(code)]},
196+
ServiceContract: contract,
197+
}
198+
}
199+
191200
// NewAnyDataResponse create the response with data with any type
192201
func NewAnyDataResponse(code apimodel.Code, msg proto.Message) *apiservice.Response {
193202
ret, err := anypb.New(proto.MessageV2(msg))

common/model/contract.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ type ServiceContract struct {
4242
Revision string
4343
// 额外描述
4444
Content string
45+
// 内容摘要
46+
ContentDigest string
47+
// 服务接口标签信息
48+
Metadata map[string]string
49+
// 服务接口标签序列化的字符串
50+
MetadataStr string
4551
// 创建时间
4652
CreateTime time.Time
4753
// 更新时间
@@ -59,6 +65,14 @@ type EnrichServiceContract struct {
5965
ManualInterfaces map[string]*InterfaceDescriptor
6066
}
6167

68+
// IsManual 是否手动添加的契约
69+
func (e *EnrichServiceContract) IsManual() bool {
70+
if len(e.ClientInterfaces) == 0 && len(e.ManualInterfaces) == 0 {
71+
return false
72+
}
73+
return len(e.ManualInterfaces) > 0
74+
}
75+
6276
func (e *EnrichServiceContract) Format() {
6377
if e.isFormated {
6478
return
@@ -89,9 +103,6 @@ func (e *EnrichServiceContract) Format() {
89103
}
90104
e.Interfaces = append(e.Interfaces, e.ClientInterfaces[k])
91105
}
92-
// 格式化完毕之后,清空暂存的 ClientInterface 以及 ManualInterface 数据
93-
e.ClientInterfaces = nil
94-
e.ManualInterfaces = nil
95106
}
96107

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

149+
// GetServiceContractCacheKey 生成契约唯一KEY
150+
func GetServiceContractCacheKey(contract *apiservice.ServiceContract) string {
151+
return fmt.Sprintf("%s/%s/%s/%s/%s", contract.Namespace, contract.Service, contract.Type,
152+
contract.Protocol, contract.Version)
153+
}
154+
138155
type InterfaceDescriptor struct {
139156
// ID
140157
ID string
@@ -156,6 +173,8 @@ type InterfaceDescriptor struct {
156173
Path string
157174
// 接口描述信息
158175
Content string
176+
// 接口内容摘要
177+
ContentDigest string
159178
// 接口信息摘要
160179
Revision string
161180
// 创建来源
@@ -167,3 +186,23 @@ type InterfaceDescriptor struct {
167186
// Valid
168187
Valid bool
169188
}
189+
190+
func (i *InterfaceDescriptor) ToSpec() *apiservice.InterfaceDescriptor {
191+
return &apiservice.InterfaceDescriptor{
192+
Id: i.ID,
193+
Method: i.Method,
194+
Path: i.Path,
195+
Content: i.Content,
196+
ContentDigest: i.ContentDigest,
197+
Source: i.Source,
198+
Revision: i.Revision,
199+
Ctime: commontime.Time2String(i.CreateTime),
200+
Mtime: commontime.Time2String(i.CreateTime),
201+
Name: i.Type,
202+
Namespace: i.Namespace,
203+
Service: i.Service,
204+
Protocol: i.Protocol,
205+
Version: i.Version,
206+
Type: i.Type,
207+
}
208+
}

common/utils/common.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"context"
2222
"crypto/sha1"
2323
"encoding/hex"
24+
"encoding/json"
2425
"errors"
2526
"fmt"
2627
"io"
28+
"reflect"
2729
"regexp"
2830
"strconv"
2931
"strings"
@@ -579,6 +581,19 @@ func CheckContractTetrad(req *apiservice.ServiceContract) (string, *apiservice.R
579581
return hex.EncodeToString(h.Sum(nil)), nil
580582
}
581583

584+
// BuildSha1Digest 构建SHA1摘要
585+
func BuildSha1Digest(value string) (string, error) {
586+
if len(value) == 0 {
587+
return "", nil
588+
}
589+
h := sha1.New()
590+
if _, err := io.WriteString(h, value); err != nil {
591+
return "", err
592+
}
593+
out := hex.EncodeToString(h.Sum(nil))
594+
return out, nil
595+
}
596+
582597
func CheckContractInterfaceTetrad(contractId string, source apiservice.InterfaceDescriptor_Source,
583598
req *apiservice.InterfaceDescriptor) (string, *apiservice.Response) {
584599
if contractId == "" {
@@ -611,3 +626,39 @@ func CalculateContractID(namespace, service, name, protocol, version string) (st
611626
out := hex.EncodeToString(h.Sum(nil))
612627
return out, nil
613628
}
629+
630+
// ConvertMetadataToStringValue 将Metadata转换为可序列化字符串
631+
func ConvertMetadataToStringValue(metadata map[string]string) (string, error) {
632+
if metadata == nil {
633+
return "", nil
634+
}
635+
v, err := json.Marshal(metadata)
636+
if err != nil {
637+
return "", err
638+
}
639+
return string(v), nil
640+
}
641+
642+
// ConvertStringValueToMetadata 将字符串反序列为metadata
643+
func ConvertStringValueToMetadata(str string) (map[string]string, error) {
644+
if str == "" {
645+
return nil, nil
646+
}
647+
v := make(map[string]string)
648+
err := json.Unmarshal([]byte(str), &v)
649+
if err != nil {
650+
return nil, err
651+
}
652+
return v, nil
653+
}
654+
655+
// NeedUpdateMetadata 判断是否出现了metadata的变更
656+
func NeedUpdateMetadata(metadata map[string]string, inMetadata map[string]string) bool {
657+
if inMetadata == nil {
658+
return false
659+
}
660+
if len(metadata) != len(inMetadata) {
661+
return true
662+
}
663+
return !reflect.DeepEqual(metadata, inMetadata)
664+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ require (
7878

7979
require (
8080
github.com/DATA-DOG/go-sqlmock v1.5.0
81-
github.com/polarismesh/specification v1.5.3-alpha.2
81+
github.com/polarismesh/specification v1.5.5-alpha.1
8282
)
8383

8484
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
300300
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
301301
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219 h1:XnFyNUWnciM6zgXaz6tm+Egs35rhoD0KGMmKh4gCdi0=
302302
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk=
303-
github.com/polarismesh/specification v1.5.3-alpha.2 h1:QSgpGmx5VfPcDPAq7qnTOkMVFNpmBMgLSDhtyMlS6/g=
304-
github.com/polarismesh/specification v1.5.3-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
303+
github.com/polarismesh/specification v1.5.5-alpha.1 h1:lGLaj+I6iD25F0FuQnR83sR+1SJ8KqykS0vCnGx2ZAQ=
304+
github.com/polarismesh/specification v1.5.5-alpha.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
305305
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
306306
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
307307
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=

0 commit comments

Comments
 (0)