summaryrefslogtreecommitdiff
path: root/events/wildcard.go
blob: b4c28ae142f0ad791284417f6eea1b383327ffa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package events

import (
	"strings"

	"github.com/spiral/errors"
)

type wildcard struct {
	prefix string
	suffix string
}

func newWildcard(pattern string) (*wildcard, error) {
	// Normalize
	origin := strings.ToLower(pattern)
	i := strings.IndexByte(origin, '*')

	/*
		http.*
		*
		*.WorkerError
	*/
	if i == -1 {
		dotI := strings.IndexByte(pattern, '.')

		if dotI == -1 {
			// http.SuperEvent
			return nil, errors.Str("wrong wildcard, no * or . Usage: http.Event or *.Event or http.*")
		}

		return &wildcard{origin[0:dotI], origin[dotI+1:]}, nil
	}

	// pref: http.
	// suff: *
	return &wildcard{origin[0:i], origin[i+1:]}, nil
}

func (w wildcard) match(s string) bool {
	s = strings.ToLower(s)
	return len(s) >= len(w.prefix)+len(w.suffix) && strings.HasPrefix(s, w.prefix) && strings.HasSuffix(s, w.suffix)
}