Skip to content

Commit

Permalink
resource_container: add explicit Type field for ResourceContainer
Browse files Browse the repository at this point in the history
Extend the resource container object to have an explicit `Type` instead of inferring/manipulating from the `Level`.
  • Loading branch information
jacobbednarz committed Jul 5, 2023
1 parent c662bc9 commit 0610f24
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/1325.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource_container: expose `Type` on `*ResourceContainer` to explicitly denote what type of resource it is instead of inferring from `Level`.
```
42 changes: 41 additions & 1 deletion resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ package cloudflare

import "fmt"

// RouteLevel holds the "level" where the resource resides.
// RouteLevel holds the "level" where the resource resides. Commonly used in
// routing configurations or builders.
type RouteLevel string

// ResourceType holds the type of the resource. This is similar to `RouteLevel`
// however this is the singular version of `RouteLevel` and isn't suitable for
// use in routing.
type ResourceType string

const (
AccountRouteLevel RouteLevel = "accounts"
ZoneRouteLevel RouteLevel = "zones"
UserRouteLevel RouteLevel = "user"

AccountType ResourceType = "account"
ZoneType ResourceType = "zone"
UserType ResourceType = "user"
)

// ResourceContainer defines an API resource you wish to target. Should not be
Expand All @@ -17,6 +27,33 @@ const (
type ResourceContainer struct {
Level RouteLevel
Identifier string
Type ResourceType
}

func (r RouteLevel) String() string {
switch r {
case AccountRouteLevel:
return "accounts"
case ZoneRouteLevel:
return "zones"
case UserRouteLevel:
return "user"

Check failure on line 40 in resource.go

View workflow job for this annotation

GitHub Actions / lint

string `user` has 3 occurrences, but such constant `UserType` already exists (goconst)
default:
return "unknown"
}
}

func (r ResourceType) String() string {
switch r {
case AccountType:
return "account"
case ZoneType:
return "zone"
case UserType:
return "user"
default:
return "unknown"
}
}

// Returns a URL fragment of the endpoint scoped by the container.
Expand Down Expand Up @@ -47,6 +84,7 @@ func UserIdentifier(id string) *ResourceContainer {
return &ResourceContainer{
Level: UserRouteLevel,
Identifier: id,
Type: UserType,
}
}

Expand All @@ -55,6 +93,7 @@ func ZoneIdentifier(id string) *ResourceContainer {
return &ResourceContainer{
Level: ZoneRouteLevel,
Identifier: id,
Type: ZoneType,
}
}

Expand All @@ -63,5 +102,6 @@ func AccountIdentifier(id string) *ResourceContainer {
return &ResourceContainer{
Level: AccountRouteLevel,
Identifier: id,
Type: AccountType,
}
}
38 changes: 38 additions & 0 deletions resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import (
"github.com/stretchr/testify/assert"
)

func TestResourceProperties(t *testing.T) {
testCases := map[string]struct {
container *ResourceContainer
expectedRoute string
expectedType string
expectedIdentifier string
}{
"account": {
container: AccountIdentifier("abcd1234"),
expectedRoute: "accounts",
expectedType: "account",
expectedIdentifier: "abcd1234",
},
"zone": {
container: ZoneIdentifier("abcd1234"),
expectedRoute: "zones",
expectedType: "zone",
expectedIdentifier: "abcd1234",
},
"user": {
container: UserIdentifier("abcd1234"),
expectedRoute: "user",
expectedType: "user",
expectedIdentifier: "abcd1234",
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
setup()
defer teardown()

assert.Equal(t, tc.container.Level.String(), tc.expectedRoute)
assert.Equal(t, tc.container.Type.String(), tc.expectedType)
assert.Equal(t, tc.container.Identifier, tc.expectedIdentifier)
})
}
}
func TestResourcURLFragment(t *testing.T) {
tests := map[string]struct {
container *ResourceContainer
Expand Down

0 comments on commit 0610f24

Please sign in to comment.