From 733ac2012b8b2de98b8bbe88189a52c0a003ac8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Mon, 3 Apr 2023 22:54:03 +0800 Subject: [PATCH] - update amap - update baidu - update qq --- go.mod | 2 +- go.sum | 4 +- library.go | 2 +- service/amap/config.go | 12 ++++ service/baidu/client.go | 36 +++++----- service/baidu/config.go | 12 ++++ service/baidu/const.go | 4 ++ service/baidu/express.go | 33 --------- service/baidu/get.go | 11 +++ service/baidu/location.ip.go | 57 +++++++++++++++ service/baidu/request.go | 21 ++++-- service/baidu/weather.go | 72 +++++++++++++++++++ service/qq/config.go | 12 ++++ .../bytedance/sonic/decoder/compiler.go | 30 +++++++- .../bytedance/sonic/decoder/decoder.go | 12 +++- .../sonic/encoder/assembler_amd64_go116.go | 3 +- .../sonic/encoder/assembler_amd64_go117.go | 3 +- .../bytedance/sonic/internal/rt/fastvalue.go | 15 +++- vendor/modules.txt | 2 +- 19 files changed, 272 insertions(+), 71 deletions(-) create mode 100644 service/amap/config.go create mode 100644 service/baidu/config.go delete mode 100644 service/baidu/express.go create mode 100644 service/baidu/get.go create mode 100644 service/baidu/location.ip.go create mode 100644 service/baidu/weather.go create mode 100644 service/qq/config.go diff --git a/go.mod b/go.mod index 17e306cc..65c632d5 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/allegro/bigcache/v3 v3.1.0 github.com/baidubce/bce-sdk-go v0.9.146 github.com/basgys/goxml2json v1.1.0 - github.com/bytedance/sonic v1.8.6 + github.com/bytedance/sonic v1.8.7 github.com/gin-gonic/gin v1.9.0 github.com/go-co-op/gocron v1.19.0 github.com/go-playground/locales v0.14.1 diff --git a/go.sum b/go.sum index 3d07042a..29b6d277 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngE github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.8.6 h1:aUgO9S8gvdN6SyW2EhIpAw5E4ChworywIEndZCkCVXk= -github.com/bytedance/sonic v1.8.6/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/bytedance/sonic v1.8.7 h1:d3sry5vGgVq/OpgozRUNP6xBsSo0mtNdwliApw+SAMQ= +github.com/bytedance/sonic v1.8.7/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/library.go b/library.go index b4126c83..58729361 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.125" + return "1.0.126" } diff --git a/service/amap/config.go b/service/amap/config.go new file mode 100644 index 00000000..6cba0ff9 --- /dev/null +++ b/service/amap/config.go @@ -0,0 +1,12 @@ +package amap + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/baidu/client.go b/service/baidu/client.go index 6578a334..ce733b67 100644 --- a/service/baidu/client.go +++ b/service/baidu/client.go @@ -3,35 +3,31 @@ package baidu import ( "github.com/dtapps/go-library/utils/golog" "github.com/dtapps/go-library/utils/gorequest" - "gorm.io/gorm" ) -type ConfigClient struct { - PgsqlDb *gorm.DB // 日志数据库 +// ClientConfig 实例配置 +type ClientConfig struct { + Ak string } - type Client struct { - client *gorequest.App // 请求客户端 - log *golog.ApiClient // 日志服务 - config *ConfigClient // 配置 + requestClient *gorequest.App // 请求服务 + config struct { + ak string + } + log struct { + status bool // 状态 + client *golog.ApiClient // 日志服务 + } } -func NewClient(config *ConfigClient) (*Client, error) { +// NewClient 创建实例化 +func NewClient(config *ClientConfig) (*Client, error) { - var err error - c := &Client{config: config} + c := &Client{} - c.client = gorequest.NewHttp() + c.config.ak = config.Ak - if c.config.PgsqlDb != nil { - c.log, err = golog.NewApiClient( - golog.WithGormClient(c.config.PgsqlDb), - golog.WithTableName(logTable), - ) - if err != nil { - return nil, err - } - } + c.requestClient = gorequest.NewHttp() return c, nil } diff --git a/service/baidu/config.go b/service/baidu/config.go new file mode 100644 index 00000000..26c85049 --- /dev/null +++ b/service/baidu/config.go @@ -0,0 +1,12 @@ +package baidu + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/baidu/const.go b/service/baidu/const.go index 56fe5fdf..86f494e6 100644 --- a/service/baidu/const.go +++ b/service/baidu/const.go @@ -1,5 +1,9 @@ package baidu +const ( + apiUrl = "https://api.map.baidu.com" +) + const ( logTable = "baidu" ) diff --git a/service/baidu/express.go b/service/baidu/express.go deleted file mode 100644 index a0a9ba4c..00000000 --- a/service/baidu/express.go +++ /dev/null @@ -1,33 +0,0 @@ -package baidu - -import ( - "github.com/dtapps/go-library/utils/gojson" - "github.com/dtapps/go-library/utils/gorequest" - "net/http" -) - -type ExpressResponse struct{} - -type ExpressResult struct { - Result ExpressResponse // 结果 - Body []byte // 内容 - Http gorequest.Response // 请求 - Err error // 错误 -} - -func newExpressResult(result ExpressResponse, body []byte, http gorequest.Response, err error) *ExpressResult { - return &ExpressResult{Result: result, Body: body, Http: http, Err: err} -} - -// Express ipv1 -// https://www.cloudflare.com/ips-v4 -func (c *Client) Express() *ExpressResult { - // 参数 - params := gorequest.NewParams() - // 请求 - request, err := c.request("https://m.baidu.com/s?word=快递查询&ts={$ts}&t_kt=0&ie=utf-8&rsv_iqid=&rsv_t=&sa=&rsv_pq=&rsv_sug4=&tj=1&inputT={$input}&sugid=&ss=", params, http.MethodPost) - // 定义 - var response ExpressResponse - err = gojson.Unmarshal(request.ResponseBody, &response) - return newExpressResult(response, request.ResponseBody, request, err) -} diff --git a/service/baidu/get.go b/service/baidu/get.go new file mode 100644 index 00000000..a92f30d2 --- /dev/null +++ b/service/baidu/get.go @@ -0,0 +1,11 @@ +package baidu + +import "github.com/dtapps/go-library/utils/golog" + +func (c *Client) GetAk() string { + return c.config.ak +} + +func (c *Client) GetLog() *golog.ApiClient { + return c.log.client +} diff --git a/service/baidu/location.ip.go b/service/baidu/location.ip.go new file mode 100644 index 00000000..f6cf4b71 --- /dev/null +++ b/service/baidu/location.ip.go @@ -0,0 +1,57 @@ +package baidu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type LocationIpResponse struct { + Address string `json:"address"` // 详细地址信息 + Content struct { + AddressDetail struct { + Province string `json:"province"` // 省份 + City string `json:"city"` // 城市 + District string `json:"district"` + Street string `json:"street"` + StreetNumber string `json:"street_number"` + CityCode int `json:"city_code"` // 百度城市代码 + Adcode string `json:"adcode"` + } `json:"address_detail"` + Address string `json:"address"` // 简要地址信息 + Point struct { + X string `json:"x"` // 当前城市中心点经度 + Y string `json:"y"` // 当前城市中心点纬度 + } `json:"point"` + } `json:"content"` + Status int `json:"status"` +} + +type LocationIpResult struct { + Result LocationIpResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newLocationIpResult(result LocationIpResponse, body []byte, http gorequest.Response) *LocationIpResult { + return &LocationIpResult{Result: result, Body: body, Http: http} +} + +// LocationIp 普通IP定位 +// https://lbsyun.baidu.com/index.php?title=webapi/ip-api +func (c *Client) LocationIp(ctx context.Context, ip string, notMustParams ...gorequest.Params) (*LocationIpResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("ak", c.GetAk()) + params.Set("ip", ip) + // 请求 + request, err := c.request(ctx, apiUrl+"/location/ip", params, http.MethodGet) + if err != nil { + return newLocationIpResult(LocationIpResponse{}, request.ResponseBody, request), err + } + // 定义 + var response LocationIpResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newLocationIpResult(response, request.ResponseBody, request), err +} diff --git a/service/baidu/request.go b/service/baidu/request.go index bc43c48d..2316d0c3 100644 --- a/service/baidu/request.go +++ b/service/baidu/request.go @@ -1,11 +1,15 @@ package baidu -import "github.com/dtapps/go-library/utils/gorequest" +import ( + "context" + "github.com/dtapps/go-library" + "github.com/dtapps/go-library/utils/gorequest" +) -func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) { +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) { // 创建请求 - client := c.client + client := c.requestClient // 设置请求地址 client.SetUri(url) @@ -19,15 +23,18 @@ func (c *Client) request(url string, params map[string]interface{}, method strin // 设置参数 client.SetParams(params) + // 传入SDk版本 + client.AfferentSdkUserVersion(go_library.Version()) + // 发起请求 - request, err := client.Request() + request, err := client.Request(ctx) if err != nil { return gorequest.Response{}, err } - // 日志 - if c.config.PgsqlDb != nil { - go c.log.GormMiddleware(request) + // 记录日志 + if c.log.status { + go c.log.client.Middleware(ctx, request, go_library.Version()) } return request, err diff --git a/service/baidu/weather.go b/service/baidu/weather.go new file mode 100644 index 00000000..37744656 --- /dev/null +++ b/service/baidu/weather.go @@ -0,0 +1,72 @@ +package baidu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type WeatherResponse struct { + Status int `json:"status"` + Result struct { + Location struct { + Country string `json:"country"` + Province string `json:"province"` + City string `json:"city"` + Name string `json:"name"` + Id string `json:"id"` + } `json:"location"` + Now struct { + Text string `json:"text"` + Temp int `json:"temp"` + FeelsLike int `json:"feels_like"` + Rh int `json:"rh"` + WindClass string `json:"wind_class"` + WindDir string `json:"wind_dir"` + Uptime string `json:"uptime"` + } `json:"now"` + Forecasts []struct { + TextDay string `json:"text_day"` + TextNight string `json:"text_night"` + High int `json:"high"` + Low int `json:"low"` + WcDay string `json:"wc_day"` + WdDay string `json:"wd_day"` + WcNight string `json:"wc_night"` + WdNight string `json:"wd_night"` + Date string `json:"date"` + Week string `json:"week"` + } `json:"forecasts"` + } `json:"result"` + Message string `json:"message"` +} + +type WeatherResult struct { + Result WeatherResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newWeatherResult(result WeatherResponse, body []byte, http gorequest.Response) *WeatherResult { + return &WeatherResult{Result: result, Body: body, Http: http} +} + +// Weather 国内天气查询服务 +// https://lbsyun.baidu.com/index.php?title=webapi/weather +func (c *Client) Weather(ctx context.Context, districtId string, notMustParams ...gorequest.Params) (*WeatherResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("ak", c.GetAk()) + params.Set("district_id", districtId) + params.Set("output", "json") + // 请求 + request, err := c.request(ctx, apiUrl+"/weather/v1/", params, http.MethodGet) + if err != nil { + return newWeatherResult(WeatherResponse{}, request.ResponseBody, request), err + } + // 定义 + var response WeatherResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newWeatherResult(response, request.ResponseBody, request), err +} diff --git a/service/qq/config.go b/service/qq/config.go new file mode 100644 index 00000000..af907b5a --- /dev/null +++ b/service/qq/config.go @@ -0,0 +1,12 @@ +package qq + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/vendor/github.com/bytedance/sonic/decoder/compiler.go b/vendor/github.com/bytedance/sonic/decoder/compiler.go index 2779e7df..bacd5f96 100644 --- a/vendor/github.com/bytedance/sonic/decoder/compiler.go +++ b/vendor/github.com/bytedance/sonic/decoder/compiler.go @@ -679,12 +679,38 @@ func (self *_Compiler) compilePtr(p *_Program, sp int, et reflect.Type) { /* dereference all the way down */ for et.Kind() == reflect.Ptr { + if et.Implements(jsonUnmarshalerType) { + p.rtt(_OP_unmarshal_p, et) + return + } + + if et.Implements(encodingTextUnmarshalerType) { + p.add(_OP_lspace) + self.compileUnmarshalTextPtr(p, et) + return + } + et = et.Elem() p.rtt(_OP_deref, et) } - /* compile the element type */ - self.compileOne(p, sp + 1, et) + /* check for recursive nesting */ + ok := self.tab[et] + if ok { + p.rtt(_OP_recurse, et) + return + } + + /* enter the recursion */ + p.add(_OP_lspace) + self.tab[et] = true + + /* not inline the pointer type + * recursing the defined pointer type's elem will casue issue379. + */ + self.compileOps(p, sp, et) + delete(self.tab, et) + j := p.pc() p.add(_OP_goto) p.pin(i) diff --git a/vendor/github.com/bytedance/sonic/decoder/decoder.go b/vendor/github.com/bytedance/sonic/decoder/decoder.go index 5326f972..19ad7196 100644 --- a/vendor/github.com/bytedance/sonic/decoder/decoder.go +++ b/vendor/github.com/bytedance/sonic/decoder/decoder.go @@ -17,6 +17,7 @@ package decoder import ( + `unsafe` `encoding/json` `reflect` `runtime` @@ -127,8 +128,17 @@ func (self *Decoder) Decode(val interface{}) error { return &json.InvalidUnmarshalError{Type: vv.Type.Pack()} } + etp := rt.PtrElem(vv.Type) + + /* check the defined pointer type for issue 379 */ + if vv.Type.IsNamed() { + newp := vp + etp = vv.Type + vp = unsafe.Pointer(&newp) + } + /* create a new stack, and call the decoder */ - sb, etp := newStack(), rt.PtrElem(vv.Type) + sb := newStack() nb, err := decodeTypedPointer(self.s, self.i, etp, vp, sb, self.f) /* return the stack back */ self.i = nb diff --git a/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go116.go b/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go116.go index 9b597843..d056259f 100644 --- a/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go116.go +++ b/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go116.go @@ -514,7 +514,8 @@ func (self *_Assembler) call_marshaler(fn obj.Addr, it *rt.GoType, vt reflect.Ty switch vt.Kind() { case reflect.Interface : self.call_marshaler_i(fn, it) case reflect.Ptr, reflect.Map: self.call_marshaler_v(fn, it, vt, true) - default : self.call_marshaler_v(fn, it, vt, false) + // struct/array of 1 direct iface type can be direct + default : self.call_marshaler_v(fn, it, vt, !rt.UnpackType(vt).Indirect()) } } diff --git a/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go117.go b/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go117.go index 8cd83e86..1f1b2807 100644 --- a/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go117.go +++ b/vendor/github.com/bytedance/sonic/encoder/assembler_amd64_go117.go @@ -539,7 +539,8 @@ func (self *_Assembler) call_marshaler(fn obj.Addr, it *rt.GoType, vt reflect.Ty switch vt.Kind() { case reflect.Interface : self.call_marshaler_i(fn, it) case reflect.Ptr, reflect.Map : self.call_marshaler_v(fn, it, vt, true) - default : self.call_marshaler_v(fn, it, vt, false) + // struct/array of 1 direct iface type can be direct + default : self.call_marshaler_v(fn, it, vt, !rt.UnpackType(vt).Indirect()) } } diff --git a/vendor/github.com/bytedance/sonic/internal/rt/fastvalue.go b/vendor/github.com/bytedance/sonic/internal/rt/fastvalue.go index 87df6b94..2b2757f5 100644 --- a/vendor/github.com/bytedance/sonic/internal/rt/fastvalue.go +++ b/vendor/github.com/bytedance/sonic/internal/rt/fastvalue.go @@ -22,14 +22,23 @@ import ( ) var ( - reflectRtypeItab = findReflectRtypeItab() + reflectRtypeItab = findReflectRtypeItab() ) +// GoType.KindFlags const const ( F_direct = 1 << 5 F_kind_mask = (1 << 5) - 1 ) +// GoType.Flags const +const ( + tflagUncommon uint8 = 1 << 0 + tflagExtraStar uint8 = 1 << 1 + tflagNamed uint8 = 1 << 2 + tflagRegularMemory uint8 = 1 << 3 +) + type GoType struct { Size uintptr PtrData uintptr @@ -44,6 +53,10 @@ type GoType struct { PtrToSelf int32 } +func (self *GoType) IsNamed() bool { + return (self.Flags & tflagNamed) != 0 +} + func (self *GoType) Kind() reflect.Kind { return reflect.Kind(self.KindFlags & F_kind_mask) } diff --git a/vendor/modules.txt b/vendor/modules.txt index e3a94caf..da467c0f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -24,7 +24,7 @@ github.com/baidubce/bce-sdk-go/util/log github.com/basgys/goxml2json # github.com/bitly/go-simplejson v0.5.0 ## explicit -# github.com/bytedance/sonic v1.8.6 +# github.com/bytedance/sonic v1.8.7 ## explicit; go 1.15 github.com/bytedance/sonic github.com/bytedance/sonic/ast