Browse files

Handle WriteOptions masks when stringing

  • Loading branch information...
1 parent 615a36e commit 0924711d24aafdc802fec2c69e984cfeb7dc504d @dustin dustin committed Jan 17, 2014
Showing with 53 additions and 13 deletions.
  1. +25 −13 client.go
  2. +28 −0 client_test.go
View
38 client.go
@@ -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.
View
28 client_test.go
@@ -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.