-
Notifications
You must be signed in to change notification settings - Fork 160
feat: Issue Credential - adds middleware #1859
feat: Issue Credential - adds middleware #1859
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1859 +/- ##
==========================================
+ Coverage 90.88% 90.91% +0.03%
==========================================
Files 190 192 +2
Lines 14351 14377 +26
==========================================
+ Hits 13043 13071 +28
+ Misses 762 761 -1
+ Partials 546 545 -1
Continue to review full report at Codecov.
|
@@ -168,6 +178,11 @@ func New(p Provider) (*Service, error) { | |||
return svc, nil | |||
} | |||
|
|||
// Use allows providing middlewares | |||
func (s *Service) Use(middlewares ...Middleware) { | |||
s.middlewares = append(s.middlewares, middlewares...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest you use stack
here so that the ordering is clear and that for-loop where you build the handler chain makes more sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to:
func (s *Service) Use(items ...Middleware) {
s.stack = append(s.stack, items...)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to use a stack
as a data structure (hence the link to https://pkg.go.dev/github.com/golang-collections/collections/stack)....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it :) I don't think we really need that. This is one more dependency for the project.
But I did a small refactor. Now it is understandable.
func (s *Service) Use(items ...Middleware) {
var handler Handler = initialHandler
for i := len(items) - 1; i >= 0; i-- {
handler = items[i](handler)
}
s.middleware = handler
}
Signed-off-by: Andrii Soluk <isoluchok@gmail.com>
Adds middleware support to the service.
Add your custom logic to the protocol by using
Use(...Middleware)
function.The middleware functions will be executed every time before state execution.
This PR also includes the helper function which saves the credentials to the store.
It was done as an example of how to use the middleware function.
This function does not depend on the format of the attachment and assumes that the payload is a verifiable credential.
It can be easily changed when we have document/RFC which describes the payload for the specific format.
Note: The
Use
function is not exposed to the context. But, there is a possibility to use that.Signed-off-by: Andrii Soluk isoluchok@gmail.com