Skip to content

Commit

Permalink
Merge pull request #91 from dongxuny/master
Browse files Browse the repository at this point in the history
Add pointer related codes
  • Loading branch information
dongxuny authored Dec 24, 2022
2 parents 08a3d48 + 944c870 commit f77b44f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/prometheus/client_golang v1.13.0
github.com/rookie-ninja/rk-entry/v2 v2.2.14
github.com/rookie-ninja/rk-entry/v2 v2.2.15
github.com/rookie-ninja/rk-logger v1.2.13
github.com/rookie-ninja/rk-query v1.2.14
github.com/rs/xid v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rookie-ninja/rk-entry/v2 v2.2.14 h1:PHzofuw52zfskrgJKC+WIMt1LtoBosrukjWHZ/DKsTE=
github.com/rookie-ninja/rk-entry/v2 v2.2.14/go.mod h1:P/Fd6Oyvvx0ITbEU2lzO3KkQ9miWVwd84aPaC0LMD0o=
github.com/rookie-ninja/rk-entry/v2 v2.2.15 h1:K5Ww1wCjOcAvK11pBA8qM7FpbHyFE5i+YQBAIYxPrZc=
github.com/rookie-ninja/rk-entry/v2 v2.2.15/go.mod h1:P/Fd6Oyvvx0ITbEU2lzO3KkQ9miWVwd84aPaC0LMD0o=
github.com/rookie-ninja/rk-logger v1.2.13 h1:ERxeNZUmszlY4xehHcJRXECPtbjYIXzN8yRIyYyLGsg=
github.com/rookie-ninja/rk-logger v1.2.13/go.mod h1:0ZiGn1KsHKOmCv+FHMH7k40DWYSJcj5yIR3EYcjlnLs=
github.com/rookie-ninja/rk-query v1.2.14 h1:aYNyMXixpsEYRfEOz9Npt5QG3A6BQlo9vKjYc78x7bc=
Expand Down
14 changes: 13 additions & 1 deletion middleware/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
var (
noopTracerProvider = trace.NewNoopTracerProvider()
noopEvent = rkquery.NewEventFactory().CreateEventNoop()
pointerCreator rkcursor.PointerCreator
)

// GetIncomingHeaders extract call-scoped incoming headers
Expand Down Expand Up @@ -52,12 +53,23 @@ func SetHeaderToClient(ctx *gin.Context, key, value string) {
header.Set(key, value)
}

// SetPointerCreator override rkcursor.PointerCreator
func SetPointerCreator(creator rkcursor.PointerCreator) {
pointerCreator = creator
}

// GetCursor create rkcursor.Cursor instance
func GetCursor(ctx *gin.Context) *rkcursor.Cursor {
return rkcursor.NewCursor(
res := rkcursor.NewCursor(
rkcursor.WithLogger(GetLogger(ctx)),
rkcursor.WithEvent(GetEvent(ctx)),
rkcursor.WithEntryNameAndType(GetEntryName(ctx), "GinEntry"))

if pointerCreator != nil {
res.Creator = pointerCreator
}

return res
}

// GetEvent extract takes the call-scoped EventData from middleware.
Expand Down
30 changes: 30 additions & 0 deletions middleware/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package rkginctx
import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
rkcursor "github.com/rookie-ninja/rk-entry/v2/cursor"
"github.com/rookie-ninja/rk-entry/v2/middleware"
"github.com/rookie-ninja/rk-logger"
"github.com/rookie-ninja/rk-query"
Expand Down Expand Up @@ -270,6 +271,35 @@ func TestGetCsrfToken(t *testing.T) {
assert.Equal(t, "value", GetCsrfToken(ctx))
}

func TestSetPointerCreator(t *testing.T) {
assert.Nil(t, pointerCreator)

SetPointerCreator(createFakePointer)

assert.NotNil(t, pointerCreator)
}

func createFakePointer(p *rkcursor.CursorPayload) rkcursor.Pointer {
return &fakePointer{}
}

type fakePointer struct{}

func (f fakePointer) PrintError(err error) {
//TODO implement me
panic("implement me")
}

func (f fakePointer) ObserveError(err error) error {
//TODO implement me
panic("implement me")
}

func (f fakePointer) Release() {
//TODO implement me
panic("implement me")
}

func assertNotPanic(t *testing.T) {
if r := recover(); r != nil {
// Expect panic to be called with non nil error
Expand Down

0 comments on commit f77b44f

Please sign in to comment.