Skip to content

Commit 496ccd0

Browse files
authored
Simplify Service Client Configuration Resolving (#478)
1 parent c6f8d25 commit 496ccd0

File tree

7 files changed

+54
-43
lines changed

7 files changed

+54
-43
lines changed

aws/config.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,9 @@ type Config struct {
7979
// for testing that do not support the modeled host prefix pattern.
8080
DisableEndpointHostPrefix bool
8181

82-
// ConfigResolver defines how additional configuration can be loaded by clients.
83-
AdditionalConfig ConfigResolver
84-
}
85-
86-
// ConfigResolver is an interface that encapsulates the behavior of loading
87-
// additional configuration.
88-
type ConfigResolver interface {
89-
// ResolveConfig calls the provide function passing a slice of configuration sources
90-
ResolveConfig(func(configs []interface{}) error) error
82+
// ConfigSources are the sources that were used to construct the Config.
83+
// Allows for additional configuration can be loaded by clients.
84+
ConfigSources []interface{}
9185
}
9286

9387
// NewConfig returns a new Config pointer that can be chained with builder

aws/external/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ func (cs Configs) ResolveAWSConfig(resolvers []AWSConfigResolver) (aws.Config, e
9797
}
9898
}
9999

100+
var sources []interface{}
101+
for _, s := range cs {
102+
sources = append(sources, s)
103+
}
104+
cfg.ConfigSources = sources
105+
100106
return cfg, nil
101107
}
102108

aws/external/config_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ func TestConfigs_AppendFromLoaders(t *testing.T) {
7676
}
7777

7878
func TestConfigs_ResolveAWSConfig(t *testing.T) {
79-
cfg, err := Configs{
79+
configSources := Configs{
8080
WithRegion("mock-region"),
8181
WithCredentialsValue(aws.Credentials{
8282
AccessKeyID: "AKID", SecretAccessKey: "SECRET",
8383
Source: "provider",
8484
}),
85-
}.ResolveAWSConfig([]AWSConfigResolver{
85+
}
86+
87+
cfg, err := configSources.ResolveAWSConfig([]AWSConfigResolver{
8688
ResolveRegion,
8789
ResolveCredentialsValue,
8890
})
@@ -101,4 +103,13 @@ func TestConfigs_ResolveAWSConfig(t *testing.T) {
101103
if e, a := "provider", creds.Source; e != a {
102104
t.Errorf("expect %v provider, got %v", e, a)
103105
}
106+
107+
var expectedSources []interface{}
108+
for _, s := range cfg.ConfigSources {
109+
expectedSources = append(expectedSources, s)
110+
}
111+
112+
if e, a := expectedSources, cfg.ConfigSources; !reflect.DeepEqual(e, a) {
113+
t.Errorf("expected %v, got %v", e, a)
114+
}
104115
}

private/model/api/api.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,8 @@ func New(config aws.Config) *{{ .StructName }} {
699699
}
700700
701701
{{ if .HasExternalClientConfigFields }}
702-
if config.AdditionalConfig != nil {
703-
if err := config.AdditionalConfig.ResolveConfig(resolveClientConfig(svc)); err != nil {
704-
panic(fmt.Errorf("failed to resolve service configuration: %v", err))
705-
}
702+
if err := resolveClientConfig(svc, config.ConfigSources); err != nil {
703+
panic(fmt.Errorf("failed to resolve service configuration: %v", err))
706704
}
707705
{{- end }}
708706
@@ -1130,18 +1128,20 @@ func (a *API) hasNonIOShapes() bool {
11301128
var tplClientConfig = template.Must(template.New("tplClientConfig").Funcs(map[string]interface{}{
11311129
"ExternalConfigFields": externalConfigFields,
11321130
}).Parse(`
1133-
func resolveClientConfig(svc *{{ .StructName }}) func(configs []interface{}) error {
1134-
return func(configs []interface{}) error {
1135-
{{- $fields := ExternalConfigFields . -}}
1136-
{{- range $idx, $field := $fields }}
1137-
if value, ok, err := {{ $.InternalConfigResolverPackageName }}.{{ $field.ExternalConfigResolverName }}(configs); err != nil {
1138-
return err
1139-
} else if ok {
1140-
svc.{{ $field.Name }} = value
1141-
}
1142-
{{ end }}
1131+
func resolveClientConfig(svc *{{ .StructName }}, configs []interface{}) error {
1132+
if len(configs) == 0 {
11431133
return nil
11441134
}
1135+
1136+
{{ $fields := ExternalConfigFields . -}}
1137+
{{- range $idx, $field := $fields }}
1138+
if value, ok, err := {{ $.InternalConfigResolverPackageName }}.{{ $field.ExternalConfigResolverName }}(configs); err != nil {
1139+
return err
1140+
} else if ok {
1141+
svc.{{ $field.Name }} = value
1142+
}
1143+
{{ end }}
1144+
return nil
11451145
}
11461146
`))
11471147

@@ -1188,7 +1188,7 @@ func TestExternalConfigResolver(t *testing.T) {
11881188
t.Run("{{ $field.Name }}", func(t *testing.T) {
11891189
t.Run("value not found", func(t *testing.T) {
11901190
svc := &{{ $.StructName }}{}
1191-
err := resolveClientConfig(svc)([]interface{}{
1191+
err := resolveClientConfig(svc, []interface{}{
11921192
mock{{ $field.Name }}(func() (value {{ $field.Type }}, ok bool, err error) {
11931193
return value, ok, err
11941194
}),
@@ -1202,7 +1202,7 @@ func TestExternalConfigResolver(t *testing.T) {
12021202
})
12031203
t.Run("resolve error", func(t *testing.T) {
12041204
svc := &{{ $.StructName }}{}
1205-
err := resolveClientConfig(svc)([]interface{}{
1205+
err := resolveClientConfig(svc, []interface{}{
12061206
mock{{ $field.Name }}(func() (value {{ $field.Type }}, ok bool, err error) {
12071207
err = fmt.Errorf("resolve error")
12081208
return value, ok, err
@@ -1218,7 +1218,7 @@ func TestExternalConfigResolver(t *testing.T) {
12181218
t.Run("value found", func(t *testing.T) {
12191219
{{- $mockValue := MockConfigFieldValue $field -}}
12201220
svc := &{{ $.StructName }}{}
1221-
err := resolveClientConfig(svc)([]interface{}{
1221+
err := resolveClientConfig(svc, []interface{}{
12221222
mock{{ $field.Name }}(func() (value {{ $field.Type }}, ok bool, err error) {
12231223
value = {{ $mockValue }}
12241224
return value, true, err

service/s3/api_client.go

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/s3/api_client_config.go

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/s3/api_client_config_test.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)