Skip to content

Commit ab6b65d

Browse files
committed
Update NewAccessKey to return a boolean for success and adjust tests accordingly
1 parent afb30a4 commit ab6b65d

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

access_key.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ func (a AccessKey) GetPrefix() string {
4949
return strings.Join(parts[:len(parts)-1], Separator)
5050
}
5151

52-
func NewAccessKey(ctx context.Context, projectID uint64) AccessKey {
52+
func NewAccessKey(ctx context.Context, projectID uint64) (AccessKey, bool) {
5353
version, ok := GetVersion(ctx)
5454
if !ok {
55-
return DefaultEncoding.Encode(ctx, projectID)
55+
return DefaultEncoding.Encode(ctx, projectID), true
5656
}
5757

5858
for _, e := range SupportedEncodings {
5959
if e.Version() == version {
60-
return e.Encode(ctx, projectID)
60+
return e.Encode(ctx, projectID), true
6161
}
6262
}
63-
return ""
63+
return "", false
6464
}
6565

6666
type Encoding interface {

access_key_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ func TestAccessKeyEncoding(t *testing.T) {
1212
t.Run("v0", func(t *testing.T) {
1313
ctx := authcontrol.WithVersion(context.Background(), 0)
1414
projectID := uint64(12345)
15-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
15+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
16+
require.True(t, ok)
1617
t.Log("=> k", accessKey)
1718

1819
outID, err := accessKey.GetProjectID()
@@ -23,7 +24,8 @@ func TestAccessKeyEncoding(t *testing.T) {
2324
t.Run("v1", func(t *testing.T) {
2425
ctx := authcontrol.WithVersion(context.Background(), 1)
2526
projectID := uint64(12345)
26-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
27+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
28+
require.True(t, ok)
2729
t.Log("=> k", accessKey)
2830
outID, err := accessKey.GetProjectID()
2931
require.NoError(t, err)
@@ -32,15 +34,17 @@ func TestAccessKeyEncoding(t *testing.T) {
3234
t.Run("v2", func(t *testing.T) {
3335
ctx := authcontrol.WithVersion(context.Background(), 2)
3436
projectID := uint64(12345)
35-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
37+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
38+
require.True(t, ok)
3639
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
3740
outID, err := accessKey.GetProjectID()
3841
require.NoError(t, err)
3942
require.Equal(t, projectID, outID)
4043

4144
ctx = authcontrol.WithPrefix(ctx, "newprefix:dev")
4245

43-
accessKey2 := authcontrol.NewAccessKey(ctx, projectID)
46+
accessKey2, ok := authcontrol.NewAccessKey(ctx, projectID)
47+
require.True(t, ok)
4448
t.Log("=> k", accessKey2, "| prefix =>", accessKey2.GetPrefix())
4549
outID, err = accessKey2.GetProjectID()
4650
require.NoError(t, err)
@@ -54,6 +58,7 @@ func TestAccessKeyEncoding(t *testing.T) {
5458

5559
func TestDecode(t *testing.T) {
5660
ctx := authcontrol.WithVersion(context.Background(), 2)
57-
accessKey := authcontrol.NewAccessKey(ctx, 237)
61+
accessKey, ok := authcontrol.NewAccessKey(ctx, 237)
62+
require.True(t, ok)
5863
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
5964
}

go.work.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtX
1515
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
1616
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
1717
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
18+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
1819
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
1920
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
2021
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

0 commit comments

Comments
 (0)