Skip to content

Conversation

@adecaro
Copy link
Contributor

@adecaro adecaro commented Nov 23, 2025

This PR introduces a new option for Context#RunView, WithContext. WithContext allows the developer to specify a context.Context to use when running the view. This is very helpful when the context needs to be enhanced with additional values, for example.

Tested in the token-sdk: hyperledger-labs/fabric-token-sdk#1270

@adecaro adecaro self-assigned this Nov 23, 2025
@adecaro adecaro added the View API Related to FSC Views label Nov 23, 2025
@adecaro adecaro added this to the 25Q4 milestone Nov 23, 2025
"go.opentelemetry.io/otel/trace"
)

//go:generate counterfeiter -o mock/parent_context.go -fake-name ParentContext . ParentContext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line could be removed after generating the fakes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SaidAltury-ibm , it is still good to have it because one can regenerate the mocks faster when needed.

Copy link
Member

@mbrandenburger mbrandenburger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank @adecaro! This PR contains some good cleanup and good progress for better testing.

Comment on lines 20 to 25
func WrapContext(ctx ParentContext, goContext context.Context) *WrappedContext {
return &WrappedContext{
ParentContext: ctx,
goContext: goContext,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I suggest some better naming here and throughout the PR.

I believe we should use the variable ctx excluselivey for context.Context as this aligns pretty well with every other go code.

A view.Context, view.ParentContext, or view.ChildContext should not use ctx as variable if possible.

Example:

func WrapContext(p ParentContext, ctx context.Context) *WrappedContext {
	return &WrappedContext{
		ParentContext: p,
		ctx: ctx,
	}

@@ -25,61 +26,61 @@ type Responders struct {
Channel *session.LocalBidirectionalChannel
}

type MockContext struct {
type DelegatedContext struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we can move the DelegatedContext into the integration test where it is exclusively used, and even make it private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it here because it is kind of a mock.


type ctx struct {
// Context implements the view.Context interface
type Context struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this renaming is great!

@mbrandenburger
Copy link
Member

@adecaro it seems that the tokenSDK does not run the tests containing this PR hyperledger-labs/fabric-token-sdk#1270

@adecaro adecaro force-pushed the f-view-manager-run-with-different-context branch from f5142cb to a7c1d7a Compare November 24, 2025 12:23
Signed-off-by: Angelo De Caro <[email protected]>
Copy link
Contributor

@SaidAltury-ibm SaidAltury-ibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. @mbrandenburger any input?


// RunViewNow invokes the Call function of the given view.
// The view context used to the run view is either ctx or the one extracted from the options (see view.WithContext), if present.
func RunViewNow(ctx ParentContext, v View, opts ...view.RunViewOption) (res interface{}, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's quickly look at this function and apply our naming suggestions here as well to be consistent.

ctx ParentContext could become parent ParentContext.
goContext := ctx.Context could become ctx := parent.Context()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. I have updated also the token-sdk

Signed-off-by: Angelo De Caro <[email protected]>
@mbrandenburger mbrandenburger changed the title ability to run a view with a different context refactor: ability to run a view with a different context Nov 25, 2025
@mbrandenburger mbrandenburger merged commit 46fb0c7 into main Nov 25, 2025
22 checks passed
@mbrandenburger mbrandenburger deleted the f-view-manager-run-with-different-context branch November 25, 2025 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

View API Related to FSC Views

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants