Skip to content

Commit 9d6cf74

Browse files
committed
Update NewAccessKey to return a boolean for success and adjust tests accordingly
1 parent 7dc2de7 commit 9d6cf74

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
@@ -14,7 +14,8 @@ func TestAccessKeyEncoding(t *testing.T) {
1414
t.Run("v0", func(t *testing.T) {
1515
ctx := authcontrol.WithVersion(context.Background(), 0)
1616
projectID := uint64(12345)
17-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
17+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
18+
require.True(t, ok)
1819
t.Log("=> k", accessKey)
1920

2021
outID, err := accessKey.GetProjectID()
@@ -25,7 +26,8 @@ func TestAccessKeyEncoding(t *testing.T) {
2526
t.Run("v1", func(t *testing.T) {
2627
ctx := authcontrol.WithVersion(context.Background(), 1)
2728
projectID := uint64(12345)
28-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
29+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
30+
require.True(t, ok)
2931
t.Log("=> k", accessKey)
3032
outID, err := accessKey.GetProjectID()
3133
require.NoError(t, err)
@@ -34,15 +36,17 @@ func TestAccessKeyEncoding(t *testing.T) {
3436
t.Run("v2", func(t *testing.T) {
3537
ctx := authcontrol.WithVersion(context.Background(), 2)
3638
projectID := uint64(12345)
37-
accessKey := authcontrol.NewAccessKey(ctx, projectID)
39+
accessKey, ok := authcontrol.NewAccessKey(ctx, projectID)
40+
require.True(t, ok)
3841
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
3942
outID, err := accessKey.GetProjectID()
4043
require.NoError(t, err)
4144
require.Equal(t, projectID, outID)
4245

4346
ctx = authcontrol.WithPrefix(ctx, "newprefix:dev")
4447

45-
accessKey2 := authcontrol.NewAccessKey(ctx, projectID)
48+
accessKey2, ok := authcontrol.NewAccessKey(ctx, projectID)
49+
require.True(t, ok)
4650
t.Log("=> k", accessKey2, "| prefix =>", accessKey2.GetPrefix())
4751
outID, err = accessKey2.GetProjectID()
4852
require.NoError(t, err)
@@ -56,7 +60,8 @@ func TestAccessKeyEncoding(t *testing.T) {
5660

5761
func TestDecode(t *testing.T) {
5862
ctx := authcontrol.WithVersion(context.Background(), 2)
59-
accessKey := authcontrol.NewAccessKey(ctx, 237)
63+
accessKey, ok := authcontrol.NewAccessKey(ctx, 237)
64+
require.True(t, ok)
6065
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
6166
}
6267

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)