From b5bea3e8577d4f4c00b7f8a7866b8d91670b3dd1 Mon Sep 17 00:00:00 2001 From: Maxim Oransky Date: Fri, 11 Sep 2020 21:57:09 +0300 Subject: [PATCH] jsoniter --- go.mod | 3 ++- go.sum | 14 ++++++++++++++ json.go | 5 +++++ session.go | 26 ++++++++++++++++++-------- ws.go | 1 - 5 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 json.go diff --git a/go.mod b/go.mod index f34ed0d..4f71ef8 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,9 @@ go 1.14 require ( github.com/google/uuid v1.1.2 github.com/gorilla/websocket v1.4.2 + github.com/json-iterator/go v1.1.10 github.com/pkg/errors v0.9.1 - github.com/tada-team/tdproto v0.0.13 + github.com/tada-team/tdproto v0.2.1 ) //replace github.com/tada-team/tdproto v0.0.13 => ../tdproto diff --git a/go.sum b/go.sum index 6fda4d7..e4e11b9 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,28 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/tada-team/tdproto v0.0.3 h1:BCkh0cDz7EU778FmfpzEWn7Ifvl+KyYW+yttK339Kpc= github.com/tada-team/tdproto v0.0.3/go.mod h1:VY1rTZXOhMxyZ1VE/Q82PB27AaFAooEQJFkO0HMlN8M= github.com/tada-team/tdproto v0.0.7 h1:eGQQG+/QNwWp0savQmnirJ80gmMajsR2MI6fyzCPNi8= github.com/tada-team/tdproto v0.0.7/go.mod h1:eQGUKL2y6C9EGrhOZY0HU7bb/5lBdVfHBlx2+e0l7x4= github.com/tada-team/tdproto v0.0.12 h1:mVKzWHsu6rWyz1idWP6ewe5rCtThiU9CcukZiTo8URM= github.com/tada-team/tdproto v0.0.12/go.mod h1:eQGUKL2y6C9EGrhOZY0HU7bb/5lBdVfHBlx2+e0l7x4= +github.com/tada-team/tdproto v0.2.1 h1:uCHXKzoETO1lvbpO9WEmgqhSp+PBmlxF+08m4cDUNQs= +github.com/tada-team/tdproto v0.2.1/go.mod h1:eQGUKL2y6C9EGrhOZY0HU7bb/5lBdVfHBlx2+e0l7x4= diff --git a/json.go b/json.go new file mode 100644 index 0000000..b72fbd4 --- /dev/null +++ b/json.go @@ -0,0 +1,5 @@ +package tdclient + +import jsoniter "github.com/json-iterator/go" + +var json = jsoniter.ConfigCompatibleWithStandardLibrary diff --git a/session.go b/session.go index 31eab4b..aaa5a38 100644 --- a/session.go +++ b/session.go @@ -1,7 +1,6 @@ package tdclient import ( - "encoding/json" "fmt" "io/ioutil" "log" @@ -10,9 +9,8 @@ import ( "os" "time" - "github.com/tada-team/tdproto" - "github.com/pkg/errors" + "github.com/tada-team/tdproto" ) type apiResp struct { @@ -25,18 +23,16 @@ type Session struct { logger *log.Logger server url.URL token string + cookie string features *tdproto.Features } -func NewSession(server string, verbose bool) (Session, error) { +func NewSession(server string) (Session, error) { s := Session{ Timeout: 10 * time.Second, } s.logger = log.New(os.Stdout, "tdclient: ", log.LstdFlags|log.Lmicroseconds|log.Lmsgprefix) - if !verbose { - s.logger.SetOutput(ioutil.Discard) - } u, err := url.Parse(server) if err != nil { @@ -59,7 +55,21 @@ func (s *Session) Features() (*tdproto.Features, error) { return s.features, nil } -func (s *Session) SetToken(token string) { s.token = token } +func (s *Session) SetToken(v string) { + s.token = v +} + +func (s *Session) SetCookie(v string) { + s.cookie = v +} + +func (s *Session) SetVerbose(v bool) { + if v { + s.logger.SetOutput(os.Stdout) + } else { + s.logger.SetOutput(ioutil.Discard) + } +} func (s Session) Ping() error { resp := new(struct { diff --git a/ws.go b/ws.go index b5929e6..4c36c28 100644 --- a/ws.go +++ b/ws.go @@ -1,7 +1,6 @@ package tdclient import ( - "encoding/json" "fmt" "log" "net/http"