Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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