/
util.go
93 lines (79 loc) · 2.12 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package yiigo
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
"strings"
"time"
"github.com/hashicorp/go-version"
)
// Nonce 生成随机串(size应为偶数)
func Nonce(size uint8) string {
nonce := make([]byte, size/2)
io.ReadFull(rand.Reader, nonce)
return hex.EncodeToString(nonce)
}
// MarshalNoEscapeHTML 不带HTML转义的JSON序列化
func MarshalNoEscapeHTML(v any) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(v); err != nil {
return nil, err
}
// 去掉 go std 给末尾加的 '\n'
// @see https://github.com/golang/go/issues/7767
b := buf.Bytes()
if l := len(b); l != 0 && b[l-1] == '\n' {
b = b[:l-1]
}
return b, nil
}
// VersionCompare 语义化的版本比较,支持:>, >=, =, !=, <, <=, | (or), & (and).
// 参数 `rangeVer` 示例:1.0.0, =1.0.0, >2.0.0, >=1.0.0&<2.0.0, <2.0.0|>3.0.0, !=4.0.4
func VersionCompare(rangeVer, curVer string) (bool, error) {
semVer, err := version.NewVersion(curVer)
if err != nil {
return false, err
}
orVers := strings.Split(rangeVer, "|")
for _, ver := range orVers {
andVers := strings.Split(ver, "&")
constraints, err := version.NewConstraint(strings.Join(andVers, ","))
if err != nil {
return false, err
}
if constraints.Check(semVer) {
return true, nil
}
}
return false, nil
}
// DetachContext returns a copy of parent that is not canceled when parent is canceled.
// The returned context returns no Deadline or Err, and its Done channel is nil.
// Calling [Cause] on the returned context returns nil.
// Use `context.WithoutCancel` if go version >= 1.21.0
func DetachContext(ctx context.Context) context.Context {
if ctx == nil {
panic("cannot create context from nil parent")
}
return detachedContext{ctx}
}
type detachedContext struct {
ctx context.Context
}
func (detachedContext) Deadline() (deadline time.Time, ok bool) {
return
}
func (detachedContext) Done() <-chan struct{} {
return nil
}
func (detachedContext) Err() error {
return nil
}
func (c detachedContext) Value(key any) any {
return c.ctx.Value(key)
}