-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
57 lines (52 loc) · 1.47 KB
/
select.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
49
50
51
52
53
54
55
56
57
package seltabl
import (
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
)
// SelectorI is an interface for running a goquery selector on a cellValue
//
// It is an interface that defines a Select method that takes a cellValue
// (goquery.Selection) and returns a string of the applied selection and an
// error.
type SelectorI interface {
Select(cellValue *goquery.Selection) (string, error)
}
// selector is a struct for running a goquery selector on a cellValue
//
// It is a struct that satisfies the SelectorInferface interface.
//
// It contains a control tag and a query selector.
type selector struct {
control string
query string
}
// Select runs the selector on the cellValue and sets the cellText
// and returns the cellText.
//
// It returns the output of running a selector and an error if the selector is
// not supported or fails.
func (s selector) Select(cellValue *goquery.Selection) (string, error) {
var cellText string
var exists bool
switch s.control {
case ctlInnerTextSelector:
cellText = cellValue.Text()
cellText = strings.TrimSpace(cellText)
if cellValue.Length() == 0 {
return "", fmt.Errorf("failed to find selector: %s", s.control)
}
case ctlAttrSelector:
cellText, exists = cellValue.Attr(s.query)
if !exists {
return "", fmt.Errorf("failed to find selector: %s", s.control)
}
default:
return "", fmt.Errorf(
"unsupported identifer: %s (identifers are %s)",
s.control,
strings.Join(cSels, " "),
)
}
return cellText, nil
}