2019-05-01 08:29:02 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// checkAndFormat checks for errors and missing fields and sets default values for optional fields.
|
|
|
|
func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) error {
|
|
|
|
|
|
|
|
// 1. fail on missing description
|
|
|
|
if len(methodDef.Description) < 1 {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrMissingMethodDesc.WrapString(httpMethod + " " + servicePath)
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. stop if no parameter
|
|
|
|
if methodDef.Parameters == nil || len(methodDef.Parameters) < 1 {
|
|
|
|
methodDef.Parameters = make(map[string]*Parameter, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. for each parameter
|
|
|
|
for pName, pData := range methodDef.Parameters {
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.1. check name
|
2019-05-01 08:29:02 +00:00
|
|
|
if strings.Trim(pName, "_") != pName {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrIllegalParamName.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(pData.Rename) < 1 {
|
|
|
|
pData.Rename = pName
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.2. Check for name/rename conflict
|
2019-05-01 08:29:02 +00:00
|
|
|
for paramName, param := range methodDef.Parameters {
|
|
|
|
|
|
|
|
// ignore self
|
|
|
|
if pName == paramName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.2.1. Same rename field
|
2019-05-01 08:29:02 +00:00
|
|
|
if pData.Rename == param.Rename {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pData.Rename + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.2.2. Not-renamed field matches a renamed field
|
2019-05-01 08:29:02 +00:00
|
|
|
if pName == param.Rename {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.2.3. Renamed field matches name
|
2019-05-01 08:29:02 +00:00
|
|
|
if pData.Rename == paramName {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.3. Fail on missing description
|
2019-05-01 08:29:02 +00:00
|
|
|
if len(pData.Description) < 1 {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrMissingParamDesc.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.4. Manage invalid type
|
2019-05-01 08:29:02 +00:00
|
|
|
if len(pData.Type) < 1 {
|
2019-11-21 20:02:48 +00:00
|
|
|
return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
|
2019-05-01 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:02:48 +00:00
|
|
|
// 3.5. Set optional + type
|
2019-05-01 08:29:02 +00:00
|
|
|
if pData.Type[0] == '?' {
|
|
|
|
pData.Optional = true
|
|
|
|
pData.Type = pData.Type[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// scopeHasPermission returns whether the permission fulfills a given scope
|
|
|
|
func scopeHasPermission(permission string, scope []string) bool {
|
|
|
|
for _, s := range scope {
|
|
|
|
if permission == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|