You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2022/09/07 10:32:13 UTC

[servicecomb-service-center] branch master updated: [fix]gov list return wrong kind (#1339)

This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new a8e52e2d [fix]gov list return wrong kind (#1339)
a8e52e2d is described below

commit a8e52e2da127c0603428921499f73a074fe23a57
Author: little-cui <su...@qq.com>
AuthorDate: Wed Sep 7 18:32:08 2022 +0800

    [fix]gov list return wrong kind (#1339)
---
 pkg/util/util.go                          | 23 +++++++++++++++++++++++
 pkg/util/util_test.go                     | 23 +++++++++++++++++++++++
 server/resource/gov/gov_resource_test.go  |  2 +-
 server/service/grc/config_distributor.go  |  3 ++-
 server/service/grc/kie/kie_distributor.go | 26 ++------------------------
 5 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/pkg/util/util.go b/pkg/util/util.go
index 51f8710a..08b18d61 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -18,6 +18,7 @@
 package util
 
 import (
+	"bytes"
 	"runtime"
 	"strings"
 	"unsafe"
@@ -146,3 +147,25 @@ func ToDomainProject(domain, project string) (domainProject string) {
 func IsVersionOrHealthPattern(pattern string) bool {
 	return strings.HasSuffix(pattern, "/version") || strings.HasSuffix(pattern, "/health")
 }
+
+func ToSnake(name string) string {
+	if name == "" {
+		return ""
+	}
+	temp := strings.Split(name, "-")
+	var buffer bytes.Buffer
+	for num, v := range temp {
+		vv := []rune(v)
+		if num == 0 {
+			buffer.WriteString(string(vv))
+			continue
+		}
+		if len(vv) > 0 {
+			if vv[0] >= 'a' && vv[0] <= 'z' { //首字母大写
+				vv[0] -= 32
+			}
+			buffer.WriteString(string(vv))
+		}
+	}
+	return buffer.String()
+}
diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go
index e464a30c..f2f08b69 100644
--- a/pkg/util/util_test.go
+++ b/pkg/util/util_test.go
@@ -155,3 +155,26 @@ func TestIsVersionOrHealthPattern(t *testing.T) {
 	assert.True(t, IsVersionOrHealthPattern("/v4/a/registry/health"))
 	assert.False(t, IsVersionOrHealthPattern("/health/a"))
 }
+
+func TestToSnake(t *testing.T) {
+	type args struct {
+		name string
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{"single word", args{"a"}, "a"},
+		{"2 words", args{"a-b"}, "aB"},
+		{"3 words", args{"a-b-cc"}, "aBCc"},
+		{"invalid", args{"a.b"}, "a.b"},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := ToSnake(tt.args.name); got != tt.want {
+				t.Errorf("ToSnake() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/server/resource/gov/gov_resource_test.go b/server/resource/gov/gov_resource_test.go
index bf9a838d..08a7448d 100644
--- a/server/resource/gov/gov_resource_test.go
+++ b/server/resource/gov/gov_resource_test.go
@@ -25,7 +25,6 @@ import (
 	"testing"
 
 	_ "github.com/apache/servicecomb-service-center/server/service/grc/mock"
-	"k8s.io/kube-openapi/pkg/validation/spec"
 
 	_ "github.com/apache/servicecomb-service-center/test"
 
@@ -37,6 +36,7 @@ import (
 	grcsvc "github.com/apache/servicecomb-service-center/server/service/grc"
 	"github.com/go-chassis/go-archaius"
 	"github.com/stretchr/testify/assert"
+	"k8s.io/kube-openapi/pkg/validation/spec"
 )
 
 func init() {
diff --git a/server/service/grc/config_distributor.go b/server/service/grc/config_distributor.go
index 79fbb0e9..eae38c4e 100644
--- a/server/service/grc/config_distributor.go
+++ b/server/service/grc/config_distributor.go
@@ -23,6 +23,7 @@ import (
 
 	model "github.com/apache/servicecomb-service-center/pkg/gov"
 	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
 	"github.com/apache/servicecomb-service-center/server/config"
 )
 
@@ -90,7 +91,7 @@ func Init() error {
 		var names []string
 		for kind, policy := range config.GetGov().Policies {
 			RegisterPolicySchema(kind, policy.ValidationSpec)
-			names = append(names, kind)
+			names = append(names, util.ToSnake(kind))
 		}
 		PolicyNames = names
 	}
diff --git a/server/service/grc/kie/kie_distributor.go b/server/service/grc/kie/kie_distributor.go
index 30ac7ba4..7a4bd508 100644
--- a/server/service/grc/kie/kie_distributor.go
+++ b/server/service/grc/kie/kie_distributor.go
@@ -18,7 +18,6 @@
 package kie
 
 import (
-	"bytes"
 	"context"
 	"encoding/json"
 	"fmt"
@@ -26,6 +25,7 @@ import (
 	"strings"
 	"time"
 
+	"github.com/apache/servicecomb-service-center/pkg/util"
 	"github.com/ghodss/yaml"
 	"github.com/go-chassis/foundation/httpclient"
 	"github.com/go-chassis/kie-client"
@@ -259,28 +259,6 @@ func new(opts config.DistributorOptions) (grcsvc.ConfigDistributor, error) {
 	return &Distributor{name: opts.Name, client: initClient(opts.Endpoint)}, nil
 }
 
-func toSnake(name string) string {
-	if name == "" {
-		return ""
-	}
-	temp := strings.Split(name, "-")
-	var buffer bytes.Buffer
-	for num, v := range temp {
-		vv := []rune(v)
-		if num == 0 {
-			buffer.WriteString(string(vv))
-			continue
-		}
-		if len(vv) > 0 {
-			if vv[0] >= 'a' && vv[0] <= 'z' { //首字母大写
-				vv[0] -= 32
-			}
-			buffer.WriteString(string(vv))
-		}
-	}
-	return buffer.String()
-}
-
 func (d *Distributor) listDataByKind(ctx context.Context, kind, project, app, env string) (*kie.KVResponse, int, error) {
 	ops := []kie.GetOption{
 		kie.WithKey("beginWith(" + toGovKeyPrefix(kind) + ")"),
@@ -369,7 +347,7 @@ func (d *Distributor) transform(kv *kie.KVDoc, kind string) (*gov.Policy, error)
 }
 
 func toGovKeyPrefix(kind string) string {
-	return grcsvc.KeyPrefix + toSnake(kind) + "."
+	return grcsvc.KeyPrefix + util.ToSnake(kind) + "."
 }
 
 func init() {