Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
使用 json.RawMessage 代替 any
Browse files Browse the repository at this point in the history
  • Loading branch information
eleven26 committed Mar 10, 2023
1 parent 1d6abc5 commit 335f7d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.7.0
)
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -28,7 +21,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
40 changes: 13 additions & 27 deletions sampler/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package sampler
import (
"encoding/json"

"github.com/spf13/cast"

"github.com/eleven26/zipkin_sampler/contract"
)

var _ contract.Span = &Span{}

type Span map[string]interface{}
type Span map[string]json.RawMessage

func NewSpans(data []byte) ([]contract.Span, error) {
var spans []Span
Expand All @@ -29,36 +27,24 @@ func NewSpans(data []byte) ([]contract.Span, error) {
return result, nil
}

func (s *Span) TraceId() string {
return (*s)["traceId"].(string)
}

func (s *Span) IsRoot() bool {
v, ok := (*s)["parentId"]
if !ok {
return true
}

if v == "" {
return true
}
return s.ParentId() == ""
}

return v == nil
func (s *Span) TraceId() string {
var v string
_ = json.Unmarshal((*s)["traceId"], &v)
return v
}

func (s *Span) ParentId() string {
v, ok := (*s)["parentId"]
if !ok {
return ""
}

if v == nil {
return ""
}

return v.(string)
var v string
_ = json.Unmarshal((*s)["parentId"], &v)
return v
}

func (s *Span) Timestamp() int64 {
return cast.ToInt64((*s)["timestamp"])
var v int64
_ = json.Unmarshal((*s)["timestamp"], &v)
return v
}
25 changes: 21 additions & 4 deletions sampler/span_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sampler

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,23 +44,39 @@ func TestNewSpans2(t *testing.T) {
}

func TestSpan(t *testing.T) {
span := Span{"timestamp": 123}
span, err := mapToSpan(map[string]any{"timestamp": 123})
assert.Nil(t, err)
assert.True(t, span.IsRoot())
assert.Equal(t, "", span.ParentId())
assert.Equal(t, int64(123), span.Timestamp())

span = Span{"parentId": "123", "timestamp": 123}
span, err = mapToSpan(map[string]any{"parentId": "123", "timestamp": 123})
assert.Nil(t, err)
assert.False(t, span.IsRoot())
assert.Equal(t, "123", span.ParentId())
assert.Equal(t, int64(123), span.Timestamp())

span = Span{"parentId": nil, "timestamp": 123}
span, err = mapToSpan(map[string]any{"parentId": nil, "timestamp": 123})
assert.Nil(t, err)
assert.True(t, span.IsRoot())
assert.Equal(t, "", span.ParentId())
assert.Equal(t, int64(123), span.Timestamp())

span = Span{"parentId": "", "timestamp": 123, "traceId": "123456"}
span, err = mapToSpan(map[string]any{"parentId": "", "timestamp": 123, "traceId": "123456"})
assert.Nil(t, err)
assert.True(t, span.IsRoot())
assert.Equal(t, "", span.ParentId())
assert.Equal(t, "123456", span.TraceId())
}

func mapToSpan(m map[string]any) (Span, error) {
bs, err := json.Marshal(m)
if err != nil {
return nil, err
}

var span Span
err = json.Unmarshal(bs, &span)

return span, err
}

0 comments on commit 335f7d9

Please sign in to comment.