-
Notifications
You must be signed in to change notification settings - Fork 84
/
scopes.go
50 lines (43 loc) · 1.37 KB
/
scopes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package config
import (
"fmt"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
)
// GetRequiredScopes missing godoc
func (p *Provider) GetRequiredScopes(path string) ([]string, error) {
return p.getValues("scopes", path, true)
}
// GetRequiredGrantTypes missing godoc
func (p *Provider) GetRequiredGrantTypes(path string) ([]string, error) {
return p.getValues("grant_types", path, false)
}
func (p *Provider) getValues(valueType, path string, singeValueExpected bool) ([]string, error) {
val, err := p.getValueForJSONPath(path)
if err != nil {
if apperrors.IsValueNotFoundInConfiguration(err) {
return nil, apperrors.NewRequiredScopesNotDefinedError()
}
return nil, err
}
singleVal, ok := val.(string)
if ok && singeValueExpected {
return []string{singleVal}, nil
}
manyVals, ok := val.([]interface{})
if !ok {
errorMessageFormat := "unexpected %s definition, should be a list of strings, but was %T"
if singeValueExpected {
errorMessageFormat = "unexpected %s definition, should be string or list of strings, but was %T"
}
return nil, fmt.Errorf(errorMessageFormat, valueType, val)
}
scopes := make([]string, 0, len(manyVals))
for _, val := range manyVals {
strVal, ok := val.(string)
if !ok {
return nil, fmt.Errorf("unexpected %T value in a string list", val)
}
scopes = append(scopes, strVal)
}
return scopes, nil
}