Skip to content

implement flag syntax and "optional" flag #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import (

type (
path struct {
flags []string
selector string
acc string
parsers []parser
loader *loader
}
)

func (p *path) hasFlag(flag string) bool {
for _, v := range p.flags {
if v == flag {
return true
}
}

return false
}

const (
accessorAttr = "attr"
accessorHTML = "html"
Expand Down Expand Up @@ -72,7 +83,22 @@ func parseFunctionSignature(s string) (string, string) {

func parseTag(tag reflect.StructTag) (*path, error) {
p := &path{}
for i, part := range strings.Split(tag.Get("sq"), " | ") {

s := tag.Get("sq")

if len(s) > 0 && s[0] == '(' {
if end := strings.Index(s, ")"); end != -1 {
for _, e := range strings.Split(s[1:end], ",") {
if e = strings.TrimSpace(e); e != "" {
p.flags = append(p.flags, e)
}
}

s = strings.TrimLeft(s[end:], ") ")
}
}

for i, part := range strings.Split(s, " | ") {
part = strings.TrimSpace(part)
switch i {
case 0:
Expand Down
17 changes: 17 additions & 0 deletions extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func TestParseTag(t *testing.T) {
},
nil,
},
{`sq:"() p"`, &path{flags: []string{}, selector: "p"}, nil},
{`sq:"( ) p"`, &path{flags: []string{}, selector: "p"}, nil},
{`sq:"(opt1) p"`, &path{flags: []string{"opt1"}, selector: "p"}, nil},
{`sq:"( opt1 ) p"`, &path{flags: []string{"opt1"}, selector: "p"}, nil},
{`sq:"(opt1,opt2) p"`, &path{flags: []string{"opt1", "opt2"}, selector: "p"}, nil},
{`sq:"( opt1 , opt2 ) p"`, &path{flags: []string{"opt1", "opt2"}, selector: "p"}, nil},
{`sq:"(opt1,opt2,opt3) p"`, &path{flags: []string{"opt1", "opt2", "opt3"}, selector: "p"}, nil},
{`sq:"( opt1 ,opt2, opt3 ) p"`, &path{flags: []string{"opt1", "opt2", "opt3"}, selector: "p"}, nil},

// bad
{`sq:"p.last | fuzzy"`, nil, fmt.Errorf("Bad accessor: %q", `fuzzy`)},
Expand All @@ -136,6 +144,15 @@ func TestParseTag(t *testing.T) {
}
continue
}
if len(p.flags) != len(test.p.flags) {
t.Errorf("Expected %#v, got %#v", test.p.flags, p.flags)
} else {
for i := 0; i < len(p.flags); i++ {
if p.flags[i] != test.p.flags[i] {
t.Errorf("Expected %#v, got %#v", test.p.flags, p.flags)
}
}
}
if p.selector != test.p.selector {
t.Errorf("Expected %q, got %q", test.p.selector, p.selector)
}
Expand Down
4 changes: 4 additions & 0 deletions sq.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func hydrateValue(v *reflect.Value, sel *goquery.Selection, p *path) []error {
if p != nil && len(p.selector) > 0 && p.selector != "." && !sel.Is(p.selector) {
sel = sel.Find(p.selector)
if sel.Size() == 0 {
if p.hasFlag("optional") {
return nil
}

return []error{fmt.Errorf("%q did not match", p.selector)}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type (
Stylesheet *css.Stylesheet `sq:"style:first-of-type"`
Stylesheets []*css.Stylesheet `sq:"style"`
CustomType CustomType `sq:"p.string"`
Optional string `sq:"(optional) blink"`

// errs
Map map[string]interface{} `sq:"div"`
Expand Down