|
| 1 | +// Package gutils |
| 2 | +// Author: hyphen |
| 3 | +// Copyright 2023 hyphen. All rights reserved. |
| 4 | +// Create-time: 2023/12/4 |
| 5 | +package gutils |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "sync" |
| 15 | + |
| 16 | + "github.com/hyphennn/glamda/internal" |
| 17 | +) |
| 18 | + |
| 19 | +func TernaryForm[T any](cond bool, tureVal, falseVal T) T { |
| 20 | + if cond { |
| 21 | + return tureVal |
| 22 | + } |
| 23 | + return falseVal |
| 24 | +} |
| 25 | + |
| 26 | +type Pair[F, S any] struct { |
| 27 | + First F |
| 28 | + Second S |
| 29 | +} |
| 30 | + |
| 31 | +func MakePair[F, S any](f F, s S) *Pair[F, S] { |
| 32 | + return &Pair[F, S]{First: f, Second: s} |
| 33 | +} |
| 34 | + |
| 35 | +func (p *Pair[F, S]) Split() (F, S) { |
| 36 | + return p.First, p.Second |
| 37 | +} |
| 38 | + |
| 39 | +func FastAssert[T any](v any) T { |
| 40 | + t, ok := v.(T) |
| 41 | + if !ok { |
| 42 | + return internal.Zero[T]() |
| 43 | + } |
| 44 | + return t |
| 45 | +} |
| 46 | + |
| 47 | +func MustDo[K, V any](key K, fc func(K) (V, error)) V { |
| 48 | + return MustEasyDo(func() (V, error) { |
| 49 | + return fc(key) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func MustDoCtx[K, V any](ctx context.Context, key K, fc func(context.Context, K) (V, error)) V { |
| 54 | + return MustEasyDo(func() (V, error) { |
| 55 | + return fc(ctx, key) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func MustEasyDo[V any](fc func() (V, error)) V { |
| 60 | + v, err := fc() |
| 61 | + if err != nil { |
| 62 | + return internal.Zero[V]() |
| 63 | + } |
| 64 | + return v |
| 65 | +} |
| 66 | + |
| 67 | +type ch[T any] chan T |
| 68 | + |
| 69 | +type SafeChan[T any] struct { |
| 70 | + ch[T] |
| 71 | + once sync.Once |
| 72 | +} |
| 73 | + |
| 74 | +func NewSafeChan[T any](size ...int) *SafeChan[T] { |
| 75 | + if len(size) == 0 { |
| 76 | + return &SafeChan[T]{make(chan T), sync.Once{}} |
| 77 | + } |
| 78 | + return &SafeChan[T]{make(chan T, size[0]), sync.Once{}} |
| 79 | +} |
| 80 | + |
| 81 | +func (s *SafeChan[T]) Listen() (t T) { |
| 82 | + t = <-s.ch |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | +func (s *SafeChan[T]) Send(t T) { |
| 87 | + s.ch <- t |
| 88 | +} |
| 89 | + |
| 90 | +func (s *SafeChan[T]) Close() { |
| 91 | + s.once.Do(func() { |
| 92 | + close(s.ch) |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +func Paging[T any](arr []T, offset, limit int) []T { |
| 97 | + return arr[TernaryForm((offset)*limit <= len(arr), (offset)*limit, len(arr)):TernaryForm((offset+1)*limit <= len(arr), (offset+1)*limit, len(arr))] |
| 98 | +} |
| 99 | + |
| 100 | +var client = &http.Client{} |
| 101 | + |
| 102 | +func AccessResp(ctx context.Context, url string, method string, header map[string]string, param map[string]string, |
| 103 | + body any, setAuthorization func(*http.Request)) (*http.Response, error) { |
| 104 | + return access(ctx, url, method, header, param, body, setAuthorization) |
| 105 | +} |
| 106 | + |
| 107 | +func Access[T any](ctx context.Context, url string, method string, header map[string]string, param map[string]string, |
| 108 | + body any, setAuthorization func(*http.Request), isSuccess func(response *http.Response) bool) (T, error) { |
| 109 | + var ret T |
| 110 | + resp, err := access(ctx, url, method, header, param, body, setAuthorization) |
| 111 | + if err != nil { |
| 112 | + return ret, fmt.Errorf("access %s failed: %w", url, err) |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + respBody, err := ioutil.ReadAll(resp.Body) |
| 116 | + if err != nil { |
| 117 | + return ret, fmt.Errorf("read resp body failed: %w", err) |
| 118 | + } |
| 119 | + if isSuccess != nil && !isSuccess(resp) { |
| 120 | + return ret, fmt.Errorf("is success return false: %s", string(respBody)) |
| 121 | + } |
| 122 | + err = json.Unmarshal(respBody, &ret) |
| 123 | + if err != nil { |
| 124 | + return ret, fmt.Errorf("unmarshal resp body failed: %w", err) |
| 125 | + } |
| 126 | + return ret, nil |
| 127 | +} |
| 128 | + |
| 129 | +func access(ctx context.Context, url string, method string, header map[string]string, param map[string]string, |
| 130 | + body any, setAuthorization func(*http.Request)) (*http.Response, error) { |
| 131 | + bodyJSON, err := json.Marshal(body) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + bodyReader := bytes.NewReader(bodyJSON) |
| 136 | + req, err := http.NewRequestWithContext(ctx, method, url, bodyReader) |
| 137 | + if setAuthorization != nil { |
| 138 | + setAuthorization(req) |
| 139 | + } |
| 140 | + q := req.URL.Query() |
| 141 | + for k, v := range param { |
| 142 | + q.Add(k, v) |
| 143 | + } |
| 144 | + req.URL.RawQuery = q.Encode() |
| 145 | + for k, v := range header { |
| 146 | + req.Header.Add(k, v) |
| 147 | + } |
| 148 | + resp, err := client.Do(req) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("access %s failed: %w", url, err) |
| 151 | + } |
| 152 | + return resp, nil |
| 153 | +} |
0 commit comments