Skip to content

Commit

Permalink
Handle WriteOptions masks when stringing
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Jan 17, 2014
1 parent 615a36e commit 0924711
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
38 changes: 25 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,31 @@ const (

// String representation of WriteOptions
func (w WriteOptions) String() string {
switch w {
case Raw:
return "raw"
case AddOnly:
return "addonly"
case Persist:
return "persist"
case Indexable:
return "indexable"
case Append:
return "append"
}
return fmt.Sprintf("0x%x", int(w))
f := []string{}
if Raw&w != 0 {
f = append(f, "raw")
w &= ^Raw
}
if AddOnly&w != 0 {
f = append(f, "addonly")
w &= ^AddOnly
}
if Persist&w != 0 {
f = append(f, "persist")
w &= ^Persist
}
if Indexable&w != 0 {
f = append(f, "indexable")
w &= ^Indexable
}
if Append&w != 0 {
f = append(f, "append")
w &= ^Append
}
if len(f) == 0 || w != 0 {
f = append(f, fmt.Sprintf("0x%x", int(w)))
}
return strings.Join(f, "|")
}

// Error returned from Write with AddOnly flag, when key already exists in the bucket.
Expand Down
28 changes: 28 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package couchbase

import "testing"

func TestWriteOptionsString(t *testing.T) {
tests := []struct {
opts WriteOptions
exp string
}{
{Raw, "raw"},
{AddOnly, "addonly"},
{Persist, "persist"},
{Indexable, "indexable"},
{Append, "append"},
{AddOnly | Raw, "raw|addonly"},
{0, "0x0"},
{Raw | AddOnly | Persist | Indexable | Append,
"raw|addonly|persist|indexable|append"},
{Raw | 8192, "raw|0x2000"},
}

for _, test := range tests {
got := test.opts.String()
if got != test.exp {
t.Errorf("Expected %v, got %v", test.exp, got)
}
}
}

0 comments on commit 0924711

Please sign in to comment.