aboutsummaryrefslogtreecommitdiff
path: root/src/tools/ak/bucketize/pipe.go
blob: 71622329dd4ce2d4b0138ea4f5dd01b57cda50c3 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bucketize

import (
	"context"
	"fmt"
	"strings"
	"sync"

	"src/tools/ak/res/res"
)

type contextKey int

const (
	ctxErr contextKey = 0
)

// errorf returns a formatted error with any context sensitive information prefixed to the error
func errorf(ctx context.Context, fmts string, a ...interface{}) error {
	if s, ok := ctx.Value(ctxErr).(string); ok {
		return fmt.Errorf(strings.Join([]string{s, fmts}, ""), a...)
	}
	return fmt.Errorf(fmts, a...)
}

// prefixErr returns a context which adds a prefix to error messages.
func prefixErr(ctx context.Context, add string) context.Context {
	if s, ok := ctx.Value(ctxErr).(string); ok {
		return context.WithValue(ctx, ctxErr, strings.Join([]string{s, add}, ""))
	}
	return context.WithValue(ctx, ctxErr, add)
}

func separatePathInfosByValues(ctx context.Context, pis []*res.PathInfo) (<-chan *res.PathInfo, <-chan *res.PathInfo) {
	valuesPIC := make(chan *res.PathInfo)
	nonValuesPIC := make(chan *res.PathInfo)
	go func() {
		defer close(valuesPIC)
		defer close(nonValuesPIC)
		for _, pi := range pis {
			if pi.Type.Kind() == res.Value || pi.Type.Kind() == res.Both && strings.HasPrefix(pi.TypeDir, "values") {
				select {
				case valuesPIC <- pi:
				case <-ctx.Done():
					return
				}
			} else {
				select {
				case nonValuesPIC <- pi:
				case <-ctx.Done():
					return
				}
			}
		}
	}()
	return valuesPIC, nonValuesPIC
}

func mergeValuesResourceStreams(ctx context.Context, vrCs []<-chan *res.ValuesResource) <-chan *res.ValuesResource {
	vrC := make(chan *res.ValuesResource)
	var wg sync.WaitGroup
	wg.Add(len(vrCs))
	output := func(c <-chan *res.ValuesResource) {
		defer wg.Done()
		for vr := range c {
			select {
			case vrC <- vr:
			case <-ctx.Done():
				return
			}
		}
	}
	for _, c := range vrCs {
		go output(c)
	}
	go func() {
		wg.Wait()
		close(vrC)
	}()
	return vrC
}

func mergeResourcesAttributeStreams(ctx context.Context, raCs []<-chan *ResourcesAttribute) <-chan *ResourcesAttribute {
	raC := make(chan *ResourcesAttribute)
	var wg sync.WaitGroup
	wg.Add(len(raCs))
	output := func(c <-chan *ResourcesAttribute) {
		defer wg.Done()
		for ra := range c {
			select {
			case raC <- ra:
			case <-ctx.Done():
				return
			}
		}
	}
	for _, c := range raCs {
		go output(c)
	}
	go func() {
		wg.Wait()
		close(raC)
	}()
	return raC
}

// mergeErrStreams fans in multiple error streams into a single stream.
func mergeErrStreams(ctx context.Context, errCs []<-chan error) <-chan error {
	errC := make(chan error)
	var wg sync.WaitGroup
	wg.Add(len(errCs))
	output := func(c <-chan error) {
		defer wg.Done()
		for e := range c {
			select {
			case errC <- e:
			case <-ctx.Done():
				return
			}
		}
	}
	for _, rc := range errCs {
		go output(rc)
	}
	go func() {
		wg.Wait()
		close(errC)
	}()
	return errC
}

// sendErr attempts to send the provided error to the provided chan, however is the context is canceled, it will return false.
func sendErr(ctx context.Context, errC chan<- error, err error) bool {
	select {
	case <-ctx.Done():
		return false
	case errC <- err:
		return true
	}
}