Skip to content

Code generation for protobuf visitors#119

Merged
pglass merged 9 commits intomainfrom
pglass/add-visit-codegen
Jul 17, 2025
Merged

Code generation for protobuf visitors#119
pglass merged 9 commits intomainfrom
pglass/add-visit-codegen

Conversation

@pglass
Copy link
Copy Markdown
Contributor

@pglass pglass commented Jul 16, 2025

What was changed

  • Add cmd/tools/genvisitor to generate code based on protobuf type hierarchies
  • Use cmd/tools/genvisitor to generate code to repair invalid utf-8 in any Failure typed fields in protobuf messages
  • Put generated code in proto/compat directory

(This PR does not add the new codec)

Why?

Checklist

  1. Closes

  2. How was this tested:

Basic unit tests (spot checks)

  1. Any docs updates needed?

@pglass pglass requested a review from a team as a code owner July 16, 2025 19:24
logger.Debug("ignore non Failure field", tag.NewAnyTag("path", path.String()))
return false
}
if strings.Contains(path.String(), "/Cause") {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this is so we only visit the top level "Failure" type? maybe add a comment here.

Copy link
Copy Markdown
Contributor Author

@pglass pglass Jul 16, 2025

Choose a reason for hiding this comment

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

Right. We can have the repairInvalidUTF8InFailure function only handle the top-level Failure and it can descend in to the Cause field.

return string(result)
}

func snakeToPascalCase[T ~string](s T) string {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can you provide some examples on input -> output to help understanding?

@@ -0,0 +1,68 @@
package main
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can we have a section about the tool in CONTRIBUTING.md and how the repair works at high level?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

actually, probably best is to create an github issue for this work so we can record design choices.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added the contributing section (not a github issue)

},
)

protoregistry.GlobalTypes.RangeMessages(func(mt protoreflect.MessageType) bool {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i think the reason is that protoreflect doesn't work with gogobuf and later we will filter types not in gogobuf. Add some comments to explain why iterates protobuf types instead of gogobuf.

serverapireplication "github.com/temporalio/s2s-proxy/proto/1_22/server/api/replication/v1"
)

func repairInvalidUTF8(vAny any) (ret bool, retErr error) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is repairInvalidUTF8 hooked in the grpc codec already or will be a separate PR?

Copy link
Copy Markdown
Contributor Author

@pglass pglass Jul 16, 2025

Choose a reason for hiding this comment

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

Will be a separate PR - just splitting up PRs a little.

@hehaifengcn hehaifengcn requested a review from Copilot July 16, 2025 21:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces code generation tools for working with protobuf type hierarchies and generates code to repair invalid UTF-8 in Failure-typed fields in protobuf messages. The main purpose is to create a visitor pattern generator that can traverse protobuf type hierarchies and generate appropriate handling code.

  • Adds cmd/tools/genvisitor tool to generate code based on protobuf type hierarchies
  • Implements UTF-8 repair functionality for Failure typed fields across protobuf messages
  • Makes function name changes to use lowercase for generated conversion functions

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
proto/compat/repair_utf8_test.go Tests for UTF-8 repair functionality with various failure scenarios
proto/compat/repair_utf8_gen.go Generated code to repair UTF-8 in Failure fields across protobuf type hierarchy
proto/compat/repair_utf8.go Core UTF-8 repair logic with depth limits for failure chains
proto/compat/frontend_conversion_gen.go Changes function name from uppercase to lowercase
proto/compat/admin_conversion_gen.go Changes function name from uppercase to lowercase
cmd/tools/genvisitor/* New code generation tool for protobuf visitor patterns
cmd/tools/genrpcwrappers/extra.go Template update for consistent function naming
Makefile Build target for running the genvisitor tool
Comments suppressed due to low confidence (1)

proto/compat/repair_utf8_gen.go:131

  • The package declaration is incorrect. This should be 'package compat' since the file is in the compat directory and is imported as such in the test file.
						retErr = err

Comment thread cmd/tools/genvisitor/emitter.go Outdated
Comment thread proto/compat/repair_utf8.go Outdated
Comment thread cmd/tools/genvisitor/emitter.go Outdated
Paul Glass and others added 5 commits July 16, 2025 17:39
@pglass pglass merged commit 32c47bb into main Jul 17, 2025
6 checks passed
@pglass pglass deleted the pglass/add-visit-codegen branch July 17, 2025 17:45
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.

3 participants