Browse Source

chore: refactor code (#1699)

Kevin Wan 3 năm trước cách đây
mục cha
commit
ec271db7a0

+ 2 - 2
core/stores/kv/store.go

@@ -24,6 +24,7 @@ type (
 		Expire(key string, seconds int) error
 		Expire(key string, seconds int) error
 		Expireat(key string, expireTime int64) error
 		Expireat(key string, expireTime int64) error
 		Get(key string) (string, error)
 		Get(key string) (string, error)
+		GetSet(key, value string) (string, error)
 		Hdel(key, field string) (bool, error)
 		Hdel(key, field string) (bool, error)
 		Hexists(key, field string) (bool, error)
 		Hexists(key, field string) (bool, error)
 		Hget(key, field string) (string, error)
 		Hget(key, field string) (string, error)
@@ -54,7 +55,6 @@ type (
 		Setex(key, value string, seconds int) error
 		Setex(key, value string, seconds int) error
 		Setnx(key, value string) (bool, error)
 		Setnx(key, value string) (bool, error)
 		SetnxEx(key, value string, seconds int) (bool, error)
 		SetnxEx(key, value string, seconds int) (bool, error)
-		Getset(key, value string) (string, error)
 		Sismember(key string, value interface{}) (bool, error)
 		Sismember(key string, value interface{}) (bool, error)
 		Smembers(key string) ([]string, error)
 		Smembers(key string) ([]string, error)
 		Spop(key string) (string, error)
 		Spop(key string) (string, error)
@@ -460,7 +460,7 @@ func (cs clusterStore) SetnxEx(key, value string, seconds int) (bool, error) {
 	return node.SetnxEx(key, value, seconds)
 	return node.SetnxEx(key, value, seconds)
 }
 }
 
 
-func (cs clusterStore) Getset(key, value string) (string, error) {
+func (cs clusterStore) GetSet(key, value string) (string, error) {
 	node, err := cs.getRedis(key)
 	node, err := cs.getRedis(key)
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err

+ 3 - 3
core/stores/kv/store_test.go

@@ -492,17 +492,17 @@ func TestRedis_SetExNx(t *testing.T) {
 
 
 func TestRedis_Getset(t *testing.T) {
 func TestRedis_Getset(t *testing.T) {
 	store := clusterStore{dispatcher: hash.NewConsistentHash()}
 	store := clusterStore{dispatcher: hash.NewConsistentHash()}
-	_, err := store.Getset("hello", "world")
+	_, err := store.GetSet("hello", "world")
 	assert.NotNil(t, err)
 	assert.NotNil(t, err)
 
 
 	runOnCluster(t, func(client Store) {
 	runOnCluster(t, func(client Store) {
-		val, err := client.Getset("hello", "world")
+		val, err := client.GetSet("hello", "world")
 		assert.Nil(t, err)
 		assert.Nil(t, err)
 		assert.Equal(t, "", val)
 		assert.Equal(t, "", val)
 		val, err = client.Get("hello")
 		val, err = client.Get("hello")
 		assert.Nil(t, err)
 		assert.Nil(t, err)
 		assert.Equal(t, "world", val)
 		assert.Equal(t, "world", val)
-		val, err = client.Getset("hello", "newworld")
+		val, err = client.GetSet("hello", "newworld")
 		assert.Nil(t, err)
 		assert.Nil(t, err)
 		assert.Equal(t, "world", val)
 		assert.Equal(t, "world", val)
 		val, err = client.Get("hello")
 		val, err = client.Get("hello")

+ 18 - 20
core/stores/redis/redis.go

@@ -615,51 +615,49 @@ func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error)
 	return
 	return
 }
 }
 
 
-// GetSet is the implementation of redis getset command.
-func (s *Redis) GetSet(key, value string) (string, error) {
-	return s.GetSetCtx(context.Background(), key, value)
+// GetBit is the implementation of redis getbit command.
+func (s *Redis) GetBit(key string, offset int64) (int, error) {
+	return s.GetBitCtx(context.Background(), key, offset)
 }
 }
 
 
-// GetSetCtx is the implementation of redis getset command.
-func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
+// GetBitCtx is the implementation of redis getbit command.
+func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val int, err error) {
 	err = s.brk.DoWithAcceptable(func() error {
 	err = s.brk.DoWithAcceptable(func() error {
 		conn, err := getRedis(s)
 		conn, err := getRedis(s)
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
 
 
-		if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
-			return nil
-		} else if err != nil {
+		v, err := conn.GetBit(ctx, key, offset).Result()
+		if err != nil {
 			return err
 			return err
-		} else {
-			return nil
 		}
 		}
+
+		val = int(v)
+		return nil
 	}, acceptable)
 	}, acceptable)
 
 
 	return
 	return
 }
 }
 
 
-// GetBit is the implementation of redis getbit command.
-func (s *Redis) GetBit(key string, offset int64) (int, error) {
-	return s.GetBitCtx(context.Background(), key, offset)
+// GetSet is the implementation of redis getset command.
+func (s *Redis) GetSet(key, value string) (string, error) {
+	return s.GetSetCtx(context.Background(), key, value)
 }
 }
 
 
-// GetBitCtx is the implementation of redis getbit command.
-func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val int, err error) {
+// GetSetCtx is the implementation of redis getset command.
+func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
 	err = s.brk.DoWithAcceptable(func() error {
 	err = s.brk.DoWithAcceptable(func() error {
 		conn, err := getRedis(s)
 		conn, err := getRedis(s)
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
 
 
-		v, err := conn.GetBit(ctx, key, offset).Result()
-		if err != nil {
-			return err
+		if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
+			return nil
 		}
 		}
 
 
-		val = int(v)
-		return nil
+		return err
 	}, acceptable)
 	}, acceptable)
 
 
 	return
 	return

+ 2 - 2
core/stores/redis/redis_test.go

@@ -703,9 +703,9 @@ func TestRedis_Set(t *testing.T) {
 
 
 func TestRedis_GetSet(t *testing.T) {
 func TestRedis_GetSet(t *testing.T) {
 	runOnRedis(t, func(client *Redis) {
 	runOnRedis(t, func(client *Redis) {
-		val, err := New(client.Addr, badType()).GetSet("hello", "world")
+		_, err := New(client.Addr, badType()).GetSet("hello", "world")
 		assert.NotNil(t, err)
 		assert.NotNil(t, err)
-		val, err = client.GetSet("hello", "world")
+		val, err := client.GetSet("hello", "world")
 		assert.Nil(t, err)
 		assert.Nil(t, err)
 		assert.Equal(t, "", val)
 		assert.Equal(t, "", val)
 		val, err = client.Get("hello")
 		val, err = client.Get("hello")

+ 3 - 0
rest/httpc/responses.go

@@ -8,6 +8,7 @@ import (
 	"github.com/zeromicro/go-zero/rest/internal/encoding"
 	"github.com/zeromicro/go-zero/rest/internal/encoding"
 )
 )
 
 
+// Parse parses the response.
 func Parse(resp *http.Response, val interface{}) error {
 func Parse(resp *http.Response, val interface{}) error {
 	if err := ParseHeaders(resp, val); err != nil {
 	if err := ParseHeaders(resp, val); err != nil {
 		return err
 		return err
@@ -16,10 +17,12 @@ func Parse(resp *http.Response, val interface{}) error {
 	return ParseJsonBody(resp, val)
 	return ParseJsonBody(resp, val)
 }
 }
 
 
+// ParseHeaders parses the rsponse headers.
 func ParseHeaders(resp *http.Response, val interface{}) error {
 func ParseHeaders(resp *http.Response, val interface{}) error {
 	return encoding.ParseHeaders(resp.Header, val)
 	return encoding.ParseHeaders(resp.Header, val)
 }
 }
 
 
+// ParseJsonBody parses the rsponse body, which should be in json content type.
 func ParseJsonBody(resp *http.Response, val interface{}) error {
 func ParseJsonBody(resp *http.Response, val interface{}) error {
 	if withJsonBody(resp) {
 	if withJsonBody(resp) {
 		return mapping.UnmarshalJsonReader(resp.Body, val)
 		return mapping.UnmarshalJsonReader(resp.Body, val)