@@ -20,6 +20,7 @@ import (
20
20
"sort"
21
21
"strings"
22
22
23
+ "oras.land/oras-go/v2/internal/maps"
23
24
"oras.land/oras-go/v2/internal/slices"
24
25
"oras.land/oras-go/v2/registry"
25
26
)
@@ -146,15 +147,16 @@ type scopesPerHostContextKey struct{}
146
147
//
147
148
// Reference: https://docs.docker.com/registry/spec/auth/scope/
148
149
func WithScopesPerHost (ctx context.Context , host string , scopes ... string ) context.Context {
149
- var regMap map [string ][]string
150
- var ok bool
151
- regMap , ok = ctx .Value (scopesPerHostContextKey {}).(map [string ][]string )
152
- if ! ok {
153
- regMap = make (map [string ][]string )
150
+ var scopesByHost map [string ][]string
151
+ if old , ok := ctx .Value (scopesPerHostContextKey {}).(map [string ][]string ); ok {
152
+ scopesByHost = make (map [string ][]string , len (old ))
153
+ maps .Copy (scopesByHost , old )
154
+ } else {
155
+ scopesByHost = make (map [string ][]string , 1 )
154
156
}
155
- scopes = CleanScopes ( scopes )
156
- regMap [host ] = scopes
157
- return context .WithValue (ctx , scopesPerHostContextKey {}, regMap )
157
+
158
+ scopesByHost [host ] = CleanScopes ( scopes )
159
+ return context .WithValue (ctx , scopesPerHostContextKey {}, scopesByHost )
158
160
}
159
161
160
162
// AppendScopesPerHost appends additional scopes to the existing scopes
@@ -171,8 +173,8 @@ func AppendScopesPerHost(ctx context.Context, host string, scopes ...string) con
171
173
172
174
// GetScopesPerHost returns the scopes in the context for the given host.
173
175
func GetScopesPerHost (ctx context.Context , host string ) []string {
174
- if regMap , ok := ctx .Value (scopesPerHostContextKey {}).(map [string ][]string ); ok {
175
- return slices .Clone (regMap [host ])
176
+ if scopesByHost , ok := ctx .Value (scopesPerHostContextKey {}).(map [string ][]string ); ok {
177
+ return slices .Clone (scopesByHost [host ])
176
178
}
177
179
return nil
178
180
}
0 commit comments