ax(batch): expand abbreviated parameter and local variable names across all packages
Applies AX principle 1 (Predictable Names Over Short Names) to function signatures and local variables: s->input/raw, v->target/value, d->duration, a,b->left,right, w->writer, r->reader, l->logger, p->part/databasePoint, fn parameter names left as-is where they are callback conventions. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
c67dca2a97
commit
b12db10680
11 changed files with 48 additions and 48 deletions
|
|
@ -9,8 +9,8 @@ import (
|
|||
|
||||
// t := parseSQLiteTimestamp("2006-01-02 15:04:05") // time.Time{...}
|
||||
// t := parseSQLiteTimestamp("") // time.Time{} (zero)
|
||||
func parseSQLiteTimestamp(s string) time.Time {
|
||||
if s == "" {
|
||||
func parseSQLiteTimestamp(raw string) time.Time {
|
||||
if raw == "" {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
|
|
@ -24,12 +24,12 @@ func parseSQLiteTimestamp(s string) time.Time {
|
|||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if t, err := time.Parse(format, s); err == nil {
|
||||
return t
|
||||
if parsed, err := time.Parse(format, raw); err == nil {
|
||||
return parsed
|
||||
}
|
||||
}
|
||||
|
||||
logging.Warn("failed to parse timestamp from database", logging.Fields{"timestamp": s})
|
||||
logging.Warn("failed to parse timestamp from database", logging.Fields{"timestamp": raw})
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -209,10 +209,10 @@ var (
|
|||
)
|
||||
|
||||
// logging.SetGlobal(logging.New(logging.Config{Level: logging.LevelDebug}))
|
||||
func SetGlobal(l *Logger) {
|
||||
func SetGlobal(logger *Logger) {
|
||||
globalMutex.Lock()
|
||||
defer globalMutex.Unlock()
|
||||
globalLogger = l
|
||||
globalLogger = logger
|
||||
}
|
||||
|
||||
// logger := logging.GetGlobal()
|
||||
|
|
@ -274,8 +274,8 @@ func Errorf(format string, args ...interface{}) {
|
|||
|
||||
// level, err := logging.ParseLevel("DEBUG") // LevelDebug, nil
|
||||
// level, err := logging.ParseLevel("nope") // LevelInfo, error
|
||||
func ParseLevel(s string) (Level, error) {
|
||||
switch strings.ToUpper(s) {
|
||||
func ParseLevel(input string) (Level, error) {
|
||||
switch strings.ToUpper(input) {
|
||||
case "DEBUG":
|
||||
return LevelDebug, nil
|
||||
case "INFO":
|
||||
|
|
@ -285,6 +285,6 @@ func ParseLevel(s string) (Level, error) {
|
|||
case "ERROR":
|
||||
return LevelError, nil
|
||||
default:
|
||||
return LevelInfo, fmt.Errorf("unknown log level: %s", s)
|
||||
return LevelInfo, fmt.Errorf("unknown log level: %s", input)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -254,7 +254,7 @@ func parseDigestParams(header string) map[string]string {
|
|||
}
|
||||
|
||||
// h := md5Hash("user:realm:pass") // "5f4dcc3b5aa765d61d8327deb882cf99"
|
||||
func md5Hash(s string) string {
|
||||
digest := md5.Sum([]byte(s))
|
||||
func md5Hash(input string) string {
|
||||
digest := md5.Sum([]byte(input))
|
||||
return hex.EncodeToString(digest[:])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,19 +30,19 @@ func putBuffer(buf *bytes.Buffer) {
|
|||
}
|
||||
|
||||
// UnmarshalJSON(data, &msg)
|
||||
func UnmarshalJSON(data []byte, v interface{}) error {
|
||||
return json.Unmarshal(data, v)
|
||||
func UnmarshalJSON(data []byte, target interface{}) error {
|
||||
return json.Unmarshal(data, target)
|
||||
}
|
||||
|
||||
// data, err := MarshalJSON(stats) // pooled buffer; safe to hold after call
|
||||
func MarshalJSON(v interface{}) ([]byte, error) {
|
||||
func MarshalJSON(value interface{}) ([]byte, error) {
|
||||
buf := getBuffer()
|
||||
defer putBuffer(buf)
|
||||
|
||||
encoder := json.NewEncoder(buf)
|
||||
// Don't escape HTML characters (matches json.Marshal behavior for these use cases)
|
||||
encoder.SetEscapeHTML(false)
|
||||
if err := encoder.Encode(v); err != nil {
|
||||
if err := encoder.Encode(value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,27 +15,27 @@ import (
|
|||
|
||||
// equalFold("xmrig", "XMRig") == true
|
||||
// equalFold("tt-miner", "TT-Miner") == true
|
||||
func equalFold(a, b string) bool {
|
||||
return bytes.EqualFold([]byte(a), []byte(b))
|
||||
func equalFold(left, right string) bool {
|
||||
return bytes.EqualFold([]byte(left), []byte(right))
|
||||
}
|
||||
|
||||
// hasPrefix("xmrig-rx0", "xmrig") == true
|
||||
// hasPrefix("ttminer-rtx", "xmrig") == false
|
||||
func hasPrefix(s, prefix string) bool {
|
||||
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
||||
func hasPrefix(input, prefix string) bool {
|
||||
return len(input) >= len(prefix) && input[:len(prefix)] == prefix
|
||||
}
|
||||
|
||||
// containsStr("peer not found", "not found") == true
|
||||
// containsStr("connection ok", "not found") == false
|
||||
func containsStr(s, substr string) bool {
|
||||
if len(substr) == 0 {
|
||||
func containsStr(haystack, needle string) bool {
|
||||
if len(needle) == 0 {
|
||||
return true
|
||||
}
|
||||
if len(s) < len(substr) {
|
||||
if len(haystack) < len(needle) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
for i := 0; i <= len(haystack)-len(needle); i++ {
|
||||
if haystack[i:i+len(needle)] == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
@ -788,10 +788,10 @@ func (m *Manager) GetMinerHistoricalHashrate(minerName string, since, until time
|
|||
|
||||
// Convert database points to mining points
|
||||
points := make([]HashratePoint, len(dbPoints))
|
||||
for i, p := range dbPoints {
|
||||
for i, databasePoint := range dbPoints {
|
||||
points[i] = HashratePoint{
|
||||
Timestamp: p.Timestamp,
|
||||
Hashrate: p.Hashrate,
|
||||
Timestamp: databasePoint.Timestamp,
|
||||
Hashrate: databasePoint.Hashrate,
|
||||
}
|
||||
}
|
||||
return points, nil
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func NewLatencyHistogram(maxSize int) *LatencyHistogram {
|
|||
}
|
||||
|
||||
// h.Record(42 * time.Millisecond) // call after each request completes
|
||||
func (histogram *LatencyHistogram) Record(d time.Duration) {
|
||||
func (histogram *LatencyHistogram) Record(duration time.Duration) {
|
||||
histogram.mutex.Lock()
|
||||
defer histogram.mutex.Unlock()
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ func (histogram *LatencyHistogram) Record(d time.Duration) {
|
|||
copy(histogram.samples, histogram.samples[1:])
|
||||
histogram.samples = histogram.samples[:len(histogram.samples)-1]
|
||||
}
|
||||
histogram.samples = append(histogram.samples, d)
|
||||
histogram.samples = append(histogram.samples, duration)
|
||||
}
|
||||
|
||||
// avg := h.Average() // returns 0 if no samples recorded
|
||||
|
|
@ -73,8 +73,8 @@ func (histogram *LatencyHistogram) Average() time.Duration {
|
|||
}
|
||||
|
||||
var total time.Duration
|
||||
for _, d := range histogram.samples {
|
||||
total += d
|
||||
for _, sample := range histogram.samples {
|
||||
total += sample
|
||||
}
|
||||
return total / time.Duration(len(histogram.samples))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -288,11 +288,11 @@ func (b *BaseMiner) InstallFromURL(url string) error {
|
|||
// a := parseVersion("6.24.0") // [6, 24, 0]
|
||||
// b := parseVersion("5.0.1") // [5, 0, 1]
|
||||
// if compareVersions(a, b) > 0 { /* a is newer */ }
|
||||
func parseVersion(v string) []int {
|
||||
parts := strings.Split(v, ".")
|
||||
func parseVersion(versionString string) []int {
|
||||
parts := strings.Split(versionString, ".")
|
||||
intParts := make([]int, len(parts))
|
||||
for i, p := range parts {
|
||||
val, err := strconv.Atoi(p)
|
||||
for i, part := range parts {
|
||||
val, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
return []int{0} // Malformed version, treat as very old
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
// toLowerASCII("XMRig") == "xmrig"
|
||||
// toLowerASCII("TT-Miner") == "tt-miner"
|
||||
func toLowerASCII(s string) string {
|
||||
runes := []rune(s)
|
||||
func toLowerASCII(input string) string {
|
||||
runes := []rune(input)
|
||||
for i, r := range runes {
|
||||
runes[i] = unicode.ToLower(r)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -211,8 +211,8 @@ func (c *Config) Validate() error {
|
|||
|
||||
// containsShellChars(";echo bad") == true
|
||||
// containsShellChars("stratum+tcp://pool.example.com:3333") == false
|
||||
func containsShellChars(s string) bool {
|
||||
for _, r := range s {
|
||||
func containsShellChars(input string) bool {
|
||||
for _, r := range input {
|
||||
switch r {
|
||||
case ';', '|', '&', '`', '$', '(', ')', '{', '}', '<', '>', '\n', '\r', '\\', '\'', '"', '!':
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -31,20 +31,20 @@ func putBuffer(buffer *bytes.Buffer) {
|
|||
|
||||
// var msg Message
|
||||
// if err := UnmarshalJSON(data, &msg); err != nil { return nil, err }
|
||||
func UnmarshalJSON(data []byte, v interface{}) error {
|
||||
return json.Unmarshal(data, v)
|
||||
func UnmarshalJSON(data []byte, target interface{}) error {
|
||||
return json.Unmarshal(data, target)
|
||||
}
|
||||
|
||||
// data, err := MarshalJSON(msg)
|
||||
// if err != nil { return nil, err }
|
||||
func MarshalJSON(v interface{}) ([]byte, error) {
|
||||
func MarshalJSON(value interface{}) ([]byte, error) {
|
||||
buffer := getBuffer()
|
||||
defer putBuffer(buffer)
|
||||
|
||||
encoder := json.NewEncoder(buffer)
|
||||
// Don't escape HTML characters (matches json.Marshal behavior for these use cases)
|
||||
encoder.SetEscapeHTML(false)
|
||||
if err := encoder.Encode(v); err != nil {
|
||||
if err := encoder.Encode(value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -352,16 +352,16 @@ func extractTarball(tarData []byte, destDir string) (string, error) {
|
|||
|
||||
// StreamBundle(bundle, conn) // stream to peer over WebSocket or HTTP body
|
||||
// StreamBundle(bundle, os.Stdout) // debug: print bundle JSON to stdout
|
||||
func StreamBundle(bundle *Bundle, w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
func StreamBundle(bundle *Bundle, writer io.Writer) error {
|
||||
encoder := json.NewEncoder(writer)
|
||||
return encoder.Encode(bundle)
|
||||
}
|
||||
|
||||
// bundle, err := ReadBundle(conn) // read from WebSocket or HTTP response body
|
||||
// if err != nil { return nil, fmt.Errorf("read bundle: %w", err) }
|
||||
func ReadBundle(r io.Reader) (*Bundle, error) {
|
||||
func ReadBundle(reader io.Reader) (*Bundle, error) {
|
||||
var bundle Bundle
|
||||
decoder := json.NewDecoder(r)
|
||||
decoder := json.NewDecoder(reader)
|
||||
if err := decoder.Decode(&bundle); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue