fix: Add db scanner methods for SpaceDelimitedArray (#194)

This commit is contained in:
David Sharnoff 2022-07-20 06:36:17 -07:00 committed by GitHub
parent 8dd5c87faa
commit 5fb36bf4c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 0 deletions

View file

@ -32,6 +32,11 @@ const (
ClientAssertionTypeJWTAssertion = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ClientAssertionTypeJWTAssertion = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
) )
var AllGrantTypes = []GrantType{
GrantTypeCode, GrantTypeRefreshToken, GrantTypeClientCredentials,
GrantTypeBearer, GrantTypeTokenExchange, GrantTypeImplicit,
ClientAssertionTypeJWTAssertion}
type GrantType string type GrantType string
type TokenRequest interface { type TokenRequest interface {

View file

@ -1,7 +1,9 @@
package oidc package oidc
import ( import (
"database/sql/driver"
"encoding/json" "encoding/json"
"fmt"
"strings" "strings"
"time" "time"
@ -95,6 +97,34 @@ func (s *SpaceDelimitedArray) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (s *SpaceDelimitedArray) Scan(src interface{}) error {
if src == nil {
*s = nil
return nil
}
switch v := src.(type) {
case string:
if len(v) == 0 {
*s = SpaceDelimitedArray{}
return nil
}
*s = strings.Split(v, " ")
case []byte:
if len(v) == 0 {
*s = SpaceDelimitedArray{}
return nil
}
*s = strings.Split(string(v), " ")
default:
return fmt.Errorf("cannot convert %T to SpaceDelimitedArray", src)
}
return nil
}
func (s SpaceDelimitedArray) Value() (driver.Value, error) {
return strings.Join(s, " "), nil
}
type Time time.Time type Time time.Time
func (t *Time) UnmarshalJSON(data []byte) error { func (t *Time) UnmarshalJSON(data []byte) error {

View file

@ -3,6 +3,8 @@ package oidc
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"strconv"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -228,6 +230,7 @@ func TestScopes_UnmarshalText(t *testing.T) {
}) })
} }
} }
func TestScopes_MarshalText(t *testing.T) { func TestScopes_MarshalText(t *testing.T) {
type args struct { type args struct {
scopes SpaceDelimitedArray scopes SpaceDelimitedArray
@ -294,3 +297,41 @@ func TestScopes_MarshalText(t *testing.T) {
}) })
} }
} }
func TestSpaceDelimitatedArray_ValuerNotNil(t *testing.T) {
inputs := [][]string{
{"two", "elements"},
{"one"},
{ /*zero*/ },
}
for _, input := range inputs {
t.Run(strconv.Itoa(len(input))+strings.Join(input, "_"), func(t *testing.T) {
sda := SpaceDelimitedArray(input)
dbValue, err := sda.Value()
if !assert.NoError(t, err, "Value") {
return
}
var reversed SpaceDelimitedArray
err = reversed.Scan(dbValue)
if assert.NoError(t, err, "Scan string") {
assert.Equal(t, sda, reversed, "scan string")
}
reversed = nil
dbValueString, ok := dbValue.(string)
if assert.True(t, ok, "dbValue is string") {
err = reversed.Scan([]byte(dbValueString))
if assert.NoError(t, err, "Scan bytes") {
assert.Equal(t, sda, reversed, "scan bytes")
}
}
})
}
}
func TestSpaceDelimitatedArray_ValuerNil(t *testing.T) {
var reversed SpaceDelimitedArray
err := reversed.Scan(nil)
if assert.NoError(t, err, "Scan nil") {
assert.Equal(t, SpaceDelimitedArray(nil), reversed, "scan nil")
}
}