Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
This commit cleans up various Go code in the internal directory.
- Ensures comments on exported functions
- Changes all *Server receiver in all files to be "s", instead
  of mixed "c", "s", "server", etc.
- Silenced Go warnings for if/else with returns.
- Cleaned up import ordering.
  • Loading branch information
tidwall committed Oct 30, 2019
1 parent 981d9ec commit c084aee
Show file tree
Hide file tree
Showing 35 changed files with 526 additions and 551 deletions.
20 changes: 10 additions & 10 deletions internal/bing/bing.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func TileXYToPixelXY(tileX, tileY int64) (pixelX, pixelY int64) {
return tileX << 8, tileY << 8
}

/// TileXYToQuadKey converts tile XY coordinates into a QuadKey at a specified level of detail.
/// Param 'tileX' is the tile X coordinate.
/// Param 'tileY' is the tile Y coordinate.
/// Param 'levelOfDetail' is the Level of detail, from 1 (lowest detail) to N (highest detail).
/// Returns a string containing the QuadKey.
// TileXYToQuadKey converts tile XY coordinates into a QuadKey at a specified level of detail.
// Param 'tileX' is the tile X coordinate.
// Param 'tileY' is the tile Y coordinate.
// Param 'levelOfDetail' is the Level of detail, from 1 (lowest detail) to N (highest detail).
// Returns a string containing the QuadKey.
func TileXYToQuadKey(tileX, tileY int64, levelOfDetail uint64) string {
quadKey := make([]byte, levelOfDetail)
for i, j := levelOfDetail, 0; i > 0; i, j = i-1, j+1 {
Expand All @@ -136,11 +136,11 @@ func TileXYToQuadKey(tileX, tileY int64, levelOfDetail uint64) string {
return string(quadKey)
}

/// QuadKeyToTileXY converts a QuadKey into tile XY coordinates.
/// Param 'quadKey' is the quadKey of the tile.
/// Return value 'tileX' is the output parameter receiving the tile X coordinate.
/// Return value 'tileY is the output parameter receiving the tile Y coordinate.
/// Return value 'levelOfDetail' is the output parameter receiving the level of detail.
// QuadKeyToTileXY converts a QuadKey into tile XY coordinates.
// Param 'quadKey' is the quadKey of the tile.
// Return value 'tileX' is the output parameter receiving the tile X coordinate.
// Return value 'tileY is the output parameter receiving the tile Y coordinate.
// Return value 'levelOfDetail' is the output parameter receiving the level of detail.
func QuadKeyToTileXY(quadKey string) (tileX, tileY int64, levelOfDetail uint64) {
levelOfDetail = uint64(len(quadKey))
for i := levelOfDetail; i > 0; i-- {
Expand Down
23 changes: 12 additions & 11 deletions internal/deadline/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ type Deadline struct {
}

// New returns a new deadline object
func New(deadline time.Time) *Deadline {
return &Deadline{unixNano: deadline.UnixNano()}
func New(dl time.Time) *Deadline {
return &Deadline{unixNano: dl.UnixNano()}
}

// Check the deadline and panic when reached
//go:noinline
func (deadline *Deadline) Check() {
if deadline == nil || deadline.unixNano == 0 {
func (dl *Deadline) Check() {
if dl == nil || dl.unixNano == 0 {
return
}
if !deadline.hit && time.Now().UnixNano() > deadline.unixNano {
deadline.hit = true
if !dl.hit && time.Now().UnixNano() > dl.unixNano {
dl.hit = true
panic("deadline")
}
}

// Hit returns true if the deadline has been hit
func (deadline *Deadline) Hit() bool {
return deadline.hit
func (dl *Deadline) Hit() bool {
return dl.hit
}

// GetDeadlineTime returns the time object for the deadline, and an "empty" boolean
func (deadline *Deadline) GetDeadlineTime() (time.Time) {
return time.Unix(0, deadline.unixNano)
// GetDeadlineTime returns the time object for the deadline, and an
// "empty" boolean
func (dl *Deadline) GetDeadlineTime() time.Time {
return time.Unix(0, dl.unixNano)
}
4 changes: 1 addition & 3 deletions internal/endpoint/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/streadway/amqp"
)

const (
amqpExpiresAfter = time.Second * 30
)
const amqpExpiresAfter = time.Second * 30

// AMQPConn is an endpoint connection
type AMQPConn struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/disque.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/tidwall/tile38/internal/log"
)

const (
disqueExpiresAfter = time.Second * 30
)
const disqueExpiresAfter = time.Second * 30

// DisqueConn is an endpoint connection
type DisqueConn struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"google.golang.org/grpc"
)

const (
grpcExpiresAfter = time.Second * 30
)
const grpcExpiresAfter = time.Second * 30

// GRPCConn is an endpoint connection
type GRPCConn struct {
Expand Down
6 changes: 2 additions & 4 deletions internal/endpoint/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package endpoint
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
"sync"
"time"

"github.com/Shopify/sarama"
"github.com/tidwall/gjson"
)

const (
kafkaExpiresAfter = time.Second * 30
)
const kafkaExpiresAfter = time.Second * 30

// KafkaConn is an endpoint connection
type KafkaConn struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"time"
)

const (
localExpiresAfter = time.Second * 30
)
const localExpiresAfter = time.Second * 30

// LocalPublisher is used to publish local notifcations
type LocalPublisher interface {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/nats-io/go-nats"
)

const (
natsExpiresAfter = time.Second * 30
)
const natsExpiresAfter = time.Second * 30

// NATSConn is an endpoint connection
type NATSConn struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/gomodule/redigo/redis"
)

const (
redisExpiresAfter = time.Second * 30
)
const redisExpiresAfter = time.Second * 30

// RedisConn is an endpoint connection
type RedisConn struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/endpoint/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (

var errCreateQueue = errors.New("Error while creating queue")

const (
sqsExpiresAfter = time.Second * 30
)
const sqsExpiresAfter = time.Second * 30

// SQSConn is an endpoint connection
type SQSConn struct {
Expand Down
9 changes: 7 additions & 2 deletions internal/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package glob

import "strings"

// Glob structure for simple string matching
type Glob struct {
Pattern string
Desc bool
Limits []string
IsGlob bool
}

func Match(pattern, name string) (matched bool, err error) {
return wildcardMatch(pattern, name)
// Match returns true when string matches pattern. Returns an error when the
// pattern is invalid.
func Match(pattern, str string) (matched bool, err error) {
return wildcardMatch(pattern, str)
}

// IsGlob returns true when the pattern is a valid glob
func IsGlob(pattern string) bool {
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
Expand All @@ -24,6 +28,7 @@ func IsGlob(pattern string) bool {
return false
}

// Parse returns a glob structure from the pattern.
func Parse(pattern string, desc bool) *Glob {
g := &Glob{Pattern: pattern, Desc: desc, Limits: []string{"", ""}}
if strings.HasPrefix(pattern, "*") {
Expand Down
1 change: 0 additions & 1 deletion internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ func BenchmarkLogPrintf(t *testing.B) {
for i := 0; i < t.N; i++ {
Printf("X %s", "Y")
}

}
Loading

0 comments on commit c084aee

Please sign in to comment.