Skip to content

Commit

Permalink
base the default behavior of UseAccessLevelOnImports on Swift version…
Browse files Browse the repository at this point in the history
… the protoc plugin is compiled with, update docs to explain interoperability of UseAccessLevelOnImports and ImplementationOnlyImports
  • Loading branch information
Skoti committed Aug 4, 2024
1 parent aaa453d commit 74857e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Documentation/PLUGIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,23 @@ this will only work if the `Visibility` is set to `internal`.

##### Generation Option: `UseAccessLevelOnImports` - imports preceded by a visibility modifier (`public`, `package`, `internal`)

By default, the code generator does not precede any imports with a visibility modifier.
You can change this with the `UseAccessLevelOnImports` option:
The default behavior depends on the Swift version the plugin is compiled with.
For Swift versions below 6.0 the default is `false` and the code generator does not precede any imports with a visibility modifier.
You can change this by explicitly setting the `UseAccessLevelOnImports` option:

```
$ protoc --swift_opt=UseAccessLevelOnImports=[value] --swift_out=. foo/bar/*.proto mumble/*.proto
```

The possible values for `UseAccessLevelOnImports` are:

* `false` (default): Generates plain import directives without a visibility modifier.
* `false`: Generates plain import directives without a visibility modifier.
* `true`: Imports of internal dependencies and any modules defined in the module
mappings will be preceded by a visibility modifier corresponding to the visibility of the generated types - see `Visibility` option.

**Important:** It is strongly encouraged to use `internal` imports instead of `@_implementationOnly` imports.
Hence `UseAccessLevelOnImports` and `ImplementationOnlyImports` options exclude each other.


### Building your project

Expand Down
10 changes: 7 additions & 3 deletions Sources/protoc-gen-swift/Docs.docc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,23 @@ this will only work if the `Visibility` is set to `internal`.

##### Generation Option: `UseAccessLevelOnImports` - imports preceded by a visibility modifier (`public`, `package`, `internal`)

By default, the code generator does not precede any imports with a visibility modifier.
You can change this with the `UseAccessLevelOnImports` option:
The default behavior depends on the Swift version the plugin is compiled with.
For Swift versions below 6.0 the default is `false` and the code generator does not precede any imports with a visibility modifier.
You can change this by explicitly setting the `UseAccessLevelOnImports` option:

```
$ protoc --swift_opt=UseAccessLevelOnImports=[value] --swift_out=. foo/bar/*.proto mumble/*.proto
```

The possible values for `UseAccessLevelOnImports` are:

* `false` (default): Generates plain import directives without a visibility modifier.
* `false`: Generates plain import directives without a visibility modifier.
* `true`: Imports of internal dependencies and any modules defined in the module
mappings will be preceded by a visibility modifier corresponding to the visibility of the generated types - see `Visibility` option.

**Important:** It is strongly encouraged to use `internal` imports instead of `@_implementationOnly` imports.
Hence `UseAccessLevelOnImports` and `ImplementationOnlyImports` options exclude each other.


### Building your project

Expand Down
4 changes: 4 additions & 0 deletions Sources/protoc-gen-swift/GeneratorOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class GeneratorOptions {
var visibility: Visibility = .internal
var swiftProtobufModuleName: String? = nil
var implementationOnlyImports: Bool = false
#if swift(>=6.0)
var useAccessLevelOnImports = true
#else
var useAccessLevelOnImports = false
#endif
var experimentalStripNonfunctionalCodegen: Bool = false

for pair in parameter.parsedPairs {
Expand Down

0 comments on commit 74857e2

Please sign in to comment.