Repository: go-kratos/kratos
Pull request #2102 (2022-06-14 11:28:45)

fix:duplicate  generation const for http
fix:duplicate  generation const for http


Patch:
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 3fea4feb..a2161bf7 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -36,7 +36,7 @@ jobs:
           - "8848:8848"
           - "9848:9848"
       polaris:
-        image: houseme/polaris-server-with-config:latest
+        image: polarismesh/polaris-server-standalone:latest
         ports:
           - 8090:8090
           - 8091:8091
diff --git a/cmd/kratos/internal/proto/add/add.go b/cmd/kratos/internal/proto/add/add.go
index 5173e6d1..626b5db2 100644
--- a/cmd/kratos/internal/proto/add/add.go
+++ b/cmd/kratos/internal/proto/add/add.go
@@ -7,6 +7,8 @@ import (
 
 	"github.com/spf13/cobra"
 	"golang.org/x/mod/modfile"
+	"golang.org/x/text/cases"
+	"golang.org/x/text/language"
 )
 
 // CmdAdd represents the add command.
@@ -63,7 +65,11 @@ func javaPackage(name string) string {
 }
 
 func serviceName(name string) string {
-	return export(strings.Split(name, ".")[0])
+	return toUpperCamelCase(strings.Split(name, ".")[0])
 }
 
-func export(s string) string { return strings.ToUpper(s[:1]) + s[1:] }
+func toUpperCamelCase(s string) string {
+	s = strings.ReplaceAll(s, "_", " ")
+	s = cases.Title(language.Und, cases.NoLower).String(s)
+	return strings.ReplaceAll(s, " ", "")
+}
diff --git a/cmd/protoc-gen-go-http/template.go b/cmd/protoc-gen-go-http/template.go
index 3b70678a..79421085 100644
--- a/cmd/protoc-gen-go-http/template.go
+++ b/cmd/protoc-gen-go-http/template.go
@@ -10,7 +10,7 @@ var httpTemplate = `
 {{$svrType := .ServiceType}}
 {{$svrName := .ServiceName}}
 
-{{- range .Methods}}
+{{- range .MethodSets}}
 const Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}"
 {{- end}}
 
diff --git a/config/config.go b/config/config.go
index 126a6379..722bcb09 100644
--- a/config/config.go
+++ b/config/config.go
@@ -20,6 +20,8 @@ var (
 	ErrNotFound = errors.New("key not found")
 	// ErrTypeAssert is type assert error.
 	ErrTypeAssert = errors.New("type assert error")
+	// ErrWatcherStopped means watcher is stopped.
+	ErrWatcherStopped = errors.New("warcher stopped")
 
 	_ Config = (*config)(nil)
 )
diff --git a/contrib/config/apollo/watcher.go b/contrib/config/apollo/watcher.go
index 04e02fbc..a085c3e3 100644
--- a/contrib/config/apollo/watcher.go
+++ b/contrib/config/apollo/watcher.go
@@ -1,8 +1,6 @@
 package apollo
 
 import (
-	"context"
-
 	"github.com/go-kratos/kratos/v2/config"
 	"github.com/go-kratos/kratos/v2/log"
 
@@ -62,7 +60,7 @@ func newWatcher(a *apollo) (config.Watcher, error) {
 func (w *watcher) Next() ([]*config.KeyValue, error) {
 	kv, ok := <-w.out
 	if !ok {
-		return nil, context.Canceled
+		return nil, config.ErrWatcherStopped
 	}
 	return kv, nil
 }
diff --git a/contrib/config/consul/watcher.go b/contrib/config/consul/watcher.go
index 1223ffc9..4a34119d 100644
--- a/contrib/config/consul/watcher.go
+++ b/contrib/config/consul/watcher.go
@@ -54,13 +54,10 @@ func newWatcher(s *source) (*watcher, error) {
 
 func (w *watcher) Next() ([]*config.KeyValue, error) {
 	select {
-	case _, ok := <-w.ch:
-		if !ok {
-			return nil, nil
-		}
+	case <-w.ch:
 		return w.source.Load()
 	case <-w.closeChan:
-		return nil, nil
+		return nil, config.ErrWatcherStopped
 	}
 }
 
diff --git a/contrib/config/etcd/watcher.go b/contrib/config/etcd/watcher.go
index 6a2d8cce..40968e45 100644
--- a/contrib/config/etcd/watcher.go
+++ b/contrib/config/etcd/watcher.go
@@ -28,13 +28,13 @@ func newWatcher(s *source) *watcher {
 
 func (s *watcher) Next() ([]*config.KeyValue, error) {
 	select {
-	case _, ok := <-s.ch:
-		if !ok {
-			return nil, nil
+	case resp := <-s.ch:
+		if resp.Err() != nil {
+			return nil, resp.Err()
 		}
 		return s.source.Load()
 	case <-s.closeChan:
-		return nil, nil
+		return nil, config.ErrWatcherStopped
 	}
 }
 
diff --git a/contrib/config/nacos/config.go b/contrib/config/nacos/config.go
index ee07e411..2c9a7a52 100644
--- a/contrib/config/nacos/config.go
+++ b/contrib/config/nacos/config.go
@@ -106,7 +106,7 @@ func (c *Config) Watch() (config.Watcher, error) {
 	err := c.client.ListenConfig(vo.ConfigParam{
 		DataId: c.opts.dataID,
 		Group:  c.opts.group,
-		OnChange: func(namespace, group, dataId, data string) {
+		OnChange: func(_, group, dataId, data string) {
 			if dataId == watcher.dataID && group == watcher.group {
 				watcher.content <- data
 			}
@@ -144,8 +144,8 @@ func newWatcher(ctx context.Context, dataID string, group string, cancelListenCo
 
 func (w *Watcher) Next() ([]*config.KeyValue, error) {
 	select {
-	case <-w.Context.Done():
-		return nil, nil
+	case <-w.Done():
+		return nil, config.ErrWatcherStopped
 	case content := <-w.content:
 		k := w.dataID
 		return []*config.KeyValue{


Test patch:
diff --git a/cmd/kratos/internal/proto/add/add_test.go b/cmd/kratos/internal/proto/add/add_test.go
new file mode 100644
index 00000000..eda6b414
--- /dev/null
+++ b/cmd/kratos/internal/proto/add/add_test.go
@@ -0,0 +1,38 @@
+package add
+
+import "testing"
+
+func TestUnderscoreToUpperCamelCase(t *testing.T) {
+	tests := []struct {
+		name string
+		want string
+	}{
+		{
+			name: "hello_world",
+			want: "HelloWorld",
+		},
+		{
+			name: "v2_kratos_dev",
+			want: "V2KratosDev",
+		},
+		{
+			name: "www_Google_com",
+			want: "WwwGoogleCom",
+		},
+		{
+			name: "wwwBaidu_com",
+			want: "WwwBaiduCom",
+		},
+		{
+			name: "HelloWorld",
+			want: "HelloWorld",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := toUpperCamelCase(tt.name); got != tt.want {
+				t.Errorf("toUpperCamelCase() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}