-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from stevenferrer/feature/index-input-docs
improved index JSON client API
- Loading branch information
Showing
11 changed files
with
162 additions
and
77 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
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,30 @@ | ||
package index | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// Docs contains the list of documents to be indexed | ||
type Docs struct { | ||
docs []interface{} | ||
} | ||
|
||
// NewDocs is a factory for *Docs | ||
func NewDocs() *Docs { | ||
return &Docs{docs: []interface{}{}} | ||
} | ||
|
||
// AddDoc adds a document to the list of documents | ||
func (d *Docs) AddDoc(doc interface{}) { | ||
d.docs = append(d.docs, doc) | ||
} | ||
|
||
// Count counts the number of documents | ||
func (d *Docs) Count() int { | ||
return len(d.docs) | ||
} | ||
|
||
// Marshal marshals the documents | ||
func (d *Docs) Marshal() ([]byte, error) { | ||
return json.Marshal(d.docs) | ||
} |
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,41 @@ | ||
package index_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stevenferrer/solr-go/index" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDocs(t *testing.T) { | ||
var names = []struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
}{ | ||
{ | ||
ID: "1", | ||
Name: "Milana Vino", | ||
}, | ||
{ | ||
ID: "2", | ||
Name: "Charly Jordan", | ||
}, | ||
{ | ||
ID: "3", | ||
Name: "Daisy Keech", | ||
}, | ||
} | ||
|
||
docs := index.NewDocs() | ||
for _, model := range names { | ||
docs.AddDoc(model) | ||
} | ||
|
||
assert.Equal(t, docs.Count(), 3) | ||
|
||
b, err := docs.Marshal() | ||
assert.NoError(t, err) | ||
|
||
expected := `[{"id":"1","name":"Milana Vino"},{"id":"2","name":"Charly Jordan"},{"id":"3","name":"Daisy Keech"}]` | ||
assert.Equal(t, expected, string(b)) | ||
} |
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
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
Oops, something went wrong.