Skip to content

Commit

Permalink
feat(core): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielHAlba committed Jul 14, 2022
1 parent 55d750d commit 832bfc1
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 133 deletions.
7 changes: 3 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ linters-settings:
gocritic:
disabled-checks:
- unnamedResult
- hugeParam
- whyNoLint
enabled-tags:
- performance
Expand All @@ -53,7 +52,7 @@ linters:
enable-all: false
disable-all: true
enable:
# - lll
- lll
- misspell
- goconst
- gochecknoinits
Expand All @@ -68,14 +67,14 @@ linters:
- gofumpt
- gocritic
- vet
# - revive
- revive
- bodyclose
- deadcode
- errcheck
- gosec
- structcheck
- unconvert
# - dupl
- dupl
- varcheck
- unparam
- staticcheck
Expand Down
7 changes: 7 additions & 0 deletions agtime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,45 +120,52 @@ func (t *NullableDate) UnmarshalJSON(data []byte) error {
return nil
}

// TruncateMonth returns a nullable date with truncated month
func (t *NullableDate) TruncateMonth() NullableDate {
return NewNullDate(TruncateMonth(t.Time))
}

// After checks if nullable date `t` is after time `a`
func (t *NullableDate) After(a time.Time) bool {
if t.Valid {
return t.Time.After(a)
}
return false
}

// Before checks if nullable date `t` is before time `a`
func (t *NullableDate) Before(a time.Time) bool {
if t.Valid {
return t.Time.Before(a)
}
return false
}

// Equal checks if nullable date `t` is equal time `a`
func (t *NullableDate) Equal(a time.Time) bool {
if t.Valid {
return t.Time.Equal(a)
}
return false
}

// AftEq checks if nullable date `t` is after or equal the time `a`
func (t *NullableDate) AftEq(a time.Time) bool {
if t.Valid {
return t.After(a) || t.Equal(a)
}
return false
}

// BfEq checks if nullable date `t` is before or equal the time `a`
func (t *NullableDate) BfEq(a time.Time) bool {
if t.Valid {
return t.Before(a) || t.Equal(a)
}
return false
}

// Between checks if nullable date `t` is between the times `a` and `b`
func (t *NullableDate) Between(a, b time.Time) bool {
return t.AftEq(a) && t.BfEq(b)
}
Expand Down
57 changes: 57 additions & 0 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package redis

import (
"context"
"time"

"github.com/go-redis/redis/v8"

"github.com/agflow/tools/log"
)

// Client is a wrapper of a redis client
type Client struct {
Redis *redis.Client
}

// NewClient returns a verified redis client
func NewClient(address, password string) *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: 0, // use default DB
})
if rdb == nil {
log.Warnf("failed to initialize redis client")
return rdb
}

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

if err := rdb.Ping(ctx).Err(); err != nil {
log.Warnf("can't ping redis, %v", err)
}
return rdb
}

// New returns a new redis client
func New(address, password string) *Client {
redisClient := NewClient(address, password)
return &Client{Redis: redisClient}
}

// Get gets the value stored on `key`
func (c *Client) Get(ctx context.Context, key string) ([]byte, error) {
return c.Redis.Get(ctx, key).Bytes()
}

// Set sets `value` on `key` with a `timeout``
func (c *Client) Set(
ctx context.Context,
key string,
value interface{},
timeout time.Duration,
) error {
return c.Redis.Set(ctx, key, value, timeout).Err()
}
12 changes: 12 additions & 0 deletions cache/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cache

import (
"context"
"time"
)

// Service declares the interface of a cache service
type Service interface {
Get(context.Context, string) ([]byte, error)
Set(context.Context, string, interface{}, time.Duration) error
}
45 changes: 20 additions & 25 deletions group_by.go → groupby/group_by.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
package main
package groupby

import (
"crypto/sha512"
"encoding/json"
"errors"
"fmt"
"reflect"
"sync"
"time"

"github.com/agflow/tools/log"
"github.com/agflow/tools/types"
"github.com/agflow/tools/security"
"github.com/agflow/tools/typing"
)

func Hash(vs interface{}) (string, error) {
h := sha512.New()
r, err := json.Marshal(vs)
if err != nil {
return "", err
}
h.Write(r)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

func getGroupHash(v reflect.Value, cols []string) (string, error) {
grouped := make([]interface{}, len(cols))
for j := range cols {
grouped[j] = v.FieldByName(cols[j]).Interface()
}
return Hash(grouped)
return security.Hash(grouped)
}

// AggrFunc is an aggregation function
type AggrFunc func([]interface{}) interface{}

var wg sync.WaitGroup //nolint: gochecknoglobals

// GroupBy groups a slice of structs with an aggregation function
func GroupBy(on, dest interface{}, cols []string, funcs map[string]AggrFunc) error { //nolint: deadcode
// Agg groups by a slice of structs with an aggregation function
func Agg(on, dest interface{}, cols []string, funcs map[string]AggrFunc) error {
groupMap := make(map[interface{}]chan interface{})
if reflect.TypeOf(on).Kind() != reflect.Slice {
return errors.New("on needs to be slice")
Expand All @@ -45,12 +34,12 @@ func GroupBy(on, dest interface{}, cols []string, funcs map[string]AggrFunc) err
destVal := reflect.ValueOf(dest)
direct := reflect.Indirect(destVal)

sliceType, err := types.BaseType(destVal.Type(), reflect.Slice)
sliceType, err := typing.Base(destVal.Type(), reflect.Slice)
if err != nil {
return err
}

baseType := types.DeRef(sliceType.Elem())
baseType := typing.DeRef(sliceType.Elem())

s := reflect.ValueOf(on)
finishChan := make(chan bool)
Expand Down Expand Up @@ -94,7 +83,12 @@ func GroupBy(on, dest interface{}, cols []string, funcs map[string]AggrFunc) err
return nil
}

func processFun(v chan interface{}, funcs map[string]AggrFunc, dest reflect.Value, finish chan bool) {
func processFun(
v chan interface{},
funcs map[string]AggrFunc,
dest reflect.Value,
finish chan bool,
) {
defer wg.Done()
var isFinished bool
grouped := make([]interface{}, 0)
Expand All @@ -114,10 +108,11 @@ func processFun(v chan interface{}, funcs map[string]AggrFunc, dest reflect.Valu
}
}

// FoldFunc is a fold function
type FoldFunc func(interface{}, interface{}) interface{}

// GroupByFold groups a slice of structs with a fold function
func GroupByFold(on, dest interface{}, cols []string, funcs map[string]FoldFunc) error { //nolint: deadcode
// Fold groups by a slice of structs with a fold function
func Fold(on, dest interface{}, cols []string, funcs map[string]FoldFunc) error {
groupMap := make(map[interface{}]int)
if reflect.TypeOf(on).Kind() != reflect.Slice {
return errors.New("on needs to be slice")
Expand All @@ -126,12 +121,12 @@ func GroupByFold(on, dest interface{}, cols []string, funcs map[string]FoldFunc)
destVal := reflect.ValueOf(dest)
direct := reflect.Indirect(destVal)

sliceType, err := types.BaseType(destVal.Type(), reflect.Slice)
sliceType, err := typing.Base(destVal.Type(), reflect.Slice)
if err != nil {
return err
}

baseType := types.DeRef(sliceType.Elem())
baseType := typing.DeRef(sliceType.Elem())

s := reflect.ValueOf(on)
for i := 0; i < s.Len(); i++ {
Expand Down
14 changes: 0 additions & 14 deletions handlers/handler.go

This file was deleted.

2 changes: 1 addition & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
timeFormat = "2006/01/02 15:04:05"
)

//nolint: gochecknoinits
// nolint: gochecknoinits
func init() {
log.SetFlags(0)
}
Expand Down
31 changes: 0 additions & 31 deletions redis/redis.go

This file was deleted.

18 changes: 18 additions & 0 deletions security/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package security

import (
"crypto/sha512"
"encoding/json"
"fmt"
)

// Hash hashes `vs`
func Hash(vs interface{}) (string, error) {
h := sha512.New()
r, err := json.Marshal(vs)
if err != nil {
return "", err
}
h.Write(r)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
8 changes: 4 additions & 4 deletions sql/pgu.go → sql/db/pgu.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sql
package db

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"reflect"

"github.com/agflow/tools/log"
"github.com/agflow/tools/types"
"github.com/agflow/tools/typing"
)

// Select runs query on database with arguments and saves result on dest variable
Expand Down Expand Up @@ -112,13 +112,13 @@ func scanAll(rows *sql.Rows, dest interface{}, structOnly bool) error {
}
direct := reflect.Indirect(value)

slice, err := types.BaseType(value.Type(), reflect.Slice)
slice, err := typing.Base(value.Type(), reflect.Slice)
if err != nil {
return err
}

isPtr := slice.Elem().Kind() == reflect.Ptr
base := types.DeRef(slice.Elem())
base := typing.DeRef(slice.Elem())
scannable := isScannable(base)

if structOnly {
Expand Down
Loading

0 comments on commit 832bfc1

Please sign in to comment.