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

Add protobuf rewrite rule overrides #144

Merged
merged 3 commits into from
Nov 21, 2024
Merged

Add protobuf rewrite rule overrides #144

merged 3 commits into from
Nov 21, 2024

Conversation

kalamay
Copy link
Contributor

@kalamay kalamay commented Nov 21, 2024

This adds an override mechanism for specifying alternative proto.Rewriter's when performing template rewrites. These new rules can be provided as an optional set of parameters to proto.ParseRewriteTemplate, so this does not change the calling signature when rules are not used. When provided, these rules are matched against the field name in a nested fashion (see the tests for an example).

We have some cases where we need to rewrite flags that are encoded as a single integer bit mask. By combining these rules with the new proto.BitOr type, we can merge flags instead of replacing them:

type Message struct {
	Flags uint64 `protobuf:"varint,2,opt,name=flags,proto3"`
}

typ := proto.TypeOf(reflect.TypeOf(Message{}))
template := []byte(`{"flags": 8}`) // n |= 0b1000
rw, err := proto.ParseRewriteTemplate(typ, template, proto.RewriterRules {
	"flags": proto.BitOr[uint64]{},
})

@kalamay kalamay merged commit a8de6b8 into master Nov 21, 2024
9 checks passed
@kalamay kalamay deleted the add-bit-merge branch November 21, 2024 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants