~ecs/hq

ref: 05e89aaebceb0242aaa43c75277e0b2cc05a11a0 hq/attribute.go -rw-r--r-- 715 bytes
05e89aaeEmber Sawady Improve multi error handling 6 months ago
                                                                                
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
package main

import (
	"errors"
)

type attr struct{}

func (attr) Execute(args []string, ins []<-chan Datum, out chan Datum) error {
	defer func() {
		out <- Datum{End: true}
		close(out)
	}()
	if len(args) == 0 {
		return errors.New("usage: . <attribute> [<attribute>...]")
	}
	for _, d := Select(ins); d != nil; _, d = Select(ins) {
		if d.End {
			continue
		}
		if d.HTML == nil {
			return errors.New("type error: expected html, got text")
		}
		for _, attr := range d.HTML.Attr {
			for _, arg := range args[1:] {
				if attr.Key == arg {
					// Work around Go being shit
					val := attr.Val
					out <- Datum{Text: &val}
					break
				}
			}
		}
	}
	return nil
}

func init() {
	Filters["."] = attr{}
}