Sfoglia il codice sorgente

Add Int64 function, imporve doc.

Gary Burd 12 anni fa
parent
commit
db71111b72
4 ha cambiato i file con 33 aggiunte e 6 eliminazioni
  1. 3 1
      README.markdown
  2. 2 4
      redis/doc.go
  3. 27 0
      redis/reply.go
  4. 1 1
      redisx/struct_test.go

+ 3 - 1
README.markdown

@@ -16,7 +16,9 @@ Features
 Documentation
 -------------
 
-The Redigo API reference is available on [GoPkgDoc](http://godoc.org/github.com/garyburd/redigo/redis).
+The Redigo API reference is available on
+[godoc.org](http://godoc.org/github.com/garyburd/redigo/redis).  (The godoc.org
+server uses Redigo and Redis for storage).
 
 Installation
 ------------

+ 2 - 4
redis/doc.go

@@ -12,10 +12,8 @@
 // License for the specific language governing permissions and limitations
 // under the License.
 
-// Package redis is a client for the Redis database.
-//
-// Package redis only supports the binary-safe Redis protocol, so you can use
-// it with any Redis version >= 1.2.0.
+// Package redis is a client for the Redis database. Package redis supports
+// Redis version 1.2.0 and above. All commands are supported.
 //
 // Connections
 //

+ 27 - 0
redis/reply.go

@@ -53,6 +53,33 @@ func Int(reply interface{}, err error) (int, error) {
 	return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", reply)
 }
 
+// Int64 is a helper that converts a command reply to 64 bit integer. If err is
+// not equal to nil, then Int returns 0, err. Otherwise, Int converts the reply
+// to an int as follows:
+//
+//  Reply type    Result
+//  integer       reply, nil
+//  bulk          strconv.ParseInt(reply, 10, 64)
+//  nil           0, ErrNil
+//  other         0, error
+func Int64(reply interface{}, err error) (int64, error) {
+	if err != nil {
+		return 0, err
+	}
+	switch reply := reply.(type) {
+	case int64:
+		return reply, nil
+	case []byte:
+		n, err := strconv.ParseInt(string(reply), 10, 64)
+		return n, err
+	case nil:
+		return 0, ErrNil
+	case Error:
+		return 0, reply
+	}
+	return 0, fmt.Errorf("redigo: unexpected type for Int64, got type %T", reply)
+}
+
 // String is a helper that converts a command reply to a string. If err is not
 // equal to nil, then String returns "", err. Otherwise String converts the
 // reply to a string as follows:

+ 1 - 1
redisx/struct_test.go

@@ -52,7 +52,7 @@ func TestScanStruct(t *testing.T) {
 		value := reflect.New(reflect.ValueOf(tt.value).Type().Elem())
 
 		if err := redisx.ScanStruct(reply, value.Interface()); err != nil {
-			t.Fatalf("ScanStruct(%s) returned error %v", tt.title)
+			t.Fatalf("ScanStruct(%s) returned error %v", tt.title, err)
 		}
 
 		if !reflect.DeepEqual(value.Interface(), tt.value) {