Skip to content

Commit

Permalink
Fixed linter errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Feb 19, 2019
1 parent baa51bb commit 5a4dc95
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package hide

var hash Hash

// Interface used to marshal/unmarshal hide.ID to/from JSON.
// Hash is used to marshal/unmarshal hide.ID to/from JSON.
type Hash interface {
Encode(ID) ([]byte, error)
Decode([]byte) (ID, error)
}

// Sets the hide.Hash used to marshal/unmarshal hide.ID to/from JSON.
// UseHash sets the hide.Hash used to marshal/unmarshal hide.ID to/from JSON.
// hide.HashID is used by default.
func UseHash(hasher Hash) {
hash = hasher
Expand Down
16 changes: 8 additions & 8 deletions hashid.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type HashID struct {
MinLength int
}

// Creates a new HashID with given salt and minimum hash length.
// NewHashID creates a new HashID with given salt and minimum hash length.
func NewHashID(salt string, minlen int) *HashID {
return &HashID{salt, minlen}
}

// Encode implements the hide.Hash interface.
func (this *HashID) Encode(id ID) ([]byte, error) {
hash, err := this.newHash()
func (hasher *HashID) Encode(id ID) ([]byte, error) {
hash, err := hasher.newHash()

if err != nil {
return nil, err
Expand All @@ -34,12 +34,12 @@ func (this *HashID) Encode(id ID) ([]byte, error) {
}

// Decode implements the hide.Hash interface.
func (this *HashID) Decode(data []byte) (ID, error) {
func (hasher *HashID) Decode(data []byte) (ID, error) {
if len(data) == 0 {
return 0, nil
}

hash, err := this.newHash()
hash, err := hasher.newHash()

if err != nil {
return 0, err
Expand All @@ -59,10 +59,10 @@ func (this *HashID) Decode(data []byte) (ID, error) {
}

// Creates a new hashids.HashID object to encode/decode IDs.
func (this *HashID) newHash() (*hashids.HashID, error) {
func (hasher *HashID) newHash() (*hashids.HashID, error) {
config := hashids.NewData()
config.Salt = this.Salt
config.MinLength = this.MinLength
config.Salt = hasher.Salt
config.MinLength = hasher.MinLength
hash, err := hashids.NewWithData(config)

if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
type ID int64

// Scan implements the Scanner interface.
func (this *ID) Scan(value interface{}) error {
func (hideid *ID) Scan(value interface{}) error {
if value == nil {
*this = 0
*hideid = 0
return nil
}

Expand All @@ -25,26 +25,26 @@ func (this *ID) Scan(value interface{}) error {
return errors.New("unexpected type")
}

*this = ID(id)
*hideid = ID(id)
return nil
}

// Value implements the driver Valuer interface.
func (this ID) Value() (driver.Value, error) {
if this == 0 {
func (hideid ID) Value() (driver.Value, error) {
if hideid == 0 {
return nil, nil
}

return int64(this), nil
return int64(hideid), nil
}

// MarshalJSON implements the encoding json interface.
func (this ID) MarshalJSON() ([]byte, error) {
if this == 0 {
func (hideid ID) MarshalJSON() ([]byte, error) {
if hideid == 0 {
return json.Marshal(nil)
}

result, err := hash.Encode(this)
result, err := hash.Encode(hideid)

if err != nil {
return nil, err
Expand All @@ -54,10 +54,10 @@ func (this ID) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON implements the encoding json interface.
func (this *ID) UnmarshalJSON(data []byte) error {
func (hideid *ID) UnmarshalJSON(data []byte) error {
// convert null to 0
if strings.TrimSpace(string(data)) == "null" {
*this = 0
*hideid = 0
return nil
}

Expand All @@ -72,6 +72,6 @@ func (this *ID) UnmarshalJSON(data []byte) error {
return err
}

*this = ID(result)
*hideid = ID(result)
return nil
}
4 changes: 2 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package hide

// Returns a new ID from given hash by using the hasher or an error if it couldn't decode the hash.
// FromString returns a new ID from given hash by using the hasher or an error if it couldn't decode the hash.
func FromString(id string) (ID, error) {
return hash.Decode([]byte(id))
}

// Returns a new hash from given ID by using the hasher or an error if it couldn't encode the ID.
// ToString returns a new hash from given ID by using the hasher or an error if it couldn't encode the ID.
// If ID is 0, "null" will be returned.
func ToString(id ID) (string, error) {
if id == 0 {
Expand Down

0 comments on commit 5a4dc95

Please sign in to comment.