code.dwrz.net

Go monorepo.
Log | Files | Refs

header_rules.go (1893B)


      1 package v4
      2 
      3 import (
      4 	sdkstrings "github.com/aws/aws-sdk-go-v2/internal/strings"
      5 )
      6 
      7 // Rules houses a set of Rule needed for validation of a
      8 // string value
      9 type Rules []Rule
     10 
     11 // Rule interface allows for more flexible rules and just simply
     12 // checks whether or not a value adheres to that Rule
     13 type Rule interface {
     14 	IsValid(value string) bool
     15 }
     16 
     17 // IsValid will iterate through all rules and see if any rules
     18 // apply to the value and supports nested rules
     19 func (r Rules) IsValid(value string) bool {
     20 	for _, rule := range r {
     21 		if rule.IsValid(value) {
     22 			return true
     23 		}
     24 	}
     25 	return false
     26 }
     27 
     28 // MapRule generic Rule for maps
     29 type MapRule map[string]struct{}
     30 
     31 // IsValid for the map Rule satisfies whether it exists in the map
     32 func (m MapRule) IsValid(value string) bool {
     33 	_, ok := m[value]
     34 	return ok
     35 }
     36 
     37 // AllowList is a generic Rule for include listing
     38 type AllowList struct {
     39 	Rule
     40 }
     41 
     42 // IsValid for AllowList checks if the value is within the AllowList
     43 func (w AllowList) IsValid(value string) bool {
     44 	return w.Rule.IsValid(value)
     45 }
     46 
     47 // ExcludeList is a generic Rule for exclude listing
     48 type ExcludeList struct {
     49 	Rule
     50 }
     51 
     52 // IsValid for AllowList checks if the value is within the AllowList
     53 func (b ExcludeList) IsValid(value string) bool {
     54 	return !b.Rule.IsValid(value)
     55 }
     56 
     57 // Patterns is a list of strings to match against
     58 type Patterns []string
     59 
     60 // IsValid for Patterns checks each pattern and returns if a match has
     61 // been found
     62 func (p Patterns) IsValid(value string) bool {
     63 	for _, pattern := range p {
     64 		if sdkstrings.HasPrefixFold(value, pattern) {
     65 			return true
     66 		}
     67 	}
     68 	return false
     69 }
     70 
     71 // InclusiveRules rules allow for rules to depend on one another
     72 type InclusiveRules []Rule
     73 
     74 // IsValid will return true if all rules are true
     75 func (r InclusiveRules) IsValid(value string) bool {
     76 	for _, rule := range r {
     77 		if !rule.IsValid(value) {
     78 			return false
     79 		}
     80 	}
     81 	return true
     82 }