Skip to content

Commit 9e6e0c7

Browse files
authored
feat(option/internaloption): add WithDefaultEndpointTemplate (#2313)
* Add DefaultEndpointTemplate to internal/settings.go * Deprecate internaloption.WithDefaultEndpoint refs: #2264
1 parent f01739e commit 9e6e0c7

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

internal/settings.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
type DialSettings struct {
2828
Endpoint string
2929
DefaultEndpoint string
30+
DefaultEndpointTemplate string
3031
DefaultMTLSEndpoint string
3132
Scopes []string
3233
DefaultScopes []string

option/internaloption/internaloption.go

+19
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ func (o defaultEndpointOption) Apply(settings *internal.DialSettings) {
2222
// It should only be used internally by generated clients.
2323
//
2424
// This is similar to WithEndpoint, but allows us to determine whether the user has overridden the default endpoint.
25+
//
26+
// Deprecated: WithDefaultEndpoint does not support setting the universe domain.
27+
// Use WithDefaultEndpointTemplate and WithDefaultUniverseDomain to compose the
28+
// default endpoint instead.
2529
func WithDefaultEndpoint(url string) option.ClientOption {
2630
return defaultEndpointOption(url)
2731
}
2832

33+
type defaultEndpointTemplateOption string
34+
35+
func (o defaultEndpointTemplateOption) Apply(settings *internal.DialSettings) {
36+
settings.DefaultEndpointTemplate = string(o)
37+
}
38+
39+
// WithDefaultEndpointTemplate provides a template for creating the endpoint
40+
// using a universe domain. See also WithDefaultUniverseDomain and
41+
// option.WithUniverseDomain.
42+
//
43+
// It should only be used internally by generated clients.
44+
func WithDefaultEndpointTemplate(url string) option.ClientOption {
45+
return defaultEndpointTemplateOption(url)
46+
}
47+
2948
type defaultMTLSEndpointOption string
3049

3150
func (o defaultMTLSEndpointOption) Apply(settings *internal.DialSettings) {

option/internaloption/internaloption_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestWithCredentials(t *testing.T) {
3535
func TestDefaultApply(t *testing.T) {
3636
opts := []option.ClientOption{
3737
WithDefaultEndpoint("https://example.com:443"),
38+
WithDefaultEndpointTemplate("https://foo.%s/"),
3839
WithDefaultMTLSEndpoint("http://mtls.example.com:445"),
3940
WithDefaultScopes("a"),
4041
WithDefaultUniverseDomain("foo.com"),
@@ -45,11 +46,12 @@ func TestDefaultApply(t *testing.T) {
4546
opt.Apply(&got)
4647
}
4748
want := internal.DialSettings{
48-
DefaultScopes: []string{"a"},
49-
DefaultEndpoint: "https://example.com:443",
50-
DefaultUniverseDomain: "foo.com",
51-
DefaultAudience: "audience",
52-
DefaultMTLSEndpoint: "http://mtls.example.com:445",
49+
DefaultScopes: []string{"a"},
50+
DefaultEndpoint: "https://example.com:443",
51+
DefaultEndpointTemplate: "https://foo.%s/",
52+
DefaultUniverseDomain: "foo.com",
53+
DefaultAudience: "audience",
54+
DefaultMTLSEndpoint: "http://mtls.example.com:445",
5355
}
5456
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")) {
5557
t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")))

0 commit comments

Comments
 (0)