github.com/greenpau/go-authcrunch@v1.1.4/pkg/util/cfg/parser.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cfg 16 17 import ( 18 "github.com/greenpau/go-authcrunch/pkg/errors" 19 ) 20 21 const ( 22 // ReplErrStr represents error replacement string. 23 ReplErrStr string = "ERROR_REPLACEMENT" 24 ) 25 26 // ArgRule represents a rules that applies to an arguments passed 27 // in a config. 28 type ArgRule struct { 29 Min int `json:"min,omitempty" xml:"min,omitempty" yaml:"min,omitempty"` 30 Max int `json:"max,omitempty" xml:"max,omitempty" yaml:"max,omitempty"` 31 } 32 33 // ValidateArg performs argument validation. 34 func ValidateArg(rules map[string]*ArgRule, k string, v []string) error { 35 r, exists := rules[k] 36 if !exists { 37 return nil 38 } 39 if r.Min > len(v) { 40 return errors.ErrValidateArgTooFew.WithArgs(k, len(v), r.Min) 41 } 42 if r.Max < len(v) { 43 return errors.ErrValidateArgTooMany.WithArgs(k, len(v), r.Min) 44 } 45 return nil 46 } 47 48 // FindStrArr returns true if a string found in a slice. 49 func FindStrArr(arr []string, s string) bool { 50 for _, x := range arr { 51 if x == s { 52 return true 53 } 54 } 55 return false 56 } 57 58 // DedupStrArr returns deduplicated string array. 59 func DedupStrArr(arr []string) []string { 60 var output []string 61 m := make(map[string]interface{}) 62 for _, s := range arr { 63 if _, exists := m[s]; exists { 64 continue 65 } 66 m[s] = true 67 output = append(output, s) 68 } 69 return output 70 }