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

feat: Issue Credential - adds middleware #1859

Merged
merged 1 commit into from
May 27, 2020
Merged

feat: Issue Credential - adds middleware #1859

merged 1 commit into from
May 27, 2020

Conversation

soluchok
Copy link
Contributor

@soluchok soluchok commented May 26, 2020

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.

service, err := ctx.Service(issuecredential.name)
if err != nil {
	/// ....
}
service.(interface {
	Use(items ...issuecredential.Middleware)
}).Use(func(next issuecredential.Handler) issuecredential.Handler {
	return issuecredential.HandlerFunc(func(metadata issuecredential.MetaData) error {
		// some logic here
		return next.Handle(metadata)
	})
})

Signed-off-by: Andrii Soluk isoluchok@gmail.com

@soluchok soluchok added the wip label May 26, 2020
@codecov
Copy link

codecov bot commented May 26, 2020

Codecov Report

Merging #1859 into master will increase coverage by 0.03%.
The diff coverage is 97.18%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
pkg/didcomm/protocol/issuecredential/states.go 100.00% <ø> (+1.09%) ⬆️
pkg/framework/aries/default.go 82.79% <60.00%> (-1.48%) ⬇️
pkg/didcomm/protocol/issuecredential/middleware.go 100.00% <100.00%> (ø)
pkg/didcomm/protocol/issuecredential/service.go 91.33% <100.00%> (+1.05%) ⬆️
...protocol/middleware/issuecredential/middlewares.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d0c7e5...cebb4c6. Read the comment docs.

@@ -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...)
Copy link
Contributor

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

Copy link
Contributor Author

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...)
}

Copy link
Contributor

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)....

Copy link
Contributor Author

@soluchok soluchok May 27, 2020

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
}

llorllale
llorllale previously approved these changes May 26, 2020
@llorllale llorllale dismissed their stale review May 26, 2020 18:53

looks OK, but need to fix codecoverage

Signed-off-by: Andrii Soluk <isoluchok@gmail.com>
@fqutishat fqutishat merged commit 61bd23b into hyperledger-archives:master May 27, 2020
@soluchok soluchok mentioned this pull request Aug 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants