Skip to content

Conversation

@bufdev
Copy link
Member

@bufdev bufdev commented Dec 10, 2025

Fixes #4213.

This PR adds swift_prefix to managed mode, e.g.

version: v2
inputs:
  - directory: api
managed:
  enabled: true
  override:
    - path: user/v1
      file_option: swift_prefix
      value: UserV1

If managed.enabled: true is set, but no override is provided, the
default behavior for swift_prefix is to take the pascal case of the
package name and use a _ delimited prefix, e.g. foo.empty -> Foo_Empty_.
This emulates the default behavior for apple/swift when swift_prefix
is not set.

@bufdev bufdev requested a review from doriable December 10, 2025 22:44
@github-actions
Copy link
Contributor

github-actions bot commented Dec 10, 2025

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedDec 12, 2025, 6:34 PM

// package declaration. If the image file doesn't have a package declaration, an
// empty string is returned.
func swiftPrefixValue(imageFile bufimage.ImageFile) string {
panic("TODO")
Copy link
Member Author

Choose a reason for hiding this comment

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

This TODO

Copy link
Member

Choose a reason for hiding this comment

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

Pushed an update for this TODO, thanks for setting this PR up!

@doriable
Copy link
Member

Will open a PR for docs so that we have the docs ready to go when this feature is merged and released.

@doriable doriable changed the title WIP: Add swift_prefix to managed mode Add swift_prefix to managed mode Dec 11, 2025
pkg := imageFile.FileDescriptorProto().GetPackage()
if pkg == "" {
return ""
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Just confirming you checked this is right (I'm not sure if there's anything special to verify or. Ot based on what protoc's behavior is, maybe not)

Copy link
Member

Choose a reason for hiding this comment

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

I actually had a question about this/wanted your thoughts on the behaviour.

I checked against apple/swift, and I think the following behaviour makes sense before and after this change:

  • When the package is not set and swift_prefix is not set, there will be no prefix (same before and after)
  • When swift_prefix is set (in the file before the change and by managed mode after the change), the struct will get the configured prefix.

The only thing I'm unsure about is the default behaviour/what we want to set the default value for swift_prefix in managed mode to be, since swift_prefix is an optional file option for apple/swift. So when a file does not have swift_prefix at all, apple/swift will still generate code and will set the prefix of each struct to a _ delimited version of the package in pascal case, e.g.

syntax = "proto3";

package weather.acme.v1;

message Foo {...}
struct Weather_Acme_V1_Foo: ...

So I wasn't sure if we wanted to set an opinionated version of this, e.g. WeatherAcmeV1, or if we should effectively treat swift_prefix as unset when managed.enabled: true but no override is given to swift_prefix. I'm starting to think we should err on the side of the latter, just pushed up an update, but interested to get your opinion on this.

@doriable doriable merged commit a293ec6 into main Dec 16, 2025
11 checks passed
@doriable doriable deleted the swift-prefix branch December 16, 2025 20:12
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.

Support swift_prefix override

4 participants