0f5a2f4ef5
* enable more linters * Run gofmt -s * run goimports * run unconvert * fix naked return * fix misspellings * fix ineffectual assigments * fix missing declaration * don't use deprecated grpc.Errof * check errors in tests * run gofmt -w -r "assert.Nil(err) -> assert.NoError(err)" * fix directory permissions * don't use nil Context * simplify boolean expressions * use bytes.Equal instead of bytes.Compare * merge variable declarations, remove redundant returns * fix some golint errors * run goimports * handle more errors * delete empty TestMain * delete empty TestMain * ignore examples for now * fix lint errors * remove unused values * more fixes * run gofmt -w -s . * add more comments * fix naming * more lint fixes * try switching travis to go1.11 * fix unnecessary conversions * fix deprecated methods * use go1.10 and disable gofmt/goimports for now * switch to 1.10 * don't re-enable gofmt and goimports * switch covermode to atomic because of -race * gofmt
38 lines
741 B
Go
38 lines
741 B
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// GetBytes transforms an empty interface type into a byte slice
|
|
func GetBytes(key interface{}) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
enc := gob.NewEncoder(&buf)
|
|
err := enc.Encode(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// ParseURL extracts database parameters from a string as a URL
|
|
// bolt://storj.db
|
|
// bolt://C:\storj.db
|
|
// redis://hostname
|
|
func ParseURL(s string) (*url.URL, error) {
|
|
if strings.HasPrefix(s, "bolt://") {
|
|
return &url.URL{
|
|
Scheme: "bolt",
|
|
Path: strings.TrimPrefix(s, "bolt://"),
|
|
}, nil
|
|
}
|
|
|
|
return url.Parse(s)
|
|
}
|