Skip to content

Commit

Permalink
Merge pull request #38 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 8805376 + 901cf56 commit e5a91f0
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 @@ -6,7 +6,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/labstack/echo/v4 v4.9.1
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 @@ -307,8 +307,8 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqn
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
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 echo.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 echo.Context) *rkcursor.Cursor {
return rkcursor.NewCursor(
res := rkcursor.NewCursor(
rkcursor.WithLogger(GetLogger(ctx)),
rkcursor.WithEvent(GetEvent(ctx)),
rkcursor.WithEntryNameAndType(GetEntryName(ctx), "EchoEntry"))

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 @@ -9,6 +9,7 @@ import (
"context"
"github.com/golang-jwt/jwt/v4"
"github.com/labstack/echo/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 @@ -278,6 +279,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 e5a91f0

Please sign in to comment.