Skip to content

Commit

Permalink
引入 gjson
Browse files Browse the repository at this point in the history
  • Loading branch information
freehub committed Nov 12, 2019
1 parent 817c44b commit 97da20d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hashicorp/consul/api v1.2.0
github.com/looplab/fsm v0.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/tidwall/gjson v1.3.4
go.mongodb.org/mongo-driver v1.1.2
xorm.io/builder v0.3.5
)
Expand Down
6 changes: 6 additions & 0 deletions demo/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ=
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9/go.mod h1:RHkNRtSLfOK7qBTHaeSX1D6BNpI3qw7NTxsmNr4RvN8=
github.com/tidwall/gjson v1.3.4 h1:On5waDnyKKk3SWE4EthbjjirAWXp43xx5cKCUZY1eZw=
github.com/tidwall/gjson v1.3.4/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down
8 changes: 7 additions & 1 deletion demo/src/controller/cache_get_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/9299381/wego/cache"
"github.com/9299381/wego/contracts"
"github.com/tidwall/gjson"
)

type CacheGetController struct {
Expand All @@ -20,5 +21,10 @@ func (s *CacheGetController) Handle(ctx contracts.Context) (interface{}, error)
ctx.Log.Info(d["aaa"])
}

return nil, nil
//使用gjson 更方便
return map[string]interface{}{
"ccc": gjson.Get(string(v), "ccc").Str,
"aaa": d["aaa"],
"gaaa": gjson.Get(string(v), "aaa").String(),
}, nil
}
12 changes: 12 additions & 0 deletions demo/test/json_demo/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package json_demo

import (
"github.com/tidwall/gjson"
"testing"
)

func TestJson(t *testing.T) {
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
value := gjson.Get(json, "name.last")
println(value.String())
}
7 changes: 4 additions & 3 deletions filters/comm_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filters

import (
"context"
"github.com/9299381/wego/args"
"github.com/9299381/wego/constants"
"github.com/9299381/wego/contracts"
"github.com/9299381/wego/loggers"
Expand All @@ -25,15 +26,15 @@ func (s *CommEndpoint) Make() endpoint.Endpoint {
//生成请求参数
req := request.(contracts.Request)
_, has := (req.Data)["mock"]
if has {
if has && args.Mode == "dev" {
if v, ok := s.Controller.(contracts.IMock); ok {
return v.Mock(), nil
}
}
//生成context上下文
cc := s.makeContext(ctx, req)
//成成线程log,统一处理ip,request_id等
cc.Log = s.makeLog(cc, req)
cc.Log = s.makeLog(req)
//参数验证
err := s.valid(cc, req)
if err != nil {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (s *CommEndpoint) valid(ctx contracts.Context, request contracts.Request) e
return nil
}

func (s *CommEndpoint) makeLog(ctx contracts.Context, req contracts.Request) *logrus.Entry {
func (s *CommEndpoint) makeLog(req contracts.Request) *logrus.Entry {
//初始化日志字段,放到context中
ip, has := (req.Data)["client_ip"]
if !has || ip == nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/spf13/viper v1.4.0
github.com/tebeka/strftime v0.1.3 // indirect
github.com/tidwall/gjson v1.3.4 // indirect
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.1.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2K
github.com/tebeka/strftime v0.1.3 h1:5HQXOqWKYRFfNyBMNVc9z5+QzuBtIXy03psIhtdJYto=
github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ=
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9/go.mod h1:RHkNRtSLfOK7qBTHaeSX1D6BNpI3qw7NTxsmNr4RvN8=
github.com/tidwall/gjson v1.3.4 h1:On5waDnyKKk3SWE4EthbjjirAWXp43xx5cKCUZY1eZw=
github.com/tidwall/gjson v1.3.4/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
Expand Down

0 comments on commit 97da20d

Please sign in to comment.