Skip to content

Commit 573339d

Browse files
authored
refactor(internal/librarian/java): extract logic and struct to derive maven coordinates to file (#5121)
Extracts logic and structs around deriving maven coordinates for various modules within a Java library into a separate file.
1 parent 4249171 commit 573339d

9 files changed

Lines changed: 413 additions & 349 deletions

File tree

internal/librarian/java/generate.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ import (
3131
)
3232

3333
const (
34-
cloudPrefix = "google-cloud-"
35-
grpcPrefix = "grpc-"
36-
protoPrefix = "proto-"
3734
commonProtos = "google/cloud/common_resources.proto"
3835
)
3936

@@ -165,27 +162,6 @@ func generateAPI(ctx context.Context, cfg *config.Config, api *config.API, libra
165162
return nil
166163
}
167164

168-
// ensureCloudPrefix returns name with the "google-cloud-" prefix,
169-
// adding it if not already present.
170-
func ensureCloudPrefix(name string) string {
171-
if !strings.HasPrefix(name, cloudPrefix) {
172-
return cloudPrefix + name
173-
}
174-
return name
175-
}
176-
177-
func deriveDistributionName(library *config.Library) string {
178-
if library.Java != nil && library.Java.DistributionNameOverride != "" {
179-
return library.Java.DistributionNameOverride
180-
}
181-
groupID := "com.google.cloud"
182-
if library.Java != nil && library.Java.GroupID != "" {
183-
groupID = library.Java.GroupID
184-
}
185-
artifactID := ensureCloudPrefix(library.Name)
186-
return fmt.Sprintf("%s:%s", groupID, artifactID)
187-
}
188-
189165
var runProtoc = func(ctx context.Context, args []string) error {
190166
return command.Run(ctx, "protoc", args...)
191167
}

internal/librarian/java/generate_test.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -130,48 +130,6 @@ func TestResolveGAPICOptions(t *testing.T) {
130130
}
131131
}
132132

133-
func TestDeriveDistributionName(t *testing.T) {
134-
for _, test := range []struct {
135-
name string
136-
library *config.Library
137-
want string
138-
}{
139-
{
140-
name: "default case",
141-
library: &config.Library{Name: "secretmanager"},
142-
want: "com.google.cloud:google-cloud-secretmanager",
143-
},
144-
{
145-
name: "groupID override",
146-
library: &config.Library{
147-
Name: "secretmanager",
148-
Java: &config.JavaModule{GroupID: "com.custom"},
149-
},
150-
want: "com.custom:google-cloud-secretmanager",
151-
},
152-
{
153-
name: "distributionName override",
154-
library: &config.Library{
155-
Name: "secretmanager",
156-
Java: &config.JavaModule{DistributionNameOverride: "com.google.cloud:google-cloud-secretmanager-v1"},
157-
},
158-
want: "com.google.cloud:google-cloud-secretmanager-v1",
159-
},
160-
{
161-
name: "library name already has prefix",
162-
library: &config.Library{Name: "google-cloud-secretmanager"},
163-
want: "com.google.cloud:google-cloud-secretmanager",
164-
},
165-
} {
166-
t.Run(test.name, func(t *testing.T) {
167-
got := deriveDistributionName(test.library)
168-
if got != test.want {
169-
t.Errorf("deriveDistributionName() = %q, want %q", got, test.want)
170-
}
171-
})
172-
}
173-
}
174-
175133
func TestResolveGAPICOptions_MultipleConfigsError(t *testing.T) {
176134
for _, test := range []struct {
177135
name string
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 java
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/googleapis/librarian/internal/config"
22+
)
23+
24+
const (
25+
googleGroupID = "com.google"
26+
protoGrpcSuffix = ".api.grpc"
27+
cloudPrefix = "google-cloud-"
28+
grpcPrefix = "grpc-"
29+
protoPrefix = "proto-"
30+
)
31+
32+
var groupInclusions = map[string]bool{
33+
"com.google.cloud": true,
34+
"com.google.analytics": true,
35+
"com.google.area120": true,
36+
}
37+
38+
type coordinate struct {
39+
GroupID string
40+
ArtifactID string
41+
Version string
42+
}
43+
44+
type libCoord struct {
45+
gapic coordinate
46+
parent coordinate
47+
bom coordinate
48+
}
49+
50+
type apiCoord struct {
51+
libCoord
52+
proto coordinate
53+
grpc coordinate
54+
}
55+
56+
func deriveLibCoord(library *config.Library) libCoord {
57+
distName := deriveDistributionName(library)
58+
parts := strings.SplitN(distName, ":", 2)
59+
groupID := parts[0]
60+
artifactID := groupID
61+
if len(parts) == 2 {
62+
artifactID = parts[1]
63+
}
64+
gapic := coordinate{
65+
GroupID: groupID,
66+
ArtifactID: artifactID,
67+
Version: library.Version,
68+
}
69+
return libCoord{
70+
gapic: gapic,
71+
parent: coordinate{
72+
GroupID: gapic.GroupID,
73+
ArtifactID: fmt.Sprintf("%s-parent", gapic.ArtifactID),
74+
Version: gapic.Version,
75+
},
76+
bom: coordinate{
77+
GroupID: gapic.GroupID,
78+
ArtifactID: fmt.Sprintf("%s-bom", gapic.ArtifactID),
79+
Version: gapic.Version,
80+
},
81+
}
82+
}
83+
84+
func deriveAPICoord(lc libCoord, version string) apiCoord {
85+
protoGrpcGroupID := protoGroupID(lc.gapic.GroupID)
86+
return apiCoord{
87+
libCoord: lc,
88+
proto: coordinate{
89+
GroupID: protoGrpcGroupID,
90+
ArtifactID: fmt.Sprintf("%s%s-%s", protoPrefix, lc.gapic.ArtifactID, version),
91+
Version: lc.gapic.Version,
92+
},
93+
grpc: coordinate{
94+
GroupID: protoGrpcGroupID,
95+
ArtifactID: fmt.Sprintf("%s%s-%s", grpcPrefix, lc.gapic.ArtifactID, version),
96+
Version: lc.gapic.Version,
97+
},
98+
}
99+
}
100+
101+
// protoGroupID returns the Maven Group ID for the generated proto and gRPC
102+
// artifacts. It maps the GAPIC library's Group ID to a standard format and
103+
// checks for special cases in groupInclusions (e.g., mapping
104+
// "com.google.cloud" to "com.google.api.grpc").
105+
func protoGroupID(mainArtifactGroupID string) string {
106+
prefix := mainArtifactGroupID
107+
if groupInclusions[mainArtifactGroupID] {
108+
prefix = googleGroupID
109+
}
110+
return prefix + protoGrpcSuffix
111+
}
112+
113+
// ensureCloudPrefix returns name with the "google-cloud-" prefix,
114+
// adding it if not already present.
115+
func ensureCloudPrefix(name string) string {
116+
if !strings.HasPrefix(name, cloudPrefix) {
117+
return cloudPrefix + name
118+
}
119+
return name
120+
}
121+
122+
func deriveDistributionName(library *config.Library) string {
123+
if library.Java != nil && library.Java.DistributionNameOverride != "" {
124+
return library.Java.DistributionNameOverride
125+
}
126+
groupID := "com.google.cloud"
127+
if library.Java != nil && library.Java.GroupID != "" {
128+
groupID = library.Java.GroupID
129+
}
130+
artifactID := ensureCloudPrefix(library.Name)
131+
return fmt.Sprintf("%s:%s", groupID, artifactID)
132+
}

0 commit comments

Comments
 (0)