Skip to content

Commit ec7465c

Browse files
authored
refactor(internal/librarian/golang): refactor version functions in Go (#4059)
Create functions to calculate client directory and versioned api path for Go package. For #3617
1 parent 0de8d88 commit ec7465c

5 files changed

Lines changed: 211 additions & 20 deletions

File tree

‎internal/librarian/golang/generate.go‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ func buildGAPICImportPath(apiPath string, library *config.Library, goAPI *config
219219
importPath, modulePathVersion, version, clientDir)
220220
}
221221

222-
func findGoAPI(library *config.Library, apiPath string) *config.GoAPI {
223-
if library.Go == nil {
224-
return nil
225-
}
226-
for _, ga := range library.Go.GoAPIs {
227-
if ga.Path == apiPath {
228-
return ga
229-
}
230-
}
231-
return nil
232-
}
233-
234222
// fixVersioning moves {name}/{version}/* up to {name}/ for versioned modules.
235223
func fixVersioning(outputDir, library, modPath string) error {
236224
// parts is the module path split by "/".
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package golang
16+
17+
import "github.com/googleapis/librarian/internal/config"
18+
19+
func findGoAPI(library *config.Library, apiPath string) *config.GoAPI {
20+
if library.Go == nil {
21+
return nil
22+
}
23+
for _, ga := range library.Go.GoAPIs {
24+
if ga.Path == apiPath {
25+
return ga
26+
}
27+
}
28+
return nil
29+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package golang
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
"github.com/googleapis/librarian/internal/config"
22+
)
23+
24+
func TestFindGoAPI(t *testing.T) {
25+
for _, test := range []struct {
26+
name string
27+
library *config.Library
28+
apiPath string
29+
want *config.GoAPI
30+
}{
31+
{
32+
name: "find an api",
33+
library: &config.Library{
34+
Name: "secretmanager",
35+
Go: &config.GoModule{
36+
GoAPIs: []*config.GoAPI{
37+
{
38+
Path: "google/cloud/secretmanager/v1",
39+
ClientDirectory: "customDir",
40+
},
41+
},
42+
},
43+
},
44+
apiPath: "google/cloud/secretmanager/v1",
45+
want: &config.GoAPI{
46+
Path: "google/cloud/secretmanager/v1",
47+
ClientDirectory: "customDir",
48+
},
49+
},
50+
{
51+
name: "do not have a go module",
52+
library: &config.Library{
53+
Name: "secretmanager",
54+
},
55+
apiPath: "google/cloud/secretmanager/v1",
56+
},
57+
{
58+
name: "find an api",
59+
library: &config.Library{
60+
Name: "secretmanager",
61+
Go: &config.GoModule{
62+
GoAPIs: []*config.GoAPI{
63+
{
64+
Path: "google/cloud/secretmanager/v1",
65+
ClientDirectory: "customDir",
66+
},
67+
},
68+
},
69+
},
70+
apiPath: "google/cloud/secretmanager/v1beta1",
71+
},
72+
} {
73+
t.Run(test.name, func(t *testing.T) {
74+
got := findGoAPI(test.library, test.apiPath)
75+
if diff := cmp.Diff(test.want, got); diff != "" {
76+
t.Errorf("mismatch (-want +got):\n%s", diff)
77+
}
78+
})
79+
}
80+
}

‎internal/librarian/golang/version.go‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ func generateInternalVersionFile(moduleDir, version string) (err error) {
6161
}
6262

6363
func generateClientVersionFile(library *config.Library, apiPath string) (err error) {
64-
version := filepath.Base(apiPath)
65-
goAPI := findGoAPI(library, apiPath)
66-
var clientDir string
67-
if goAPI != nil && goAPI.ClientDirectory != "" {
68-
clientDir = goAPI.ClientDirectory
69-
}
70-
71-
dir := filepath.Join(library.Output, library.Name, clientDir, "api"+version)
64+
dir, clientDir := resolveClientPath(library, apiPath)
7265
if err := os.MkdirAll(dir, 0755); err != nil {
7366
return err
7467
}
@@ -96,6 +89,22 @@ func generateClientVersionFile(library *config.Library, apiPath string) (err err
9689
})
9790
}
9891

92+
// resolveClientPath constructs the full path for the API version and determines the client directory.
93+
func resolveClientPath(library *config.Library, apiPath string) (string, string) {
94+
version := filepath.Base(apiPath)
95+
clientDir := clientDirectory(library, apiPath)
96+
return filepath.Join(library.Output, library.Name, clientDir, "api"+version), clientDir
97+
}
98+
99+
func clientDirectory(library *config.Library, apiPath string) string {
100+
goAPI := findGoAPI(library, apiPath)
101+
if goAPI != nil {
102+
return goAPI.ClientDirectory
103+
}
104+
// Return an empty client directory if we can't find one.
105+
return ""
106+
}
107+
99108
// writeLicenseHeader writes the license header as Go comments to the given file.
100109
func writeLicenseHeader(f *os.File) error {
101110
year := time.Now().Format("2006")

‎internal/librarian/golang/version_test.go‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/google/go-cmp/cmp"
2324
"github.com/googleapis/librarian/internal/config"
2425
)
2526

@@ -113,3 +114,87 @@ func TestGenerateClientVersionFile(t *testing.T) {
113114
})
114115
}
115116
}
117+
118+
func TestResolveClientPath(t *testing.T) {
119+
for _, test := range []struct {
120+
name string
121+
library *config.Library
122+
apiPath string
123+
wantVersionPath string
124+
wantClientDir string
125+
}{
126+
{
127+
name: "from apiPath",
128+
library: &config.Library{
129+
Name: "secretmanager",
130+
APIs: []*config.API{
131+
{
132+
Path: "google/cloud/secretmanager/v1",
133+
},
134+
{
135+
Path: "google/cloud/secretmanager/v1beta1",
136+
},
137+
},
138+
},
139+
apiPath: "google/cloud/secretmanager/v1",
140+
wantVersionPath: "secretmanager/apiv1",
141+
wantClientDir: "",
142+
},
143+
{
144+
name: "non existing GoAPI",
145+
library: &config.Library{
146+
Name: "secretmanager",
147+
APIs: []*config.API{
148+
{
149+
Path: "google/cloud/secretmanager/v1",
150+
},
151+
},
152+
Go: &config.GoModule{
153+
GoAPIs: []*config.GoAPI{
154+
{
155+
Path: "google/cloud/secretmanager/v1beta1",
156+
},
157+
},
158+
},
159+
},
160+
apiPath: "google/cloud/secretmanager/v1",
161+
wantVersionPath: "secretmanager/apiv1",
162+
wantClientDir: "",
163+
},
164+
{
165+
name: "from apiPath and client directory",
166+
library: &config.Library{
167+
Name: "ai",
168+
APIs: []*config.API{
169+
{
170+
Path: "google/cloud/ai/v1",
171+
},
172+
},
173+
Go: &config.GoModule{
174+
GoAPIs: []*config.GoAPI{
175+
{
176+
Path: "google/cloud/ai/v1",
177+
ClientDirectory: "customdir",
178+
},
179+
{
180+
Path: "google/cloud/ai/v1beta1",
181+
},
182+
},
183+
},
184+
},
185+
apiPath: "google/cloud/ai/v1",
186+
wantVersionPath: "ai/customdir/apiv1",
187+
wantClientDir: "customdir",
188+
},
189+
} {
190+
t.Run(test.name, func(t *testing.T) {
191+
gotVersionPath, gotClientDir := resolveClientPath(test.library, test.apiPath)
192+
if diff := cmp.Diff(test.wantVersionPath, gotVersionPath); diff != "" {
193+
t.Errorf("mismatch (-want +got):\n%s", diff)
194+
}
195+
if diff := cmp.Diff(test.wantClientDir, gotClientDir); diff != "" {
196+
t.Errorf("mismatch (-want +got):\n%s", diff)
197+
}
198+
})
199+
}
200+
}

0 commit comments

Comments
 (0)