diff --git a/go.mod b/go.mod index 3894fd9..77a13e8 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( entgo.io/ent v0.12.3 github.com/PuerkitoBio/goquery v1.8.1 github.com/alexliesenfeld/health v0.7.0 - github.com/davecgh/go-spew v1.1.1 github.com/disgoorg/disgo v0.16.4 github.com/disgoorg/snowflake/v2 v2.0.1 github.com/gin-gonic/gin v1.9.1 @@ -20,15 +19,16 @@ require ( github.com/maxbrunsfeld/counterfeiter/v6 v6.6.1 github.com/nekomeowww/fo v1.1.1 github.com/nekomeowww/go-pinecone v0.1.0 - github.com/nekomeowww/timecapsule/v2 v2.1.1 + github.com/nekomeowww/timecapsule/v2 v2.1.3 + github.com/nekomeowww/xo v1.1.1 github.com/pkoukk/tiktoken-go v0.1.1 - github.com/redis/rueidis v1.0.5 + github.com/redis/rueidis v1.0.8 github.com/samber/lo v1.38.1 github.com/sashabaranov/go-openai v1.11.1 - github.com/sirupsen/logrus v1.9.2 + github.com/sirupsen/logrus v1.9.3 github.com/slack-go/slack v0.12.2 github.com/sourcegraph/conc v0.3.0 - github.com/stretchr/testify v1.8.3 + github.com/stretchr/testify v1.8.4 go.uber.org/fx v1.19.3 go.uber.org/multierr v1.11.0 go.uber.org/ratelimit v0.2.0 @@ -44,6 +44,7 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/disgoorg/json v1.0.0 // indirect github.com/disgoorg/log v1.2.0 // indirect @@ -81,9 +82,10 @@ require ( github.com/quic-go/qtls-go1-19 v0.3.2 // indirect github.com/quic-go/qtls-go1-20 v0.2.2 // indirect github.com/quic-go/quic-go v0.32.0 // indirect - github.com/redis/go-redis/v9 v9.0.4 // indirect + github.com/redis/go-redis/v9 v9.0.5 // indirect github.com/samber/mo v1.8.0 // indirect github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect + github.com/shopspring/decimal v1.3.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect @@ -92,12 +94,12 @@ require ( go.uber.org/dig v1.17.0 // indirect go.uber.org/goleak v1.2.1 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.9.0 // indirect + golang.org/x/crypto v0.10.0 // indirect golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/mod v0.10.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/net v0.11.0 // indirect + golang.org/x/sys v0.9.0 // indirect + golang.org/x/text v0.10.0 // indirect golang.org/x/tools v0.9.1 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index e06ff4e..ecd86fe 100644 --- a/go.sum +++ b/go.sum @@ -15,7 +15,6 @@ github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxB github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= -github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= @@ -112,7 +111,6 @@ github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= -github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= @@ -135,8 +133,10 @@ github.com/nekomeowww/fo v1.1.1 h1:948AqPsiF2SAUmOlMQhyXzgq89HAtuAZx2g0pUkxjCA= github.com/nekomeowww/fo v1.1.1/go.mod h1:KKW/q6aFa1k/IFL+nS9dDxsgZPfn4v4ADH7g8pb4nik= github.com/nekomeowww/go-pinecone v0.1.0 h1:byYmyHQJ4velNUeECvNuCN6KbXGuyVjSFlvO9VV2zj8= github.com/nekomeowww/go-pinecone v0.1.0/go.mod h1:p8I6F7G7fSrfaa9k3HpsWNxf2y+W1wqn8tw0jsV9e1s= -github.com/nekomeowww/timecapsule/v2 v2.1.1 h1:1UeBgLhiWstn1pv7w9Bo+43AbhzYYrpyoVhtETtw0uI= -github.com/nekomeowww/timecapsule/v2 v2.1.1/go.mod h1:4vevpnLBU2hsjHU7qKPRvD3NUQGjdAYb4y8dbL47Baw= +github.com/nekomeowww/timecapsule/v2 v2.1.3 h1:KIg6jZSg4RkQG9qV2k5TMq9LUzM6cO3MiXvTyLnqZ9U= +github.com/nekomeowww/timecapsule/v2 v2.1.3/go.mod h1:/DD0z43GI6mG6WmRxKwRPJNaYsy696wabZyyjKcL2/4= +github.com/nekomeowww/xo v1.1.1 h1:IHcpIA2qEGe1Qx4rb2ZrX7xBfSKMbnuaLPA/5RXJs8M= +github.com/nekomeowww/xo v1.1.1/go.mod h1:TgJ7VZ9UEBr3H6btyfyLIuZz+JEdd+3WWAJijPNYoHs= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE= @@ -160,10 +160,10 @@ github.com/quic-go/qtls-go1-20 v0.2.2 h1:WLOPx6OY/hxtTxKV1Zrq20FtXtDEkeY00CGQm8G github.com/quic-go/qtls-go1-20 v0.2.2/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM= github.com/quic-go/quic-go v0.32.0 h1:lY02md31s1JgPiiyfqJijpu/UX/Iun304FI3yUqX7tA= github.com/quic-go/quic-go v0.32.0/go.mod h1:/fCsKANhQIeD5l76c2JFU+07gVE3KaA0FP+0zMWwfwo= -github.com/redis/go-redis/v9 v9.0.4 h1:FC82T+CHJ/Q/PdyLW++GeCO+Ol59Y4T7R4jbgjvktgc= -github.com/redis/go-redis/v9 v9.0.4/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= -github.com/redis/rueidis v1.0.5 h1:VQ8m2AfkNskm5nK9lswTvTpL6/clF+UY1KgIcAZcBx4= -github.com/redis/rueidis v1.0.5/go.mod h1:yxbpgX+VYNxCvdE0KEQXDeUFcF2hB2Oz/TJiaqFxoEU= +github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= +github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= +github.com/redis/rueidis v1.0.8 h1:ZTQw0kIfvXlQexS4xCBXSiZUnloNlVhAMc3Kbfotsjo= +github.com/redis/rueidis v1.0.8/go.mod h1:+1zDH4a8XhwIbCSlIhVGIu6Xib0ZMDoBM0qGhHXc1ew= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= @@ -171,14 +171,14 @@ github.com/samber/mo v1.8.0 h1:vYjHTfg14JF9tD2NLhpoUsRi9bjyRoYwa4+do0nvbVw= github.com/samber/mo v1.8.0/go.mod h1:BfkrCPuYzVG3ZljnZB783WIJIGk1mcZr9c9CPf8tAxs= github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE= github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s= -github.com/sashabaranov/go-openai v1.9.4 h1:KanoCEoowAI45jVXlenMCckutSRr39qOmSi9MyPBfZM= -github.com/sashabaranov/go-openai v1.9.4/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sashabaranov/go-openai v1.11.1 h1:QAyLx/MhiVrV4nsGTAZI+JPP/n0nlYZfxltzv9jTClw= github.com/sashabaranov/go-openai v1.11.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= -github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= -github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= +github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ= github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -199,8 +199,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= @@ -211,7 +212,6 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc= github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= -github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= @@ -233,8 +233,8 @@ golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= +golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= +golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -253,8 +253,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= +golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -274,8 +274,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -285,8 +285,9 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= diff --git a/internal/bots/telegram/handlers/recap/recap_command.go b/internal/bots/telegram/handlers/recap/recap_command.go index cb7207a..a732de5 100644 --- a/internal/bots/telegram/handlers/recap/recap_command.go +++ b/internal/bots/telegram/handlers/recap/recap_command.go @@ -10,7 +10,7 @@ import ( "github.com/nekomeowww/insights-bot/pkg/types/bot/handlers/recap" "github.com/nekomeowww/insights-bot/pkg/types/telegram" "github.com/nekomeowww/insights-bot/pkg/types/tgchat" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" "github.com/samber/lo" "go.uber.org/zap" ) @@ -151,7 +151,7 @@ func (h *CommandHandler) handleRecapCommandForPrivateSubscriptionsMode(c *tgbot. return h.handleUserNeverStartedChatOrBlockedErr(c, chatID, chatTitle, newRecapCommandWhenUserBlockedMessage(c.Bot, hashKey)) } else { h.logger.Error("failed to send private message to user", - zap.String("message", utils.SprintJSON(msg)), + zap.String("message", xo.SprintJSON(msg)), zap.Int64("chat_id", c.Update.Message.From.ID), zap.Error(err), ) diff --git a/internal/bots/telegram/handlers/recap/subscribe_recap.go b/internal/bots/telegram/handlers/recap/subscribe_recap.go index 6ac2fe2..e3bf3d1 100644 --- a/internal/bots/telegram/handlers/recap/subscribe_recap.go +++ b/internal/bots/telegram/handlers/recap/subscribe_recap.go @@ -7,7 +7,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/nekomeowww/insights-bot/pkg/bots/tgbot" "github.com/nekomeowww/insights-bot/pkg/types/telegram" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" "github.com/samber/lo" "go.uber.org/zap" ) @@ -83,7 +83,7 @@ func (h *CommandHandler) handleSubscribeRecapCommand(c *tgbot.Context) (tgbot.Re return h.handleUserNeverStartedChatOrBlockedErr(c, chatID, chatTitle, newSubscribeRecapCommandWhenUserBlockedMessage(c.Bot, hashKey)) } else { h.logger.Error("failed to send private message to user", - zap.String("message", utils.SprintJSON(msg)), + zap.String("message", xo.SprintJSON(msg)), zap.Int64("chat_id", c.Update.Message.From.ID), zap.Error(err), ) @@ -165,7 +165,7 @@ func (h *CommandHandler) handleUnsubscribeRecapCommand(c *tgbot.Context) (tgbot. c.Bot.MayRequest(tgbotapi.NewDeleteMessage(chatID, c.Update.Message.MessageID)) } else { h.logger.Error("failed to send private message to user", - zap.String("message", utils.SprintJSON(msg)), + zap.String("message", xo.SprintJSON(msg)), zap.Int64("chat_id", c.Update.Message.From.ID), zap.Error(err), ) diff --git a/internal/models/chathistories/chat_histories_test.go b/internal/models/chathistories/chat_histories_test.go index daef071..b22634e 100644 --- a/internal/models/chathistories/chat_histories_test.go +++ b/internal/models/chathistories/chat_histories_test.go @@ -19,7 +19,7 @@ import ( "github.com/nekomeowww/insights-bot/internal/lib" "github.com/nekomeowww/insights-bot/internal/thirdparty/openai/openaimock" "github.com/nekomeowww/insights-bot/pkg/tutils" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" ) var model *Model @@ -102,17 +102,17 @@ func TestSaveOneTelegramChatHistory(t *testing.T) { require := require.New(t) message := &tgbotapi.Message{ - MessageID: int(utils.RandomInt64()), + MessageID: int(xo.RandomInt64()), From: &tgbotapi.User{ - ID: utils.RandomInt64(), - FirstName: utils.RandomHashString(5), - UserName: utils.RandomHashString(10), + ID: xo.RandomInt64(), + FirstName: xo.RandomHashString(5), + UserName: xo.RandomHashString(10), }, Chat: &tgbotapi.Chat{ - ID: utils.RandomInt64(), + ID: xo.RandomInt64(), }, Date: int(time.Now().Unix()), - Text: utils.RandomHashString(10), + Text: xo.RandomHashString(10), } err := model.SaveOneTelegramChatHistory(message) require.NoError(err) @@ -141,22 +141,22 @@ func TestUpdateOneTelegramChatHistory(t *testing.T) { require := require.New(t) message := &tgbotapi.Message{ - MessageID: int(utils.RandomInt64()), + MessageID: int(xo.RandomInt64()), From: &tgbotapi.User{ - ID: utils.RandomInt64(), - FirstName: utils.RandomHashString(5), - UserName: utils.RandomHashString(10), + ID: xo.RandomInt64(), + FirstName: xo.RandomHashString(5), + UserName: xo.RandomHashString(10), }, Chat: &tgbotapi.Chat{ - ID: utils.RandomInt64(), + ID: xo.RandomInt64(), }, Date: int(time.Now().Unix()), - Text: utils.RandomHashString(10), + Text: xo.RandomHashString(10), } err := model.SaveOneTelegramChatHistory(message) require.NoError(err) - message.Text = utils.RandomHashString(10) + message.Text = xo.RandomHashString(10) err = model.UpdateOneTelegramChatHistory(message) require.NoError(err) @@ -178,42 +178,42 @@ func TestFindLastOneHourChatHistories(t *testing.T) { assert := assert.New(t) require := require.New(t) - chatID := utils.RandomInt64() + chatID := xo.RandomInt64() message1 := &tgbotapi.Message{ MessageID: 1, From: &tgbotapi.User{ - ID: utils.RandomInt64(), - FirstName: utils.RandomHashString(5), - UserName: utils.RandomHashString(10), + ID: xo.RandomInt64(), + FirstName: xo.RandomHashString(5), + UserName: xo.RandomHashString(10), }, Chat: &tgbotapi.Chat{ID: chatID}, Date: int(time.Now().Unix()), - Text: utils.RandomHashString(10), + Text: xo.RandomHashString(10), } message2 := &tgbotapi.Message{ MessageID: 2, From: &tgbotapi.User{ - ID: utils.RandomInt64(), - FirstName: utils.RandomHashString(5), - UserName: utils.RandomHashString(10), + ID: xo.RandomInt64(), + FirstName: xo.RandomHashString(5), + UserName: xo.RandomHashString(10), }, Chat: &tgbotapi.Chat{ID: chatID}, Date: int(time.Now().Unix()), - Text: utils.RandomHashString(10), + Text: xo.RandomHashString(10), } message3 := &tgbotapi.Message{ MessageID: 3, From: &tgbotapi.User{ - ID: utils.RandomInt64(), - FirstName: utils.RandomHashString(5), - UserName: utils.RandomHashString(10), + ID: xo.RandomInt64(), + FirstName: xo.RandomHashString(5), + UserName: xo.RandomHashString(10), }, Chat: &tgbotapi.Chat{ID: chatID}, Date: int(time.Now().Unix()), - Text: utils.RandomHashString(10), + Text: xo.RandomHashString(10), } err := model.SaveOneTelegramChatHistory(message1) diff --git a/internal/models/chathistories/private_forwarded_test.go b/internal/models/chathistories/private_forwarded_test.go index f7695f3..af867b1 100644 --- a/internal/models/chathistories/private_forwarded_test.go +++ b/internal/models/chathistories/private_forwarded_test.go @@ -5,14 +5,14 @@ import ( "testing" "github.com/nekomeowww/insights-bot/pkg/types/redis" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" "github.com/redis/rueidis" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestHasOngoingRecapForwardedFromPrivateMessages(t *testing.T) { - userID := utils.RandomInt64() + userID := xo.RandomInt64() // assign batch for user ID setCmd := model.redis.B(). @@ -31,7 +31,7 @@ func TestHasOngoingRecapForwardedFromPrivateMessages(t *testing.T) { } func TestEnabledRecapForwardedFromPrivateMessages(t *testing.T) { - userID := utils.RandomInt64() + userID := xo.RandomInt64() err := model.EnabledRecapForwardedFromPrivateMessages(userID) require.NoError(t, err) @@ -50,7 +50,7 @@ func TestEnabledRecapForwardedFromPrivateMessages(t *testing.T) { } func TestDisableRecapForwardedFromPrivateMessages(t *testing.T) { - userID := utils.RandomInt64() + userID := xo.RandomInt64() err := model.EnabledRecapForwardedFromPrivateMessages(userID) require.NoError(t, err) diff --git a/internal/models/tgchats/feature_flags_test.go b/internal/models/tgchats/feature_flags_test.go index e2db28b..b06ac8b 100644 --- a/internal/models/tgchats/feature_flags_test.go +++ b/internal/models/tgchats/feature_flags_test.go @@ -7,20 +7,20 @@ import ( "github.com/nekomeowww/insights-bot/ent" "github.com/nekomeowww/insights-bot/ent/telegramchatfeatureflags" "github.com/nekomeowww/insights-bot/pkg/types/telegram" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestChatHistoriesRecap(t *testing.T) { - chatID := utils.RandomInt64() + chatID := xo.RandomInt64() t.Run("Enable", func(t *testing.T) { assert := assert.New(t) require := require.New(t) - err := model.EnableChatHistoriesRecap(chatID, telegram.ChatTypeGroup, utils.RandomHashString(6)) + err := model.EnableChatHistoriesRecap(chatID, telegram.ChatTypeGroup, xo.RandomHashString(6)) require.NoError(err) featureFlag, err := model.ent.TelegramChatFeatureFlags. @@ -43,7 +43,7 @@ func TestChatHistoriesRecap(t *testing.T) { assert := assert.New(t) require := require.New(t) - err := model.DisableChatHistoriesRecap(chatID, telegram.ChatTypeGroup, utils.RandomHashString(6)) + err := model.DisableChatHistoriesRecap(chatID, telegram.ChatTypeGroup, xo.RandomHashString(6)) require.NoError(err) featureFlag, err := model.ent.TelegramChatFeatureFlags. @@ -66,12 +66,12 @@ func TestListChatHistoriesRecapEnabledChats(t *testing.T) { assert := assert.New(t) require := require.New(t) - chatID1 := utils.RandomInt64() - chatTitle1 := utils.RandomHashString(6) - chatID2 := utils.RandomInt64() - chatTitle2 := utils.RandomHashString(6) - chatID3 := utils.RandomInt64() - chatTitle3 := utils.RandomHashString(6) + chatID1 := xo.RandomInt64() + chatTitle1 := xo.RandomHashString(6) + chatID2 := xo.RandomInt64() + chatTitle2 := xo.RandomHashString(6) + chatID3 := xo.RandomInt64() + chatTitle3 := xo.RandomHashString(6) err := model.EnableChatHistoriesRecap(chatID1, telegram.ChatTypeGroup, chatTitle1) require.NoError(err) diff --git a/pkg/bots/tgbot/tgbot.go b/pkg/bots/tgbot/tgbot.go index fd102f4..453ad01 100644 --- a/pkg/bots/tgbot/tgbot.go +++ b/pkg/bots/tgbot/tgbot.go @@ -16,6 +16,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/redis/rueidis" "github.com/samber/lo" + "github.com/sourcegraph/conc/panics" "go.uber.org/zap" "github.com/nekomeowww/fo" @@ -23,7 +24,8 @@ import ( "github.com/nekomeowww/insights-bot/pkg/logger" "github.com/nekomeowww/insights-bot/pkg/types/redis" "github.com/nekomeowww/insights-bot/pkg/types/telegram" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" + "github.com/nekomeowww/xo/exp/channelx" ) type BotServiceOptions struct { @@ -92,9 +94,11 @@ type BotService struct { webhookServer *http.Server webhookUpdateChan chan tgbotapi.Update updateChan tgbotapi.UpdatesChannel - alreadyClose bool - ctxCancel context.CancelFunc webhookStarted bool + + alreadyStopped bool + + puller *channelx.Puller[tgbotapi.Update] } func NewBotService(callOpts ...CallOption) (*BotService, error) { @@ -126,6 +130,14 @@ func NewBotService(callOpts ...CallOption) (*BotService, error) { dispatcher: opts.dispatcher, } + bot.puller = channelx.NewPuller[tgbotapi.Update](). + WithHandler(func(update tgbotapi.Update) { + bot.dispatcher.Dispatch(bot.BotAPI, update, bot.opts.rueidisClient) + }). + WithPanicHandler(func(panicValues *panics.Recovered) { + bot.logger.Error("panic occurred", zap.Any("panic", panicValues)) + }) + // init webhook server and set webhook if bot.opts.webhookURL != "" { parsed, err := url.Parse(bot.opts.webhookURL) @@ -135,6 +147,7 @@ func NewBotService(callOpts ...CallOption) (*BotService, error) { bot.webhookUpdateChan = make(chan tgbotapi.Update, b.Buffer) bot.webhookServer = newWebhookServer(parsed.Path, bot.opts.webhookPort, bot.BotAPI, bot.webhookUpdateChan) + bot.puller = bot.puller.WithNotifyChannel(bot.webhookUpdateChan) err = setWebhook(bot.opts.webhookURL, bot.BotAPI) if err != nil { @@ -144,6 +157,7 @@ func NewBotService(callOpts ...CallOption) (*BotService, error) { u := tgbotapi.NewUpdate(0) u.Timeout = 60 bot.updateChan = b.GetUpdatesChan(u) + bot.puller = bot.puller.WithNotifyChannel(bot.updateChan) } // obtain webhook info @@ -166,20 +180,12 @@ func NewBotService(callOpts ...CallOption) (*BotService, error) { return bot, nil } -func (b *BotService) getUpdateChan() tgbotapi.UpdatesChannel { - if b.opts.webhookURL != "" { - return b.webhookUpdateChan - } - - return b.updateChan -} - func (b *BotService) Stop(ctx context.Context) error { - if b.alreadyClose { + if b.alreadyStopped { return nil } - b.alreadyClose = true + b.alreadyStopped = true if b.opts.webhookURL != "" { closeCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) @@ -194,36 +200,17 @@ func (b *BotService) Stop(ctx context.Context) error { b.StopReceivingUpdates() } - b.ctxCancel() + _ = b.puller.StopPull(ctx) return nil } func (b *BotService) startPullUpdates() { - ctx, cancel := context.WithCancel(context.Background()) - b.ctxCancel = cancel - - for { - if b.alreadyClose { - b.logger.Info("stopped to receiving updates") - - return - } - - select { - case update := <-b.getUpdateChan(): - b.dispatcher.Dispatch(b.BotAPI, update, b.opts.rueidisClient) - case <-ctx.Done(): - b.logger.Info("stopped to receiving updates") - b.webhookStarted = false - - return - } - } + b.puller.StartPull(context.Background()) } func (b *BotService) Start(ctx context.Context) error { - return utils.Invoke0(ctx, func() error { + return fo.Invoke0(ctx, func() error { if b.opts.webhookURL != "" && b.webhookServer != nil { l, err := net.Listen("tcp", b.webhookServer.Addr) if err != nil { @@ -240,7 +227,7 @@ func (b *BotService) Start(ctx context.Context) error { b.logger.Info("Telegram Bot webhook server is listening", zap.String("addr", b.webhookServer.Addr)) } - go b.startPullUpdates() + b.startPullUpdates() b.webhookStarted = true return nil }) @@ -272,7 +259,7 @@ type Bot struct { func (b *Bot) MaySend(chattable tgbotapi.Chattable) *tgbotapi.Message { may := fo.NewMay[tgbotapi.Message]().Use(func(err error, messageArgs ...any) { - b.logger.Error("failed to send message to telegram", zap.String("message", utils.SprintJSON(chattable)), zap.Error(err)) + b.logger.Error("failed to send message to telegram", zap.String("message", xo.SprintJSON(chattable)), zap.Error(err)) }) return lo.ToPtr(may.Invoke(b.Send(chattable))) @@ -280,7 +267,7 @@ func (b *Bot) MaySend(chattable tgbotapi.Chattable) *tgbotapi.Message { func (b *Bot) MayRequest(chattable tgbotapi.Chattable) *tgbotapi.APIResponse { may := fo.NewMay[*tgbotapi.APIResponse]().Use(func(err error, messageArgs ...any) { - b.logger.Error("failed to send request to telegram", zap.String("request", utils.SprintJSON(chattable)), zap.Error(err)) + b.logger.Error("failed to send request to telegram", zap.String("request", xo.SprintJSON(chattable)), zap.Error(err)) }) return may.Invoke(b.Request(chattable)) diff --git a/pkg/bots/tgbot/utils.go b/pkg/bots/tgbot/utils.go index ef59c98..ff9aec6 100644 --- a/pkg/bots/tgbot/utils.go +++ b/pkg/bots/tgbot/utils.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/nekomeowww/insights-bot/pkg/types/telegram" - "github.com/nekomeowww/insights-bot/pkg/utils" + "github.com/nekomeowww/xo" ) var ( @@ -60,13 +60,13 @@ func FullNameFromFirstAndLastName(firstName, lastName string) string { if firstName == "" { return lastName } - if utils.ContainsCJKChar(firstName) && !utils.ContainsCJKChar(lastName) { + if xo.ContainsCJKChar(firstName) && !xo.ContainsCJKChar(lastName) { return firstName + " " + lastName } - if !utils.ContainsCJKChar(firstName) && utils.ContainsCJKChar(lastName) { + if !xo.ContainsCJKChar(firstName) && xo.ContainsCJKChar(lastName) { return lastName + " " + firstName } - if utils.ContainsCJKChar(firstName) && utils.ContainsCJKChar(lastName) { + if xo.ContainsCJKChar(firstName) && xo.ContainsCJKChar(lastName) { return lastName + " " + firstName } diff --git a/pkg/utils/concurrent.go b/pkg/utils/concurrent.go deleted file mode 100644 index 076f1ff..0000000 --- a/pkg/utils/concurrent.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "context" - "sync" -) - -func Invoke0(ctx context.Context, funcToBeRan func() error) error { - var err error - resChan := make(chan struct{}, 1) - - go func() { - err = funcToBeRan() - resChan <- struct{}{} - }() - - var wg sync.WaitGroup - - wg.Add(1) - - go func() { - select { - case <-ctx.Done(): - err = ctx.Err() - case <-resChan: - } - - wg.Done() - }() - wg.Wait() - - return err -} diff --git a/pkg/utils/debug.go b/pkg/utils/debug.go deleted file mode 100644 index d449c41..0000000 --- a/pkg/utils/debug.go +++ /dev/null @@ -1,40 +0,0 @@ -package utils - -import ( - "encoding/json" - "fmt" - "strings" - - "github.com/davecgh/go-spew/spew" -) - -// Print 格式化输出所有传入的值的字段、值、类型、大小。 -func Print(inputs ...interface{}) { - fmt.Println(Sprint(inputs)) -} - -// Sprint 格式化输出所有传入的值的字段、值、类型、大小,并返回字符串。 -// -// NOTICE: 包含换行符。 -func Sprint(inputs ...interface{}) string { - return spew.Sdump(inputs) -} - -// PrintJSON 格式化输出 JSON 格式。 -func PrintJSON(inputs ...interface{}) { - fmt.Println(SprintJSON(inputs)) -} - -// SprintJSON 格式化输出 JSON 格式并返回字符串。 -// -// NOTICE: 包含换行符。 -func SprintJSON(inputs ...interface{}) string { - strSlice := make([]string, 0) - - for _, v := range inputs { - b, _ := json.MarshalIndent(v, "", " ") - strSlice = append(strSlice, string(b)) - } - - return strings.Join(strSlice, "\n") -} diff --git a/pkg/utils/debug_test.go b/pkg/utils/debug_test.go deleted file mode 100644 index b139df7..0000000 --- a/pkg/utils/debug_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package utils - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestPrintAndPrintJSON(t *testing.T) { - type testEmbedded struct { - C int - D []string - } - - type testStruct struct { - A int - B string - Embedded testEmbedded - } - - t.Run("Print", func(t *testing.T) { - assert := assert.New(t) - - assert.NotPanics(func() { - Print(nil) - }) - assert.NotPanics(func() { - Print(testStruct{}) - }) - }) - - t.Run("Sprint", func(t *testing.T) { - assert := assert.New(t) - - assert.NotPanics(func() { - str := Sprint(nil) - assert.NotEmpty(str) - }) - assert.NotPanics(func() { - str := Sprint(testStruct{}) - assert.NotEmpty(str) - }) - }) - - t.Run("PrintJSON", func(t *testing.T) { - assert := assert.New(t) - - assert.NotPanics(func() { - PrintJSON(nil) - }) - assert.NotPanics(func() { - PrintJSON(testStruct{}) - }) - }) - - t.Run("SprintJSON", func(t *testing.T) { - assert := assert.New(t) - - assert.NotPanics(func() { - str := SprintJSON(nil) - assert.NotEmpty(str) - }) - - assert.NotPanics(func() { - str := SprintJSON(testStruct{}) - assert.NotEmpty(str) - }) - }) -} diff --git a/pkg/utils/path.go b/pkg/utils/path.go deleted file mode 100644 index 4bd4e31..0000000 --- a/pkg/utils/path.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "os" - "path/filepath" - "runtime" -) - -// RelativePathOf 获取基于调用函数的调用对象相对位置的相对路径。 -func RelativePathOf(fp string) string { - _, file, _, ok := runtime.Caller(1) - if !ok { - return "" - } - - callerDir := filepath.Dir(filepath.FromSlash(file)) - - return filepath.FromSlash(filepath.Join(callerDir, fp)) -} - -func RelativePathBasedOnPwdOf(fp string) string { - dir, err := os.Getwd() - if err != nil { - return "" - } - - return filepath.FromSlash(filepath.Join(dir, fp)) -} diff --git a/pkg/utils/random.go b/pkg/utils/random.go deleted file mode 100644 index c7ca291..0000000 --- a/pkg/utils/random.go +++ /dev/null @@ -1,102 +0,0 @@ -package utils - -import ( - "crypto/rand" - "crypto/sha256" - "encoding/base64" - "fmt" - "math/big" - "strings" -) - -const ( - defaultRandBytesLength = 32 - - defaultRandomHashStringRandomBytesLength = 64 - maxRandomHashStringLength = 64 -) - -// RandBytes 根据给定的长度生成字节,长度默认为 32。 -func RandBytes(length ...int) ([]byte, error) { - b := make([]byte, defaultRandBytesLength) - if len(length) != 0 { - b = make([]byte, length[0]) - } - - _, err := rand.Read(b) - if err != nil { - return nil, err - } - - return b, nil -} - -// RandomBase64Token 根据给定的字节长度生成 URL 安全的 Base64 字符串,长度默认为 32 -// 长度为原始字节数据的长度,并非 Base64 字符串实际长度,默认 32 情况下实际长度约为 44。 -func RandomBase64Token(length ...int) (string, error) { - b, err := RandBytes(length...) - if err != nil { - return "", err - } - - return base64.URLEncoding.EncodeToString(b), nil -} - -// RandomHashString 生成随机 SHA256 字符串,最大长度为 64。 -func RandomHashString(length ...int) string { - b, _ := RandBytes(defaultRandomHashStringRandomBytesLength) - - if len(length) != 0 { - sliceLength := length[0] - if sliceLength > maxRandomHashStringLength || sliceLength <= 0 { - sliceLength = maxRandomHashStringLength - } - - return fmt.Sprintf("%x", sha256.Sum256(b))[:sliceLength] - } - - return fmt.Sprintf("%x", sha256.Sum256(b)) -} - -// RandomInt64 生成随机整数。 -func RandomInt64(max ...int64) int64 { - innerMax := int64(0) - if len(max) == 0 || (len(max) > 0 && max[0] <= 0) { - innerMax = 9999999999 - } else { - innerMax = max[0] - } - - nBig, _ := rand.Int(rand.Reader, big.NewInt(innerMax)) - n := nBig.Int64() - - return n -} - -// RandomInt64InRange 在区间内生成随机整数。 -func RandomInt64InRange(min, max int64) int64 { - if max <= 0 { - panic("max must be greater than 0") - } - if min >= max { - panic("min must be less than max") - } - - nBig, _ := rand.Int(rand.Reader, big.NewInt(max-min)) - n := nBig.Int64() - - return n + min -} - -// RandomInt64String 在区间内生成随机整数。 -func RandomInt64String(digits int64) string { - max := big.NewInt(0) - min := big.NewInt(0) - - max.SetString(strings.Repeat("9", int(digits)), 10) - min.SetString(fmt.Sprintf("%s%s", "1", strings.Repeat("0", int(digits)-1)), 10) - - nBig, _ := rand.Int(rand.Reader, new(big.Int).Sub(max, min)) - - return new(big.Int).Add(nBig, min).String() -} diff --git a/pkg/utils/random_test.go b/pkg/utils/random_test.go deleted file mode 100644 index ee219f7..0000000 --- a/pkg/utils/random_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package utils - -import ( - "fmt" - "strconv" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestRandomBytes(t *testing.T) { - t.Run("no args", func(t *testing.T) { - assert := assert.New(t) - - rand1, _ := RandBytes() - rand2, _ := RandBytes() - - assert.NotEqual(rand1, rand2) - assert.Equal(len(rand1), 32) - assert.Equal(len(rand2), 32) - }) - t.Run("with args", func(t *testing.T) { - assert := assert.New(t) - - arg := 123 - rand1, _ := RandBytes(arg) - rand2, _ := RandBytes(arg) - - assert.NotEqual(rand1, rand2) - assert.Equal(len(rand1), arg) - assert.Equal(len(rand2), arg) - }) -} - -func TestRandomBase64Token(t *testing.T) { - t.Run("no args", func(t *testing.T) { - assert := assert.New(t) - - rand1, _ := RandomBase64Token() - rand2, _ := RandomBase64Token() - - assert.NotEqual(rand1, rand2) - assert.Equal(len(rand1), 44) - assert.Equal(len(rand2), 44) - }) - t.Run("with args", func(t *testing.T) { - assert := assert.New(t) - - arg := 123 - rand1, _ := RandomBase64Token(arg) - rand2, _ := RandomBase64Token(arg) - - assert.NotEqual(rand1, rand2) - }) -} - -func TestRandomHashString(t *testing.T) { - assert := assert.New(t) - - hashString := RandomHashString() - assert.NotEmpty(hashString) - assert.Len(hashString, 64) - - hashString2 := RandomHashString(32) - assert.NotEmpty(hashString2) - assert.Len(hashString2, 32) -} - -func TestRandomString(t *testing.T) { - data, err := RandBytes(32) - assert.NoError(t, err) - assert.Len(t, data, 32) - - assert.Len(t, fmt.Sprintf("%x", data), 64) -} - -func TestRandomInt64(t *testing.T) { - assert := assert.New(t) - - assert.NotZero(RandomInt64()) -} - -func TestRandomInt64InRange(t *testing.T) { - assert := assert.New(t) - - randomNumber := RandomInt64InRange(100000, 999999) - assert.NotZero(randomNumber) - assert.Len(strconv.FormatInt(randomNumber, 10), 6) -} - -func TestRandomInt64String(t *testing.T) { - assert := assert.New(t) - - for i := 0; i < 100; i++ { - t.Run(fmt.Sprintf("Try#%d", i+1), func(t *testing.T) { - numberStr := RandomInt64String(6) - assert.NotEmpty(numberStr) - assert.Len(numberStr, 6) - - numberStr = RandomInt64String(32) - assert.NotEmpty(numberStr) - assert.Len(numberStr, 32) - }) - } -} diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go deleted file mode 100644 index 84b539b..0000000 --- a/pkg/utils/strings.go +++ /dev/null @@ -1,57 +0,0 @@ -package utils - -import ( - "unicode" -) - -func ContainsCJKChar(s string) bool { - for _, r := range s { - if unicode.Is(unicode.Han, r) { - return true - } - if unicode.Is(unicode.Hangul, r) { - return true - } - if unicode.Is(unicode.Hiragana, r) { - return true - } - if unicode.Is(unicode.Katakana, r) { - return true - } - - /* - U+3001 、 - U+3002 。 - U+3003 〃 - U+3008 〈 - U+3009 〉 - U+300A 《 - U+300B 》 - U+300C 「 - U+300D 」 - U+300E 『 - U+300F 』 - U+3010 【 - U+3011 】 - U+3014 〔 - U+3015 〕 - U+3016 〖 - U+3017 〗 - U+3018 〘 - U+3019 〙 - U+301A 〚 - U+301B 〛 - U+301C 〜 - U+301D 〝 - U+301E 〞 - U+301F 〟 - U+3030 〰 - U+303D 〽 - */ - if r >= 0x3001 && r <= 0x303D { - return true - } - } - - return false -} diff --git a/pkg/utils/strings_test.go b/pkg/utils/strings_test.go deleted file mode 100644 index d4b585b..0000000 --- a/pkg/utils/strings_test.go +++ /dev/null @@ -1 +0,0 @@ -package utils diff --git a/pkg/utils/units.go b/pkg/utils/units.go deleted file mode 100644 index c4f172a..0000000 --- a/pkg/utils/units.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -// Bytes Units. -const ( - // KB = 1024. - BytesUnitKB = 1024 - // MB = 1024 * KB. - BytesUnitMB = 1024 * BytesUnitKB - // GB = 1024 * MB. - BytesUnitGB = 1024 * BytesUnitMB -)