feat: docs api design

This commit is contained in:
neo-f
2023-03-22 22:45:17 +08:00
commit 084d0de8bc
52 changed files with 3420 additions and 0 deletions

50
pkg/utils/cache.go Normal file
View File

@@ -0,0 +1,50 @@
package utils
import (
"sync"
"time"
)
type Cache[T any] struct {
storage map[string]T
ttl map[string]time.Time
mu sync.RWMutex
}
func NewCache[T any]() *Cache[T] {
return &Cache[T]{
storage: make(map[string]T),
ttl: make(map[string]time.Time),
mu: sync.RWMutex{},
}
}
func (c *Cache[T]) Get(key string, ttl time.Duration, fn func() (T, error)) (T, error) {
c.mu.RLock()
if v, ok := c.storage[key]; ok {
if time.Now().Before(c.ttl[key]) {
c.mu.RUnlock()
return v, nil
}
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if v, ok := c.storage[key]; ok {
if time.Now().Before(c.ttl[key]) {
return v, nil
}
delete(c.storage, key)
delete(c.ttl, key)
}
v, err := fn()
if err != nil {
return v, err
}
c.storage[key] = v
c.ttl[key] = time.Now().Add(ttl)
return v, nil
}

19
pkg/utils/collections.go Normal file
View File

@@ -0,0 +1,19 @@
package utils
func Any[T any](items []T, predicate func(T) bool) bool {
for _, item := range items {
if predicate(item) {
return true
}
}
return false
}
func All[T any](items []T, predicate func(T) bool) bool {
for _, item := range items {
if !predicate(item) {
return false
}
}
return true
}

39
pkg/utils/date.go Normal file
View File

@@ -0,0 +1,39 @@
package utils
import (
"fmt"
"time"
"github.com/spf13/cast"
)
var parseLayouts = []string{
"2006-01-02 15:04:05",
"2006-1-2 15:04:05",
"2006-1-2 15:4:5",
"2006/01/02 15:04:05",
"2006/1/2 15:04:05",
"2006/1/2 15:4:5",
"2006年01月02日 15时04分05秒",
"2006年1月2日 15时04分05秒",
"2006年1月2日 15时4分5秒",
"2006年01月02日 15时04分",
"2006年1月2日 15时04分",
"2006年1月2日 15时4分",
"2006年01月02日 15:04:05",
"2006年1月2日 15:04:05",
"2006年1月2日 15:4:5",
}
func ToDateE(v interface{}) (time.Time, error) {
beijing, _ := time.LoadLocation("Asia/Shanghai")
for _, layout := range parseLayouts {
if t, err := time.ParseInLocation(layout, cast.ToString(v), beijing); err == nil {
return t, nil
}
}
if t, err := cast.ToTimeE(v); err == nil {
return t, nil
}
return time.Time{}, fmt.Errorf("不能被识别的时间%s", cast.ToString(v))
}

26
pkg/utils/dbg.go Normal file
View File

@@ -0,0 +1,26 @@
package utils
import (
"encoding/json"
"fmt"
)
func DBG(v interface{}, msg ...string) {
bv, err := json.MarshalIndent(v, "", " ")
if len(msg) == 0 {
fmt.Println("===========DBG===========")
} else {
fmt.Printf("===========%s===========\n", msg[0])
}
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(bv))
}
fmt.Println("=========================")
}
func DbgE(v interface{}, err error) {
bv, _ := json.Marshal(v)
fmt.Println(string(bv))
}

11
pkg/utils/hash.go Normal file
View File

@@ -0,0 +1,11 @@
package utils
import (
"crypto/md5"
"encoding/hex"
)
func MD5(d []byte) string {
hash := md5.Sum(d)
return hex.EncodeToString(hash[:])
}

14
pkg/utils/ptr.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import "reflect"
func Ptr[T any](v T) *T {
return &v
}
func Unptr[T any](v *T) T {
if v == nil {
return reflect.Zero(reflect.TypeOf(v).Elem()).Interface().(T)
}
return *v
}