mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-01 10:00:19 +08:00
feat: support custom user mapping (#2029)
* feat: support custom user mapping * fix: parse id to string * Update data.json * Update data.json --------- Co-authored-by: hsluoyz <hsluoyz@qq.com>
This commit is contained in:
@ -289,3 +289,18 @@ func HasString(strs []string, str string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ParseIdToString(input interface{}) (string, error) {
|
||||
switch v := input.(type) {
|
||||
case string:
|
||||
return v, nil
|
||||
case int:
|
||||
return strconv.Itoa(v), nil
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported id type: %T", input)
|
||||
}
|
||||
}
|
||||
|
@ -246,3 +246,23 @@ func TestSnakeString(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseId(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
description string
|
||||
input interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{"Should be return 123456", "123456", "123456"},
|
||||
{"Should be return 123456", 123456, "123456"},
|
||||
{"Should be return 123456", int64(123456), "123456"},
|
||||
{"Should be return 123456", float64(123456), "123456"},
|
||||
}
|
||||
for _, scenery := range scenarios {
|
||||
t.Run(scenery.description, func(t *testing.T) {
|
||||
actual, err := ParseIdToString(scenery.input)
|
||||
assert.Nil(t, err, "The returned value not is expected")
|
||||
assert.Equal(t, scenery.expected, actual, "The returned value not is expected")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user