Skip to content

Commit

Permalink
Moved to couchbaselabs instead of couchbase. Moved transcoding to own…
Browse files Browse the repository at this point in the history
… file.
  • Loading branch information
brett19 committed Jan 14, 2015
1 parent 6781663 commit 845047d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 79 deletions.
80 changes: 2 additions & 78 deletions bucket.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gocouchbase

import "encoding/json"
import "time"
import "fmt"
import "github.com/couchbase/gocouchbaseio"
import "time"
import "github.com/couchbaselabs/gocouchbaseio"
import "net/http"
import "net/url"
import "math/rand"
Expand All @@ -24,82 +24,6 @@ func (b *Bucket) GetOperationTimeout() time.Duration {
return b.client.GetOperationTimeout()
}

func (b *Bucket) decodeValue(bytes []byte, flags uint32, out interface{}) (interface{}, error) {
fmt.Printf("Early Flags: %08x\n", flags)

// Check for legacy flags
if flags&cfMask == 0 {
// Legacy Flags
if flags == lfJson {
// Legacy JSON
flags = cfFmtJson
} else {
return nil, clientError{"Unexpected legacy flags value"}
}
}

fmt.Printf("Flags: %08x\n", flags)

// Make sure compression is disabled
if flags&cfCmprMask != cfCmprNone {
return nil, clientError{"Unexpected value compression"}
}

// If an output object was passed, try to json Unmarshal to it
if out != nil {
if flags&cfFmtJson != 0 {
err := json.Unmarshal(bytes, out)
if err != nil {
return nil, clientError{err.Error()}
}
return out, nil
} else {
return nil, clientError{"Unmarshal target passed, but type does not match."}
}
}

// Normal types of decoding
if flags&cfFmtMask == cfFmtBinary {
return bytes, nil
} else if flags&cfFmtMask == cfFmtString {
return string(bytes[0:]), nil
} else if flags&cfFmtMask == cfFmtJson {
var outVal interface{}
err := json.Unmarshal(bytes, &outVal)
if err != nil {
return nil, clientError{err.Error()}
}
return outVal, nil
} else {
return nil, clientError{"Unexpected flags value"}
}
}

func (b *Bucket) encodeValue(value interface{}) ([]byte, uint32, error) {
var bytes []byte
var flags uint32
var err error

switch value.(type) {
case []byte:
bytes = value.([]byte)
flags = cfFmtBinary
case string:
bytes = []byte(value.(string))
flags = cfFmtString
default:
bytes, err = json.Marshal(value)
if err != nil {
return nil, 0, clientError{err.Error()}
}
flags = cfFmtJson
}

// No compression supported currently

return bytes, flags, nil
}

type getResult struct {
bytes []byte
flags uint32
Expand Down
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gocouchbase

import "time"
import "fmt"
import "github.com/couchbase/gocouchbaseio"
import "github.com/couchbaselabs/gocouchbaseio"
import "net/http"
import "crypto/tls"

Expand Down
80 changes: 80 additions & 0 deletions transcoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package gocouchbase

import "encoding/json"
import "fmt"

func (b *Bucket) decodeValue(bytes []byte, flags uint32, out interface{}) (interface{}, error) {
fmt.Printf("Early Flags: %08x\n", flags)

// Check for legacy flags
if flags&cfMask == 0 {
// Legacy Flags
if flags == lfJson {
// Legacy JSON
flags = cfFmtJson
} else {
return nil, clientError{"Unexpected legacy flags value"}
}
}

fmt.Printf("Flags: %08x\n", flags)

// Make sure compression is disabled
if flags&cfCmprMask != cfCmprNone {
return nil, clientError{"Unexpected value compression"}
}

// If an output object was passed, try to json Unmarshal to it
if out != nil {
if flags&cfFmtJson != 0 {
err := json.Unmarshal(bytes, out)
if err != nil {
return nil, clientError{err.Error()}
}
return out, nil
} else {
return nil, clientError{"Unmarshal target passed, but type does not match."}
}
}

// Normal types of decoding
if flags&cfFmtMask == cfFmtBinary {
return bytes, nil
} else if flags&cfFmtMask == cfFmtString {
return string(bytes[0:]), nil
} else if flags&cfFmtMask == cfFmtJson {
var outVal interface{}
err := json.Unmarshal(bytes, &outVal)
if err != nil {
return nil, clientError{err.Error()}
}
return outVal, nil
} else {
return nil, clientError{"Unexpected flags value"}
}
}

func (b *Bucket) encodeValue(value interface{}) ([]byte, uint32, error) {
var bytes []byte
var flags uint32
var err error

switch value.(type) {
case []byte:
bytes = value.([]byte)
flags = cfFmtBinary
case string:
bytes = []byte(value.(string))
flags = cfFmtString
default:
bytes, err = json.Marshal(value)
if err != nil {
return nil, 0, clientError{err.Error()}
}
flags = cfFmtJson
}

// No compression supported currently

return bytes, flags, nil
}

0 comments on commit 845047d

Please sign in to comment.