/
ua.go
48 lines (38 loc) · 903 Bytes
/
ua.go
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
44
45
46
47
48
package useragent
import "strings"
type Parser struct {
Trie *RuneTrie
}
// Precedence is the order in which the user agent matched the
// browser, device, and OS. The lower the number, the higher the
// precedence.
type Precedence struct {
Browser uint8
OS uint8
Type uint8
}
type UserAgent struct {
Browser string
OS string
Version string
Desktop bool
Mobile bool
Tablet bool
TV bool
Bot bool
precedence Precedence
}
// Create a new Trie and populate it with user agent data.
func NewParser() *Parser {
trie := NewRuneTrie()
parser := &Parser{Trie: trie}
// For each newline in the file, add the user agent to the trie.
for _, ua := range strings.Split(userAgentsFile, "\n") {
parser.Trie.Put(ua)
}
return parser
}
// Parse a user agent string and return a UserAgent struct.
func (p *Parser) Parse(ua string) UserAgent {
return p.Trie.Get(ua)
}