~ecs/hq

ref: 05e89aaebceb0242aaa43c75277e0b2cc05a11a0 hq/join.go -rw-r--r-- 1.2 KiB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"errors"
)

type join struct{}

func (join) Execute(args []string, ins []<-chan Datum, out chan Datum) error {
	defer func() {
		out <- Datum{End: true}
		close(out)
	}()
	round := append([]<-chan Datum{}, ins...)
	roundDat := make([]Datum, len(ins))
	for ch, d := Select(round); d != nil; ch, d = Select(round) {
		for i, c := range ins {
			if c == ch {
				roundDat[i] = *d
				break
			}
		}
		for i, c := range round {
			if c == ch {
				round = append(round[:i], round[i+1:]...)
			}
		}
		for i := 0; i < len(roundDat); i++ {
			if roundDat[i].End {
				roundDat = append(roundDat[:i], roundDat[i+1:]...)
				ins = append(ins[:i], ins[i+1:]...)
				i--
			}
		}
		if len(ins) == 0 {
			return nil
		}
		if len(round) == 0 {
			var s string
			for i := 0; i < len(args)-1 || i < len(roundDat); i++ {
				if i < len(roundDat) {
					if roundDat[i].HTML != nil {
						return errors.New("type error: expected text, got html")
					}
					s += *roundDat[i].Text
				}
				if i < len(args)-1 {
					s += args[i+1]
				}
			}
			out <- Datum{Text: &s}
			round = append([]<-chan Datum{}, ins...)
			roundDat = make([]Datum, len(ins))
		}
	}
	return nil
}

func init() {
	Filters["join"] = join{}
}