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