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

refactor common proto resource (un)marshallers into generic functions #41562

Merged
merged 2 commits into from
May 15, 2024

Conversation

nklaassen
Copy link
Contributor

We have many implementations of resource marshallers and unmarshallers that are basically identical aside from the types. Most developers just find an existing resource and copy whatever it does.

This PR adds new generic marshal and unmarshal functions that should work for all new RFD153-style resources based on protobuf types generated with buf. The recommendation for new types going forward is to use services.MarshalProtoResource and services.UnmarshalProtoResource for your type.

services.FastMarshalProtoResourceDeprecated and services.FastUnmarshalProtoResourceDeprecated were also added since 7 of the new types already wrote their (un)marshal functions based on utils.FastMarshal, which does not support some protobuf features like oneof. If someone takes the time to check if it would be safe to convert these types to the new protojson-based marshaler, they could potentially be converted.

For now in this PR, I am keeping all functionality identical and just refactoring common code into shared functions. The one exception to this is MarshalAccessMonitoringRule which previously just called utils.FastMarshal without considering the MarshalOptions, it now uses FastMarshalProtoResourceDeprecated which does respect the marshal options.


cp := proto.Clone(r).(T)
//nolint:staticcheck // SA1019. Id is deprecated, but still needed.
cp.GetMetadata().Id = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK no one is using this anymore

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine to leave this for now, we'll be removing Id entirely in the near future


unmarshalled, err := tc.unmarshalFunc(marshaled)
require.NoError(t, err)
require.Empty(t, cmp.Diff(resource, unmarshalled, cmpopts.IgnoreUnexported(vnet.VnetConfig{}, vnet.VnetConfigSpec{}, headerv1.Metadata{})))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
require.Empty(t, cmp.Diff(resource, unmarshalled, cmpopts.IgnoreUnexported(vnet.VnetConfig{}, vnet.VnetConfigSpec{}, headerv1.Metadata{})))
require.Empty(t, cmp.Diff(resource, unmarshalled, protocmp.Transform()))

@nklaassen nklaassen enabled auto-merge May 15, 2024 18:02
@nklaassen nklaassen added this pull request to the merge queue May 15, 2024
Merged via the queue into master with commit b466c8d May 15, 2024
37 checks passed
@nklaassen nklaassen deleted the nklaassen/marshalprotoresource branch May 15, 2024 18:35
@public-teleport-github-review-bot

@nklaassen See the table below for backport results.

Branch Result
branch/v13 Failed
branch/v14 Failed
branch/v15 Failed

nklaassen added a commit that referenced this pull request May 15, 2024
nklaassen added a commit that referenced this pull request May 15, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 23, 2024
…ctions (#41615)

* [v15] refactor common proto resource (un)marshallers into generic functions

Backport #41562 to branch/v15

* test with type that exists
github-merge-queue bot pushed a commit that referenced this pull request May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants