Skip to content

Commit 832bfc1

Browse files
committed
feat(core): refactor
1 parent 55d750d commit 832bfc1

File tree

16 files changed

+197
-133
lines changed

16 files changed

+197
-133
lines changed

.golangci.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ linters-settings:
3939
gocritic:
4040
disabled-checks:
4141
- unnamedResult
42-
- hugeParam
4342
- whyNoLint
4443
enabled-tags:
4544
- performance
@@ -53,7 +52,7 @@ linters:
5352
enable-all: false
5453
disable-all: true
5554
enable:
56-
# - lll
55+
- lll
5756
- misspell
5857
- goconst
5958
- gochecknoinits
@@ -68,14 +67,14 @@ linters:
6867
- gofumpt
6968
- gocritic
7069
- vet
71-
# - revive
70+
- revive
7271
- bodyclose
7372
- deadcode
7473
- errcheck
7574
- gosec
7675
- structcheck
7776
- unconvert
78-
# - dupl
77+
- dupl
7978
- varcheck
8079
- unparam
8180
- staticcheck

agtime/time.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,45 +120,52 @@ func (t *NullableDate) UnmarshalJSON(data []byte) error {
120120
return nil
121121
}
122122

123+
// TruncateMonth returns a nullable date with truncated month
123124
func (t *NullableDate) TruncateMonth() NullableDate {
124125
return NewNullDate(TruncateMonth(t.Time))
125126
}
126127

128+
// After checks if nullable date `t` is after time `a`
127129
func (t *NullableDate) After(a time.Time) bool {
128130
if t.Valid {
129131
return t.Time.After(a)
130132
}
131133
return false
132134
}
133135

136+
// Before checks if nullable date `t` is before time `a`
134137
func (t *NullableDate) Before(a time.Time) bool {
135138
if t.Valid {
136139
return t.Time.Before(a)
137140
}
138141
return false
139142
}
140143

144+
// Equal checks if nullable date `t` is equal time `a`
141145
func (t *NullableDate) Equal(a time.Time) bool {
142146
if t.Valid {
143147
return t.Time.Equal(a)
144148
}
145149
return false
146150
}
147151

152+
// AftEq checks if nullable date `t` is after or equal the time `a`
148153
func (t *NullableDate) AftEq(a time.Time) bool {
149154
if t.Valid {
150155
return t.After(a) || t.Equal(a)
151156
}
152157
return false
153158
}
154159

160+
// BfEq checks if nullable date `t` is before or equal the time `a`
155161
func (t *NullableDate) BfEq(a time.Time) bool {
156162
if t.Valid {
157163
return t.Before(a) || t.Equal(a)
158164
}
159165
return false
160166
}
161167

168+
// Between checks if nullable date `t` is between the times `a` and `b`
162169
func (t *NullableDate) Between(a, b time.Time) bool {
163170
return t.AftEq(a) && t.BfEq(b)
164171
}

cache/redis/redis.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package redis
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/go-redis/redis/v8"
8+
9+
"github.com/agflow/tools/log"
10+
)
11+
12+
// Client is a wrapper of a redis client
13+
type Client struct {
14+
Redis *redis.Client
15+
}
16+
17+
// NewClient returns a verified redis client
18+
func NewClient(address, password string) *redis.Client {
19+
rdb := redis.NewClient(&redis.Options{
20+
Addr: address,
21+
Password: password,
22+
DB: 0, // use default DB
23+
})
24+
if rdb == nil {
25+
log.Warnf("failed to initialize redis client")
26+
return rdb
27+
}
28+
29+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
30+
defer cancel()
31+
32+
if err := rdb.Ping(ctx).Err(); err != nil {
33+
log.Warnf("can't ping redis, %v", err)
34+
}
35+
return rdb
36+
}
37+
38+
// New returns a new redis client
39+
func New(address, password string) *Client {
40+
redisClient := NewClient(address, password)
41+
return &Client{Redis: redisClient}
42+
}
43+
44+
// Get gets the value stored on `key`
45+
func (c *Client) Get(ctx context.Context, key string) ([]byte, error) {
46+
return c.Redis.Get(ctx, key).Bytes()
47+
}
48+
49+
// Set sets `value` on `key` with a `timeout``
50+
func (c *Client) Set(
51+
ctx context.Context,
52+
key string,
53+
value interface{},
54+
timeout time.Duration,
55+
) error {
56+
return c.Redis.Set(ctx, key, value, timeout).Err()
57+
}

cache/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
// Service declares the interface of a cache service
9+
type Service interface {
10+
Get(context.Context, string) ([]byte, error)
11+
Set(context.Context, string, interface{}, time.Duration) error
12+
}

group_by.go renamed to groupby/group_by.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
1-
package main
1+
package groupby
22

33
import (
4-
"crypto/sha512"
5-
"encoding/json"
64
"errors"
7-
"fmt"
85
"reflect"
96
"sync"
107
"time"
118

129
"github.com/agflow/tools/log"
13-
"github.com/agflow/tools/types"
10+
"github.com/agflow/tools/security"
11+
"github.com/agflow/tools/typing"
1412
)
1513

16-
func Hash(vs interface{}) (string, error) {
17-
h := sha512.New()
18-
r, err := json.Marshal(vs)
19-
if err != nil {
20-
return "", err
21-
}
22-
h.Write(r)
23-
return fmt.Sprintf("%x", h.Sum(nil)), nil
24-
}
25-
2614
func getGroupHash(v reflect.Value, cols []string) (string, error) {
2715
grouped := make([]interface{}, len(cols))
2816
for j := range cols {
2917
grouped[j] = v.FieldByName(cols[j]).Interface()
3018
}
31-
return Hash(grouped)
19+
return security.Hash(grouped)
3220
}
3321

22+
// AggrFunc is an aggregation function
3423
type AggrFunc func([]interface{}) interface{}
3524

3625
var wg sync.WaitGroup //nolint: gochecknoglobals
3726

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

48-
sliceType, err := types.BaseType(destVal.Type(), reflect.Slice)
37+
sliceType, err := typing.Base(destVal.Type(), reflect.Slice)
4938
if err != nil {
5039
return err
5140
}
5241

53-
baseType := types.DeRef(sliceType.Elem())
42+
baseType := typing.DeRef(sliceType.Elem())
5443

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

97-
func processFun(v chan interface{}, funcs map[string]AggrFunc, dest reflect.Value, finish chan bool) {
86+
func processFun(
87+
v chan interface{},
88+
funcs map[string]AggrFunc,
89+
dest reflect.Value,
90+
finish chan bool,
91+
) {
9892
defer wg.Done()
9993
var isFinished bool
10094
grouped := make([]interface{}, 0)
@@ -114,10 +108,11 @@ func processFun(v chan interface{}, funcs map[string]AggrFunc, dest reflect.Valu
114108
}
115109
}
116110

111+
// FoldFunc is a fold function
117112
type FoldFunc func(interface{}, interface{}) interface{}
118113

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

129-
sliceType, err := types.BaseType(destVal.Type(), reflect.Slice)
124+
sliceType, err := typing.Base(destVal.Type(), reflect.Slice)
130125
if err != nil {
131126
return err
132127
}
133128

134-
baseType := types.DeRef(sliceType.Elem())
129+
baseType := typing.DeRef(sliceType.Elem())
135130

136131
s := reflect.ValueOf(on)
137132
for i := 0; i < s.Len(); i++ {

handlers/handler.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

log/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const (
1616
timeFormat = "2006/01/02 15:04:05"
1717
)
1818

19-
//nolint: gochecknoinits
19+
// nolint: gochecknoinits
2020
func init() {
2121
log.SetFlags(0)
2222
}

redis/redis.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

security/hash.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package security
2+
3+
import (
4+
"crypto/sha512"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
// Hash hashes `vs`
10+
func Hash(vs interface{}) (string, error) {
11+
h := sha512.New()
12+
r, err := json.Marshal(vs)
13+
if err != nil {
14+
return "", err
15+
}
16+
h.Write(r)
17+
return fmt.Sprintf("%x", h.Sum(nil)), nil
18+
}

sql/pgu.go renamed to sql/db/pgu.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sql
1+
package db
22

33
import (
44
"context"
@@ -8,7 +8,7 @@ import (
88
"reflect"
99

1010
"github.com/agflow/tools/log"
11-
"github.com/agflow/tools/types"
11+
"github.com/agflow/tools/typing"
1212
)
1313

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

115-
slice, err := types.BaseType(value.Type(), reflect.Slice)
115+
slice, err := typing.Base(value.Type(), reflect.Slice)
116116
if err != nil {
117117
return err
118118
}
119119

120120
isPtr := slice.Elem().Kind() == reflect.Ptr
121-
base := types.DeRef(slice.Elem())
121+
base := typing.DeRef(slice.Elem())
122122
scannable := isScannable(base)
123123

124124
if structOnly {

0 commit comments

Comments
 (0)