From 746e9541d1e0562fe55ae1c02e713605d1627abd Mon Sep 17 00:00:00 2001 From: Dzung Tran Date: Thu, 12 May 2022 14:03:43 +0700 Subject: [PATCH] fix goreportcard issues --- pkg/authz/opa.go | 16 ++-------------- pkg/utils/strings.go | 31 ++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/pkg/authz/opa.go b/pkg/authz/opa.go index 56a36c1..65ebb0a 100644 --- a/pkg/authz/opa.go +++ b/pkg/authz/opa.go @@ -205,10 +205,8 @@ func CheckPolicies(user *domains.UserWithRoles, callOpts ...CallOPAInputOption) "user": user, } - // Apply options to input - if opts.RequestMethod != "" && opts.RequestEndpoint != "" { - input["method"] = opts.RequestMethod - input["endpoint"] = opts.RequestEndpoint + if opts.Org != nil { + input["org"] = opts.Org } if len(opts.ExtraData) > 0 { @@ -223,16 +221,6 @@ func CheckPolicies(user *domains.UserWithRoles, callOpts ...CallOPAInputOption) input["endpoint"] = opts.RequestEndpoint } - if opts.Org != nil { - input["org"] = opts.Org - } - - if len(opts.ExtraData) > 0 { - for k, v := range opts.ExtraData { - input[k] = v - } - } - // Run evaluation. rs, err := preparedQuery.Eval(ctx, rego.EvalInput(input)) if err != nil { diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 4194b9d..33df79b 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -112,35 +112,44 @@ func ToScreamingDelimited(s string, del uint8, screaming bool) string { // treat acronyms as words, eg for JSONData -> JSON is a whole word nextCaseIsChanged := false if i+1 < len(s) { - next := s[i+1] - if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') { - nextCaseIsChanged = true - } + next := rune(s[i+1]) + nextCaseIsChanged = (isRuneInRange(v, 'A', 'Z') && isRuneInRange(next, 'a', 'z')) || + (isRuneInRange(v, 'a', 'z') && isRuneInRange(next, 'A', 'Z')) } if i > 0 && n[len(n)-1] != del && nextCaseIsChanged { // add underscore if next letter case type is changed - if v >= 'A' && v <= 'Z' { + if isRuneInRange(v, 'A', 'Z') { n += string(del) + string(v) - } else if v >= 'a' && v <= 'z' { + } + + if isRuneInRange(v, 'a', 'z') { n += string(v) + string(del) } - } else if v == ' ' || v == '_' || v == '-' { + continue + } + + if v == ' ' || v == '_' || v == '-' { // replace spaces/underscores with delimiters n += string(del) - } else { - n = n + string(v) + continue } + + n = n + string(v) } + n = strings.ToLower(n) if screaming { n = strings.ToUpper(n) - } else { - n = strings.ToLower(n) } + return n } +func isRuneInRange(chr rune, fromRune rune, toRune rune) bool { + return chr >= fromRune && chr <= toRune +} + // UcFirst Upper case first character func UcFirst(str string) string { r := []rune(str)