-
Notifications
You must be signed in to change notification settings - Fork 59
feat(sidekick/swift): generate deprecation attributes #6750
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
Merged
coryan
merged 3 commits into
googleapis:main
from
coryan:feat-swift-deprecated-elements
Jul 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
internal/sidekick/swift/generate_deprecated_enum_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package swift | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/googleapis/librarian/internal/sidekick/api" | ||
| "github.com/googleapis/librarian/internal/sidekick/parser" | ||
| ) | ||
|
|
||
| func TestGenerateEnum_Deprecated(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name string | ||
| enumDeprecated bool | ||
| valDeprecated bool | ||
| wantEnum string | ||
| wantCase string | ||
| }{ | ||
| { | ||
| name: "deprecated-enum", | ||
| enumDeprecated: true, | ||
| valDeprecated: false, | ||
| wantEnum: "/// -- enum marker --\n@available(*, deprecated)\npublic enum Status", | ||
| wantCase: "/// -- case marker --\n case unspecified", | ||
| }, | ||
| { | ||
| name: "deprecated-value", | ||
| enumDeprecated: false, | ||
| valDeprecated: true, | ||
| wantEnum: "/// -- enum marker --\npublic enum Status", | ||
| wantCase: "/// -- case marker --\n @available(*, deprecated)\n case unspecified", | ||
| }, | ||
| { | ||
| name: "both-deprecated", | ||
| enumDeprecated: true, | ||
| valDeprecated: true, | ||
| wantEnum: "/// -- enum marker --\n@available(*, deprecated)\npublic enum Status", | ||
| wantCase: "/// -- case marker --\n @available(*, deprecated)\n case unspecified", | ||
| }, | ||
| { | ||
| name: "not-deprecated", | ||
| enumDeprecated: false, | ||
| valDeprecated: false, | ||
| wantEnum: "/// -- enum marker --\npublic enum Status", | ||
| wantCase: "/// -- case marker --\n case unspecified", | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| outDir := t.TempDir() | ||
|
|
||
| enum := &api.Enum{ | ||
| Name: "Status", | ||
| Package: "google.cloud.test.v1", | ||
| ID: ".google.cloud.test.v1.Status", | ||
| Deprecated: test.enumDeprecated, | ||
| Documentation: "-- enum marker --", | ||
| } | ||
| enum.Values = []*api.EnumValue{ | ||
| { | ||
| Name: "STATUS_UNSPECIFIED", | ||
| Number: 0, | ||
| Parent: enum, | ||
| Deprecated: test.valDeprecated, | ||
| Documentation: "-- case marker --", | ||
| }, | ||
| } | ||
| enum.UniqueNumberValues = enum.Values | ||
|
|
||
| model := api.NewTestAPI(nil, []*api.Enum{enum}, nil) | ||
| model.PackageName = "google.cloud.test.v1" | ||
| cfg := &parser.ModelConfig{} | ||
| if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "Status.swift") | ||
| content, err := os.ReadFile(filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| contentStr := string(content) | ||
|
|
||
| got := extractBlock(t, contentStr, "/// -- enum marker --", "public enum Status") | ||
| if diff := cmp.Diff(test.wantEnum, got); diff != "" { | ||
| t.Errorf("mismatch (-want +got):\n%s", diff) | ||
| } | ||
| got = extractBlock(t, contentStr, "/// -- case marker --", "case unspecified") | ||
| if diff := cmp.Diff(test.wantCase, got); diff != "" { | ||
| t.Errorf("mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package swift | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/googleapis/librarian/internal/sidekick/api" | ||
| "github.com/googleapis/librarian/internal/sidekick/parser" | ||
| ) | ||
|
|
||
| func TestGenerateField_Deprecated(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name string | ||
| deprecated bool | ||
| repeated bool | ||
| want string | ||
| endStr string | ||
| }{ | ||
| { | ||
| name: "deprecated", | ||
| deprecated: true, | ||
| repeated: false, | ||
| want: " /// -- field marker --\n @available(*, deprecated)\n public var normalField: Swift.String", | ||
| endStr: "public var normalField: Swift.String", | ||
| }, | ||
| { | ||
| name: "not-deprecated", | ||
| deprecated: false, | ||
| repeated: false, | ||
| want: " /// -- field marker --\n public var normalField: Swift.String", | ||
| endStr: "public var normalField: Swift.String", | ||
| }, | ||
| { | ||
| name: "deprecated-repeated", | ||
| deprecated: true, | ||
| repeated: true, | ||
| want: " /// -- field marker --\n @available(*, deprecated)\n public var normalField: [Swift.String]", | ||
| endStr: "public var normalField: [Swift.String]", | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| outDir := t.TempDir() | ||
|
|
||
| field := &api.Field{ | ||
| Name: "normal_field", | ||
| Documentation: "-- field marker --", | ||
| ID: ".google.cloud.test.v1.TestMessage.normal_field", | ||
| Typez: api.TypezString, | ||
| Deprecated: test.deprecated, | ||
| Repeated: test.repeated, | ||
| } | ||
|
|
||
| msg := &api.Message{ | ||
| Name: "TestMessage", | ||
| Package: "google.cloud.test.v1", | ||
| ID: ".google.cloud.test.v1.TestMessage", | ||
| Fields: []*api.Field{field}, | ||
| } | ||
|
|
||
| model := api.NewTestAPI([]*api.Message{msg}, nil, nil) | ||
| model.PackageName = "google.cloud.test.v1" | ||
| cfg := &parser.ModelConfig{} | ||
| if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "TestMessage.swift") | ||
| content, err := os.ReadFile(filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| contentStr := string(content) | ||
|
|
||
| got := extractBlock(t, contentStr, " /// -- field marker --", test.endStr) | ||
| if diff := cmp.Diff(test.want, got); diff != "" { | ||
| t.Errorf("mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
internal/sidekick/swift/generate_deprecated_message_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package swift | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/googleapis/librarian/internal/sidekick/api" | ||
| "github.com/googleapis/librarian/internal/sidekick/parser" | ||
| ) | ||
|
|
||
| func TestGenerateMessage_Deprecated(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name string | ||
| topDeprecated bool | ||
| nestedDeprecated bool | ||
| wantTop string | ||
| wantNested string | ||
| }{ | ||
| { | ||
| name: "deprecated-both", | ||
| topDeprecated: true, | ||
| nestedDeprecated: true, | ||
| wantTop: "/// -- top marker --\n@available(*, deprecated)\npublic struct TopMessage", | ||
| wantNested: " /// -- nested marker --\n @available(*, deprecated)\n public struct NestedMessage", | ||
| }, | ||
| { | ||
| name: "deprecated-top-only", | ||
| topDeprecated: true, | ||
| nestedDeprecated: false, | ||
| wantTop: "/// -- top marker --\n@available(*, deprecated)\npublic struct TopMessage", | ||
| wantNested: " /// -- nested marker --\n public struct NestedMessage", | ||
| }, | ||
| { | ||
| name: "deprecated-nested-only", | ||
| topDeprecated: false, | ||
| nestedDeprecated: true, | ||
| wantTop: "/// -- top marker --\npublic struct TopMessage", | ||
| wantNested: " /// -- nested marker --\n @available(*, deprecated)\n public struct NestedMessage", | ||
| }, | ||
| { | ||
| name: "not-deprecated", | ||
| topDeprecated: false, | ||
| nestedDeprecated: false, | ||
| wantTop: "/// -- top marker --\npublic struct TopMessage", | ||
| wantNested: " /// -- nested marker --\n public struct NestedMessage", | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| outDir := t.TempDir() | ||
|
|
||
| nested := &api.Message{ | ||
| Name: "NestedMessage", | ||
| Package: "google.cloud.test.v1", | ||
| ID: ".google.cloud.test.v1.TopMessage.NestedMessage", | ||
| Deprecated: test.nestedDeprecated, | ||
| Documentation: "-- nested marker --", | ||
| } | ||
|
|
||
| top := &api.Message{ | ||
| Name: "TopMessage", | ||
| Package: "google.cloud.test.v1", | ||
| ID: ".google.cloud.test.v1.TopMessage", | ||
| Deprecated: test.topDeprecated, | ||
| Documentation: "-- top marker --", | ||
| Messages: []*api.Message{nested}, | ||
| } | ||
|
|
||
| model := api.NewTestAPI([]*api.Message{top}, nil, nil) | ||
| model.PackageName = "google.cloud.test.v1" | ||
| cfg := &parser.ModelConfig{} | ||
| if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "TopMessage.swift") | ||
| content, err := os.ReadFile(filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| contentStr := string(content) | ||
|
|
||
| gotTop := extractBlock(t, contentStr, "/// -- top marker --", "public struct TopMessage") | ||
| if diff := cmp.Diff(test.wantTop, gotTop); diff != "" { | ||
| t.Errorf("mismatch top (-want +got):\n%s", diff) | ||
| } | ||
|
|
||
| gotNested := extractBlock(t, contentStr, " /// -- nested marker --", "public struct NestedMessage") | ||
| if diff := cmp.Diff(test.wantNested, gotNested); diff != "" { | ||
| t.Errorf("mismatch nested (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.