-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mango index & queries to couchdb package
- Loading branch information
Romain
committed
Oct 17, 2016
1 parent
5e3180b
commit 39152df
Showing
6 changed files
with
399 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mango | ||
|
||
import "encoding/json" | ||
|
||
// An IndexDefinition is just a list of fields to be indexed. | ||
type IndexDefinition []string | ||
|
||
// MarshalJSON implements the json.Marshaller interface on IndexDefinition | ||
// by wrapping it in a {"fields": ...} | ||
func (def IndexDefinition) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(makeMap("fields", []string(def))) | ||
} | ||
|
||
// An IndexDefinitionRequest is a request to be POSTED to create the index | ||
type IndexDefinitionRequest struct { | ||
Name string `json:"name,omitempty"` | ||
DDoc string `json:"ddoc,omitempty"` | ||
Index IndexDefinition `json:"index"` | ||
} | ||
|
||
// IndexOnFields constructs a new IndexDefinitionRequest | ||
// it lets couchdb defaults for index & designdoc names. | ||
func IndexOnFields(fields ...string) IndexDefinitionRequest { | ||
return IndexDefinitionRequest{ | ||
Index: IndexDefinition(fields), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mango | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndexMarshaling(t *testing.T) { | ||
def := IndexOnFields("folderID", "name") | ||
jsonbytes, _ := json.Marshal(def) | ||
expected := `{"index":{"fields":["folderID","name"]}}` | ||
assert.Equal(t, expected, string(jsonbytes), "index should MarshalJSON properly") | ||
} |
Oops, something went wrong.