package s3notification import "strings" // Match is a rule an event matched: the record's configurationId and the // target it is published to. type Match struct { ID string Target string } // Match returns the rules that cover eventName (such as s3:ObjectCreated:Put) // for the object key. ParseConfiguration refuses overlapping rules, as AWS // does, so at most one rule matches. func (c *Configuration) Match(eventName, key string) []Match { if c == nil { return nil } var matches []Match for i := range c.Rules { rule := &c.Rules[i] if rule.matches(eventName, key) { matches = append(matches, Match{ID: rule.ID, Target: rule.Target}) } } return matches } func (r *Rule) matches(eventName, key string) bool { if !strings.HasPrefix(key, r.Prefix) || !strings.HasSuffix(key, r.Suffix) { return false } for _, configured := range r.Events { if eventMatches(configured, eventName) { return true } } return false } // overlaps reports whether one event for one key could match both rules. func (r *Rule) overlaps(o *Rule) bool { if !strings.HasPrefix(r.Prefix, o.Prefix) && !strings.HasPrefix(o.Prefix, r.Prefix) { return false } if !strings.HasSuffix(r.Suffix, o.Suffix) && !strings.HasSuffix(o.Suffix, r.Suffix) { return false } for _, a := range r.Events { for _, b := range o.Events { if a == b || eventMatches(a, b) || eventMatches(b, a) { return true } } } return false } // eventMatches reports whether a configured event, possibly a category // wildcard such as s3:ObjectCreated:*, covers eventName. func eventMatches(configured, eventName string) bool { if category, ok := strings.CutSuffix(configured, "*"); ok { return len(eventName) > len(category) && strings.HasPrefix(eventName, category) } return configured == eventName }