Skip to content

Commit

Permalink
Fix all golint warnings
Browse files Browse the repository at this point in the history
This quite possibly breaks someone's application.
  • Loading branch information
dustin committed Dec 5, 2013
1 parent e200d3f commit bbf38cb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 110 deletions.
8 changes: 4 additions & 4 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

// Handle the stream of changes coming from a Changes thing.
// A ChangeHandler handles the stream of changes coming from Changes.
//
// The handler returns the next sequence number when the stream should
// be resumed, otherwise -1 to indicate the changes feed should stop.
Expand Down Expand Up @@ -65,7 +65,7 @@ func i64defopt(opts map[string]interface{}, k string, def int64) int64 {
return rv
}

// Feed the changes.
// Changes feeds a ChangeHandler a CouchDB changes feed.
//
// The handler receives the body of the stream and is expected to consume
// the contents.
Expand Down Expand Up @@ -96,7 +96,7 @@ func (p Database) Changes(handler ChangeHandler,
params.Del("heartbeat")
}

full_url := fmt.Sprintf("%s/_changes?%s", p.DBURL(),
fullURL := fmt.Sprintf("%s/_changes?%s", p.DBURL(),
params.Encode())

var conn net.Conn
Expand All @@ -111,7 +111,7 @@ func (p Database) Changes(handler ChangeHandler,
},
}}

resp, err := client.Get(full_url)
resp, err := client.Get(fullURL)
if err == nil {
func() {
defer resp.Body.Close()
Expand Down
74 changes: 38 additions & 36 deletions couch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ var defaultHdrs = map[string][]string{}
// HTTP Client used by typical requests.
//
// Defaults to http.DefaultClient
var HttpClient = http.DefaultClient
var HTTPClient = http.DefaultClient

// HttpError represents errors returned from unsuccessful HTTP requests.
type HttpError struct {
// HTTPError represents errors returned from unsuccessful HTTP requests.
type HTTPError struct {
Status int
Msg string
}

func (e *HttpError) Error() string {
func (e *HTTPError) Error() string {
return e.Msg
}

Expand All @@ -50,21 +50,21 @@ func unmarshalURL(u string, results interface{}) error {
return err
}

r, err := HttpClient.Do(req)
r, err := HTTPClient.Do(req)
if err != nil {
return err
}
defer r.Body.Close()

if r.StatusCode != 200 {
return &HttpError{r.StatusCode, r.Status}
return &HTTPError{r.StatusCode, r.Status}
}

return json.NewDecoder(r.Body).Decode(results)
}

type IdAndRev struct {
Id string `json:"_id"`
type idAndRev struct {
ID string `json:"_id"`
Rev string `json:"_rev"`
}

Expand Down Expand Up @@ -92,18 +92,19 @@ func interact(method, u string, headers map[string][]string, in []byte, out inte
req.Header = fullHeaders
req.Close = true

res, err := HttpClient.Do(req)
res, err := HTTPClient.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()

if res.StatusCode < 200 || res.StatusCode >= 300 {
return res.StatusCode, &HttpError{res.StatusCode, res.Status}
return res.StatusCode, &HTTPError{res.StatusCode, res.Status}
}
return res.StatusCode, json.NewDecoder(res.Body).Decode(out)
}

// Database represents operations available on an existing CouchDB
type Database struct {
Host string
Port string
Expand All @@ -114,18 +115,20 @@ type Database struct {
changesFailDelay time.Duration
}

// BaseURL returns the URL to the database server containing this database.
func (p Database) BaseURL() string {
if p.authinfo == nil {
return fmt.Sprintf("http://%s:%s", p.Host, p.Port)
}
return fmt.Sprintf("http://%s@%s:%s", p.authinfo.String(), p.Host, p.Port)
}

// DBURL returns the URL to this specific database.
func (p Database) DBURL() string {
return fmt.Sprintf("%s/%s", p.BaseURL(), p.Name)
}

// Test whether CouchDB is running (ignores Database.Name)
// Running returns true if CouchDB is running (ignores Database.Name)
func (p Database) Running() bool {
dbs := []string{}
u := fmt.Sprintf("%s/%s", p.BaseURL(), "_all_dbs")
Expand All @@ -137,7 +140,7 @@ type databaseInfo struct {
// other stuff too, ignore for now
}

// Test whether specified database exists in specified CouchDB instance
// Exists returns true if this database exists on the CouchDB server
func (p Database) Exists() bool {
di := &databaseInfo{}
return unmarshalURL(p.DBURL(), &di) == nil && di.DBName == p.Name
Expand All @@ -163,12 +166,12 @@ func (p Database) createDatabase() error {
return p.simpleOp("PUT", p.DBURL(), errNewDB)
}

// Deletes the given database and all documents
// DeleteDatabase deletes the given database and all documents
func (p Database) DeleteDatabase() error {
return p.simpleOp("DELETE", p.DBURL(), errDelDB)
}

var errNotRunning = errors.New("CouchDB not running")
var errNotRunning = errors.New("couchdb not running")

// Connect to the database at the given URL.
// example: couch.Connect("http://localhost:5984/testdb/")
Expand Down Expand Up @@ -196,6 +199,8 @@ func Connect(dburl string) (Database, error) {
return db, nil
}

// NewDatabase connects to a CouchDB server and creates the specified
// database if it does not exist.
func NewDatabase(host, port, name string) (Database, error) {
db := Database{host, port, name, nil, net.Dial, defaultChangeDelay}
if !db.Running() {
Expand Down Expand Up @@ -231,9 +236,10 @@ func cleanJSON(d interface{}) (jsonBuf []byte, id, rev string, err error) {
return
}

// Response represents a typical command response from against a CouchDB server.
type Response struct {
Ok bool
Id string
ID string
Rev string
Error string
Reason string
Expand All @@ -258,7 +264,7 @@ func (p Database) Bulk(docs []interface{}) (results []Response, err error) {
return
}

// Inserts document to CouchDB, returning id and rev on success.
// Insert a document into CouchDB, returning id and rev on success.
// Document may specify both "_id" and "_rev" fields (will overwrite existing)
// or just "_id" (will use that id, but not overwrite existing)
// or neither (will use autogenerated id)
Expand All @@ -268,8 +274,8 @@ func (p Database) Insert(d interface{}) (string, string, error) {
return "", "", err
}
if id != "" && rev != "" {
new_rev, err2 := p.Edit(d)
return id, new_rev, err2
newRev, err2 := p.Edit(d)
return id, newRev, err2
} else if id != "" {
return p.insertWith(jsonBuf, id)
} else {
Expand All @@ -286,11 +292,12 @@ func (p Database) insert(jsonBuf []byte) (string, string, error) {
if !ir.Ok {
return "", "", fmt.Errorf("%s: %s", ir.Error, ir.Reason)
}
return ir.Id, ir.Rev, nil
return ir.ID, ir.Rev, nil
}

// Inserts the given document (shouldn't contain "_id" or "_rev" tagged fields)
// using the passed 'id' as the _id. Will fail if the id already exists.
// InsertWith inserts the given document (shouldn't contain "_id" or
// "_rev" tagged fields) using the passed 'id' as the _id. Will fail
// if the id already exists.
func (p Database) InsertWith(d interface{}, id string) (string, string, error) {
jsonBuf, err := json.Marshal(d)
if err != nil {
Expand All @@ -309,35 +316,35 @@ func (p Database) insertWith(jsonBuf []byte, id string) (string, string, error)
if !ir.Ok {
return "", "", fmt.Errorf("%s: %s", ir.Error, ir.Reason)
}
return ir.Id, ir.Rev, nil
return ir.ID, ir.Rev, nil
}

var errNoRev = errors.New("rev not specified in interface (try InsertWith)")

// Edits the given document, returning the new revision.
// Edit edits the given document, returning the new revision.
// d must contain "_id" and "_rev" tagged fields.
func (p Database) Edit(d interface{}) (string, error) {
jsonBuf, err := json.Marshal(d)
if err != nil {
return "", err
}
idRev := IdAndRev{}
idRev := idAndRev{}
must(json.Unmarshal(jsonBuf, &idRev))
if idRev.Id == "" {
if idRev.ID == "" {
return "", errNoID
}
if idRev.Rev == "" {
return "", errNoRev
}
u := fmt.Sprintf("%s/%s", p.DBURL(), url.QueryEscape(idRev.Id))
u := fmt.Sprintf("%s/%s", p.DBURL(), url.QueryEscape(idRev.ID))
ir := Response{}
if _, err = interact("PUT", u, defaultHdrs, jsonBuf, &ir); err != nil {
return "", err
}
return ir.Rev, nil
}

// Edits the given document, returning the new revision.
// EditWith edits the given document, returning the new revision.
// d should not contain "_id" or "_rev" tagged fields. If it does, they will
// be overwritten with the passed values.
func (p Database) EditWith(d interface{}, id, rev string) (string, error) {
Expand All @@ -360,7 +367,7 @@ func (p Database) EditWith(d interface{}, id, rev string) (string, error) {

var errNoID = errors.New("no id specified")

// Unmarshals the document matching id to the given interface
// Retrieve unmarshals the document matching id to the given interface
func (p Database) Retrieve(id string, d interface{}) error {
if id == "" {
return errNoID
Expand All @@ -369,7 +376,7 @@ func (p Database) Retrieve(id string, d interface{}) error {
return unmarshalURL(fmt.Sprintf("%s/%s", p.DBURL(), id), d)
}

// Deletes document given by id and rev.
// Delete deletes document given by id and rev.
func (p Database) Delete(id, rev string) error {
headers := map[string][]string{
"If-Match": []string{rev},
Expand All @@ -385,12 +392,7 @@ func (p Database) Delete(id, rev string) error {
return nil
}

type Row struct {
Id *string
Key *string
}

// Result from GetInfo
// DBInfo represents the result from GetInfo
type DBInfo struct {
Name string `json:"db_name"`
DocCount int64 `json:"doc_count"`
Expand All @@ -405,7 +407,7 @@ type DBInfo struct {
CommitedSeq int64 `json:"committed_update_seq"`
}

// Get the DBInfo for this database.
// GetInfo gets the DBInfo for this database.
func (p Database) GetInfo() (DBInfo, error) {
rv := DBInfo{}
err := unmarshalURL(p.DBURL(), &rv)
Expand Down
Loading

0 comments on commit bbf38cb

Please sign in to comment.