Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-go] Support direct Payload's and Payload`s at top-level of Any in payload visitor #1862

Open
yuandrew opened this issue Mar 11, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@yuandrew
Copy link
Contributor

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
When creating a common.Payload and marshaling it into an Any type, the visitor is not able to visit this scenario, due to a missing case *common.Payload in visitPayloads().

This scenario is not used in our API today, but something we may want to support in the future.

Describe the solution you'd like
Implement this base case so we can visit this scenario.

Additional context
Here is a basic test I wrote to validate this new feature

func TestVisitPayloads_AnyPayload(t *testing.T) {
	msg, err := anypb.New(inputPayload())
	require.NoError(t, err)
	root := &protocol.Message{Body: msg}
	var anyCount int
	err = VisitPayloads(context.Background(), root, VisitPayloadsOptions{
		Visitor: func(ctx *VisitPayloadsContext, p []*common.Payload) ([]*common.Payload, error) {
			anyCount++
			// Only mutate if the payloads has "test"
			if len(p) == 1 && string(p[0].Data) == "test" {
				return []*common.Payload{{Data: []byte("new-val")}}, nil
			}
			return p, nil
		},
	})
	require.NoError(t, err)
	require.Equal(t, 1, anyCount)
	update1, err := root.Body.UnmarshalNew()
	require.NoError(t, err)
	require.Equal(t, "new-val", string(update1.(*common.Payload).Data))
}

also see temporalio/api-go#202 (comment) for more context

@yuandrew yuandrew added the enhancement New feature or request label Mar 11, 2025
@yuandrew yuandrew changed the title Support direct Payload's and Payload`s at top-level of Any in payload visitor [api-go] Support direct Payload's and Payload`s at top-level of Any in payload visitor Mar 12, 2025
@yuandrew
Copy link
Contributor Author

When this is implemented, this is another test that can be added

func TestProtocolMessage(t *testing.T) {
	require := require.New(t)

	var messageType protoreflect.MessageType
	protoregistry.GlobalTypes.RangeMessages(func(mt protoreflect.MessageType) bool {
		if string(mt.Descriptor().FullName()) == "temporal.api.protocol.v1.Message" {
			messageType = mt
		}
		return true
	})

	// Create empty instance and populate with test values
	msg := messageType.New().Interface().(proto.Message)
	var totalCount, count int
	populatePayload(&msg, msg, require, &totalCount, &count)

	require.Equal(0, count)
	require.Equal(1, totalCount)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant