Skip to content

Commit

Permalink
Merge pull request #277 from xmidt-org/feature/attributes-consistency
Browse files Browse the repository at this point in the history
renamed Attributes for consistency with other parts of the API
  • Loading branch information
johnabass authored Aug 13, 2024
2 parents 53b0414 + a287834 commit cf754b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
10 changes: 5 additions & 5 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package bascule

// Attributes is an optional interface that a Token may implement
// AttributesAccessor is an optional interface that a Token may implement
// that provides access to arbitrary key/value pairs.
type Attributes interface {
type AttributesAccessor interface {
// Get returns the value of an attribute, if it exists.
Get(key string) (any, bool)
}
Expand All @@ -19,7 +19,7 @@ type Attributes interface {
// type map[string]any or Attributes, this function will return false.
//
// If no keys are supplied, this function returns the zero value for T and false.
func GetAttribute[T any](a Attributes, keys ...string) (v T, ok bool) {
func GetAttribute[T any](a AttributesAccessor, keys ...string) (v T, ok bool) {
if len(keys) == 0 {
return
}
Expand All @@ -33,8 +33,8 @@ func GetAttribute[T any](a Attributes, keys ...string) (v T, ok bool) {
continue
}

var a Attributes
if a, ok = raw.(Attributes); ok {
var a AttributesAccessor
if a, ok = raw.(AttributesAccessor); ok {
raw, ok = a.Get(keys[i])
}
}
Expand Down
10 changes: 5 additions & 5 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type AttributesTestSuite struct {
suite.Suite
}

func (suite *AttributesTestSuite) testAttributes() Attributes {
func (suite *AttributesTestSuite) testAttributesAccessor() AttributesAccessor {
return testAttributes{
"value": 123,
"untypedNil": nil,
Expand All @@ -31,16 +31,16 @@ func (suite *AttributesTestSuite) testAttributes() Attributes {
"nestedMap": map[string]any{
"value": 123,
},
"nestedAttributes": Attributes(testAttributes{
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
}),
},
"nestedAttributes": Attributes(testAttributes{
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
"nestedMap": map[string]any{
"value": 123,
},
"nestedAttributes": Attributes(testAttributes{
"nestedAttributes": AttributesAccessor(testAttributes{
"value": 123,
}),
}),
Expand Down Expand Up @@ -117,7 +117,7 @@ func (suite *AttributesTestSuite) TestGetAttribute() {

for _, testCase := range testCases {
suite.Run(fmt.Sprintf("%v", testCase.keys), func() {
actual, ok := GetAttribute[int](suite.testAttributes(), testCase.keys...)
actual, ok := GetAttribute[int](suite.testAttributesAccessor(), testCase.keys...)
suite.Equal(testCase.expectedValue, actual)
suite.Equal(testCase.expectedOK, ok)
})
Expand Down
4 changes: 4 additions & 0 deletions basculejwt/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (t token) Capabilities() (caps []string) {
return
}

func (t token) Get(key string) (any, bool) {
return t.jwt.Get(key)
}

// tokenParser is the canonical parser for bascule that deals with JWTs.
// This parser does not use the source.
type tokenParser struct {
Expand Down
5 changes: 5 additions & 0 deletions basculejwt/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (suite *TokenTestSuite) TestTokenParser() {
suite.Equal(suite.capabilities, caps)
suite.True(ok)

suite.Require().Implements((*bascule.AttributesAccessor)(nil), token)
v, ok := bascule.GetAttribute[string](token.(bascule.AttributesAccessor), "version")
suite.True(ok)
suite.Equal(suite.version, v)

suite.Require().Implements((*Claims)(nil), token)
claims := token.(Claims)
suite.Equal(suite.audience, claims.Audience())
Expand Down

0 comments on commit cf754b0

Please sign in to comment.