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{}
}