Skip to content

Commit 7e9aaf9

Browse files
authored
Reduce the set of linters run, and fix existing findings (#2)
1 parent b815e73 commit 7e9aaf9

12 files changed

Lines changed: 277 additions & 90 deletions

File tree

.golangci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
version: "2"
22
linters:
3-
default: all
3+
default: none
4+
enable:
5+
- revive
46
formatters:
57
enable:
68
- gci

binder.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
set "github.com/deckarep/golang-set/v2"
99
)
1010

11-
var (
12-
// ErrDuplicateBinding is returned when Binder.Store is called with a binding whose ID has already
13-
// been stored (which implies that the graph being executed contains multiple tasks producing
14-
// bindings for the same Key).
15-
ErrDuplicateBinding = errors.New("duplicate binding")
16-
)
11+
// ErrDuplicateBinding is returned when Binder.Store is called with a binding whose ID has already
12+
// been stored (which implies that the graph being executed contains multiple tasks producing
13+
// bindings for the same Key).
14+
var ErrDuplicateBinding = errors.New("duplicate binding")
1715

1816
// BindStatus represents the tristate of a Binding.
1917
type BindStatus int

binder_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
set "github.com/deckarep/golang-set/v2"
88
"github.com/google/go-cmp/cmp"
99
"github.com/google/go-cmp/cmp/cmpopts"
10-
1110
tg "github.com/thought-machine/taskgraph"
1211
tgt "github.com/thought-machine/taskgraph/taskgraphtest"
1312
)
@@ -45,7 +44,11 @@ func TestBindersBindingsAndKeys(t *testing.T) {
4544
}
4645

4746
if err := b.Store(key2.Bind(456)); !errors.Is(err, tg.ErrDuplicateBinding) {
48-
t.Errorf("Expected b.Store(key2.Bind(456)) to return error %v; got %v", tg.ErrDuplicateBinding, err)
47+
t.Errorf(
48+
"Expected b.Store(key2.Bind(456)) to return error %v; got %v",
49+
tg.ErrDuplicateBinding,
50+
err,
51+
)
4952
}
5053

5154
tgt.DiffPresent[string](t, b, key1, "foo")
@@ -144,7 +147,11 @@ func TestOverlayBinder(t *testing.T) {
144147
tgt.ExpectPending[int](t, ob, key2)
145148

146149
if err := ob.Store(key1.Bind(123)); !errors.Is(err, tg.ErrDuplicateBinding) {
147-
t.Errorf("Expected ob.Store(key1.Bind(123)) to return error %v; got %v", tg.ErrDuplicateBinding, err)
150+
t.Errorf(
151+
"Expected ob.Store(key1.Bind(123)) to return error %v; got %v",
152+
tg.ErrDuplicateBinding,
153+
err,
154+
)
148155
}
149156

150157
if err := ob.Store(key2.Bind(456)); err != nil {
@@ -194,10 +201,18 @@ func TestGraphTaskBinder(t *testing.T) {
194201
})
195202

196203
if err := gtb.Store(key1.Bind(456)); !errors.Is(err, tg.ErrDuplicateBinding) {
197-
t.Errorf("Expected gtb.Store(key1.Bind(456)) to return error %v; got %v", tg.ErrDuplicateBinding, err)
204+
t.Errorf(
205+
"Expected gtb.Store(key1.Bind(456)) to return error %v; got %v",
206+
tg.ErrDuplicateBinding,
207+
err,
208+
)
198209
}
199210

200211
if err := gtb.Store(key2.Bind(123)); !errors.Is(err, tg.ErrDuplicateBinding) {
201-
t.Errorf("Expected gtb.Store(key2.Bind(123)) to return error %v; got %v", tg.ErrDuplicateBinding, err)
212+
t.Errorf(
213+
"Expected gtb.Store(key2.Bind(123)) to return error %v; got %v",
214+
tg.ErrDuplicateBinding,
215+
err,
216+
)
202217
}
203218
}

example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ var taskIsPalindrome = tg.Reflect[bool]{
3636
Depends: []any{keyInput, keyReversed},
3737
}.Locate()
3838

39-
var graphIsPalindrome = tg.Must(tg.New("example_graph", tg.WithTasks(taskReverseInput, taskIsPalindrome)))
39+
var graphIsPalindrome = tg.Must(
40+
tg.New("example_graph", tg.WithTasks(taskReverseInput, taskIsPalindrome)),
41+
)
4042

4143
func Example() {
4244
res, err := graphIsPalindrome.Run(context.Background(), keyInput.Bind("racecar"))

graph.go

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ func (gn *graphNode) execute(ctx context.Context, rs *runState) (err error) {
175175
"task %s: mismatch between task Provides declaration and returned bindings: missing bindings [%s], got extra bindings [%s]",
176176
gn.task.Name(),
177177
strings.Join(missing, ", "),
178-
strings.Join(extra, ", "))
178+
strings.Join(extra, ", "),
179+
)
179180
}
180181

181182
for _, dependent := range gn.dependents {
@@ -263,7 +264,8 @@ func (g *graph) Run(ctx context.Context, inputs ...Binding) (b Binder, err error
263264
if err != nil {
264265
result = "error"
265266
}
266-
executionLatency.WithLabelValues(g.name, result).Observe(float64(time.Since(startTime) / time.Millisecond))
267+
executionLatency.WithLabelValues(g.name, result).
268+
Observe(float64(time.Since(startTime) / time.Millisecond))
267269
}()
268270
base, err := g.buildInputBinder(inputs...)
269271
if err != nil {
@@ -352,7 +354,10 @@ func (g *graph) AsTask(exposeKeys ...ID) (Task, error) {
352354
}
353355
}
354356
if len(missing) > 0 {
355-
return nil, wrapStackErrorf("exposed key(s) not bound after graph execution: %s", strings.Join(missing, ", "))
357+
return nil, wrapStackErrorf(
358+
"exposed key(s) not bound after graph execution: %s",
359+
strings.Join(missing, ", "),
360+
)
356361
}
357362

358363
// The exposed keys are added to the external binder via the graphTaskBinder, so we don't return
@@ -371,7 +376,10 @@ func (g *graph) Graphviz(includeInputs bool) string {
371376
for _, dep := range n.task.Depends() {
372377
if !g.allProvided.Contains(dep) {
373378
inputID := fmt.Sprintf("%s_input_%s", n.id, dep.id)
374-
nodes = append(nodes, fmt.Sprintf(" %s [label=\"Input - %s\", shape=diamond];", inputID, dep))
379+
nodes = append(
380+
nodes,
381+
fmt.Sprintf(" %s [label=\"Input - %s\", shape=diamond];", inputID, dep),
382+
)
375383
edges = append(edges, fmt.Sprintf(" %s -> %s;", inputID, n.id))
376384
}
377385
}
@@ -384,8 +392,14 @@ func (g *graph) Graphviz(includeInputs bool) string {
384392
for _, dep := range n.task.Provides() {
385393
if !g.allDependencies.Contains(dep) {
386394
outputID := fmt.Sprintf("%s_output_%s", n.id, dep)
387-
nodes = append(nodes, fmt.Sprintf(" %s [label=\"Output\", shape=diamond];", outputID))
388-
edges = append(edges, fmt.Sprintf(" %s -> %s [label=\"%s\"];", n.id, outputID, dep))
395+
nodes = append(
396+
nodes,
397+
fmt.Sprintf(" %s [label=\"Output\", shape=diamond];", outputID),
398+
)
399+
edges = append(
400+
edges,
401+
fmt.Sprintf(" %s -> %s [label=\"%s\"];", n.id, outputID, dep),
402+
)
389403
}
390404
}
391405
}
@@ -402,15 +416,17 @@ func (g *graph) Graphviz(includeInputs bool) string {
402416
return buf.String()
403417
}
404418

405-
type GraphOptions struct {
419+
type graphOptions struct {
406420
tasks []Task
407421
tracer trace.Tracer
408422
}
409423

410-
type GraphOption func(opts *GraphOptions) error
424+
// A GraphOption is used to configure a new Graph.
425+
type GraphOption func(opts *graphOptions) error
411426

427+
// WithTasks sets the tasks which form the graph.
412428
func WithTasks(tasks ...TaskSet) GraphOption {
413-
return func(opts *GraphOptions) error {
429+
return func(opts *graphOptions) error {
414430
opts.tasks = taskset(tasks).Tasks()
415431

416432
if len(opts.tasks) > taskLimit {
@@ -421,8 +437,9 @@ func WithTasks(tasks ...TaskSet) GraphOption {
421437
}
422438
}
423439

440+
// WithTracer sets a tracer to record graph execution.
424441
func WithTracer(tracer trace.Tracer) GraphOption {
425-
return func(opts *GraphOptions) error {
442+
return func(opts *graphOptions) error {
426443
opts.tracer = tracer
427444

428445
return nil
@@ -433,7 +450,7 @@ func WithTracer(tracer trace.Tracer) GraphOption {
433450
//
434451
// Ideally, Graphs should be created on program startup, rather than creating them dynamically.
435452
func New(name string, opts ...GraphOption) (Graph, error) {
436-
o := &GraphOptions{
453+
o := &graphOptions{
437454
tracer: noop.NewTracerProvider().Tracer("github.com/thought-machine/taskgraph"),
438455
}
439456

@@ -458,7 +475,10 @@ func New(name string, opts ...GraphOption) (Graph, error) {
458475
var badTaskErrs error
459476
for _, t := range g.tasks {
460477
if t.Name() == "" || t.Location() == "" {
461-
badTaskErrs = errors.Join(badTaskErrs, fmt.Errorf("tasks must have a name and location: (%s, %s)", t.Name(), t.Location()))
478+
badTaskErrs = errors.Join(
479+
badTaskErrs,
480+
fmt.Errorf("tasks must have a name and location: (%s, %s)", t.Name(), t.Location()),
481+
)
462482
}
463483
node := &graphNode{
464484
id: sanitizeTaskName(t.Name()),
@@ -477,7 +497,10 @@ func New(name string, opts ...GraphOption) (Graph, error) {
477497

478498
g.allProvided.Append(t.Provides()...)
479499
for _, id := range t.Provides() {
480-
provideTasks[id.String()] = append(provideTasks[id.String()], fmt.Sprintf("%s - %s", t.Name(), t.Location()))
500+
provideTasks[id.String()] = append(
501+
provideTasks[id.String()],
502+
fmt.Sprintf("%s - %s", t.Name(), t.Location()),
503+
)
481504
}
482505
}
483506
if badTaskErrs != nil {
@@ -486,20 +509,34 @@ func New(name string, opts ...GraphOption) (Graph, error) {
486509
var duplicateTaskNames []string
487510
for name, locations := range taskLocations {
488511
if len(locations) > 1 {
489-
duplicateTaskNames = append(duplicateTaskNames, fmt.Sprintf("%s (%s)", name, strings.Join(locations, ", ")))
512+
duplicateTaskNames = append(
513+
duplicateTaskNames,
514+
fmt.Sprintf("%s (%s)", name, strings.Join(locations, ", ")),
515+
)
490516
}
491517
}
492518
if len(duplicateTaskNames) > 0 {
493-
return nil, wrapStackErrorf("%w: %s", ErrDuplicateTaskNames, strings.Join(duplicateTaskNames, ", "))
519+
return nil, wrapStackErrorf(
520+
"%w: %s",
521+
ErrDuplicateTaskNames,
522+
strings.Join(duplicateTaskNames, ", "),
523+
)
494524
}
495525
var duplicateProvides []string
496526
for id, tasks := range provideTasks {
497527
if len(tasks) > 1 {
498-
duplicateProvides = append(duplicateProvides, fmt.Sprintf("%s (%s)", id, strings.Join(tasks, ", ")))
528+
duplicateProvides = append(
529+
duplicateProvides,
530+
fmt.Sprintf("%s (%s)", id, strings.Join(tasks, ", ")),
531+
)
499532
}
500533
}
501534
if len(duplicateProvides) > 0 {
502-
return nil, wrapStackErrorf("%w: %s", ErrDuplicateProvidedKeys, strings.Join(duplicateProvides, ", "))
535+
return nil, wrapStackErrorf(
536+
"%w: %s",
537+
ErrDuplicateProvidedKeys,
538+
strings.Join(duplicateProvides, ", "),
539+
)
503540
}
504541

505542
for _, node := range g.nodes {
@@ -543,7 +580,11 @@ func sanitizeTaskName(name string) string {
543580
func checkCycle(node *graphNode, path []string) error {
544581
for i := len(path) - 1; i >= 0; i-- {
545582
if path[i] == node.task.Name() {
546-
return wrapStackErrorf("%w: %s", ErrGraphCycle, strings.Join(append(path[i:], path[i]), " -> "))
583+
return wrapStackErrorf(
584+
"%w: %s",
585+
ErrGraphCycle,
586+
strings.Join(append(path[i:], path[i]), " -> "),
587+
)
547588
}
548589
}
549590
path = append(path, node.task.Name())

graph_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9-
109
tg "github.com/thought-machine/taskgraph"
1110
tgt "github.com/thought-machine/taskgraph/taskgraphtest"
1211
)
@@ -172,7 +171,13 @@ func TestGraphErrors(t *testing.T) {
172171
for i := 0; i <= 1000; i++ {
173172
tasks = append(tasks, tg.NewTask("task", tgt.DummyTaskFunc(), nil, nil))
174173
}
175-
if _, err := tg.New("test_graph", tg.WithTasks(tasks...)); !errors.Is(err, tg.ErrTooManyTasks) {
174+
if _, err := tg.New(
175+
"test_graph",
176+
tg.WithTasks(tasks...),
177+
); !errors.Is(
178+
err,
179+
tg.ErrTooManyTasks,
180+
) {
176181
t.Errorf("expected error %v; got %v", tg.ErrTooManyTasks, err)
177182
}
178183
})

key.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ func (k *key[T]) Get(b Binder) (T, error) {
8282
typed, ok := binding.Value().(T)
8383
if !ok {
8484
var want T
85-
return empty, wrapStackErrorf("cannot get key %q: %w (got %T, want %T)", k.id, ErrWrongType, binding.Value(), want)
85+
return empty, wrapStackErrorf(
86+
"cannot get key %q: %w (got %T, want %T)",
87+
k.id,
88+
ErrWrongType,
89+
binding.Value(),
90+
want,
91+
)
8692
}
8793
return typed, nil
8894
default:

reflect.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func (rk *reflectKey) Get(b Binder) (reflect.Value, error) {
4242
if !outs[1].IsNil() {
4343
err, ok := outs[1].Interface().(error)
4444
if !ok {
45-
return reflect.Value{}, wrapStackErrorf("could not convert output 1 to error; got %T", outs[1].Interface())
45+
return reflect.Value{}, wrapStackErrorf(
46+
"could not convert output 1 to error; got %T",
47+
outs[1].Interface(),
48+
)
4649
}
4750
return reflect.Value{}, err
4851
}
@@ -163,14 +166,21 @@ func newReflectFn(fn any, resultType reflect.Type, deps ...any) (rf *reflectFn,
163166
if !outs[1].IsNil() {
164167
err, ok := outs[1].Interface().(error)
165168
if !ok {
166-
return nil, wrapStackErrorf("could not convert function output 1 to error; got %T", outs[1].Interface())
169+
return nil, wrapStackErrorf(
170+
"could not convert function output 1 to error; got %T",
171+
outs[1].Interface(),
172+
)
167173
}
168174
return nil, err
169175
}
170176
return outs[0].Interface(), nil
171177
}
172178
} else {
173-
return nil, wrapStackErrorf("function should return %s or (%s, error)", resultType, resultType)
179+
return nil, wrapStackErrorf(
180+
"function should return %s or (%s, error)",
181+
resultType,
182+
resultType,
183+
)
174184
}
175185

176186
hasContext := fnType.NumIn() > 0 && fnType.In(0).Implements(contextType)
@@ -181,7 +191,11 @@ func newReflectFn(fn any, resultType reflect.Type, deps ...any) (rf *reflectFn,
181191
offset++
182192
}
183193
if argCount != len(deps) {
184-
return nil, wrapStackErrorf("function takes %d arguments (excluding any initial context), but %d deps were provided", argCount, len(deps))
194+
return nil, wrapStackErrorf(
195+
"function takes %d arguments (excluding any initial context), but %d deps were provided",
196+
argCount,
197+
len(deps),
198+
)
185199
}
186200

187201
var keys []*reflectKey
@@ -192,7 +206,12 @@ func newReflectFn(fn any, resultType reflect.Type, deps ...any) (rf *reflectFn,
192206
return nil, wrapStackErrorf("dependency %d: %w", i, err)
193207
}
194208
if !rk.valueType.AssignableTo(fnType.In(i + offset)) {
195-
return nil, wrapStackErrorf("dependency %d is Key[%v]; want Key[%v]", i, rk.valueType, fnType.In(i+offset))
209+
return nil, wrapStackErrorf(
210+
"dependency %d is Key[%v]; want Key[%v]",
211+
i,
212+
rk.valueType,
213+
fnType.In(i+offset),
214+
)
196215
}
197216
keys = append(keys, rk)
198217
id, err := rk.ID()
@@ -272,7 +291,11 @@ func (r Reflect[T]) Build() (Task, error) {
272291
}
273292
typed, ok := res.(T)
274293
if !ok {
275-
return nil, wrapStackErrorf("%s: could not convert function result to T; got %T", r.errorPrefix(), res)
294+
return nil, wrapStackErrorf(
295+
"%s: could not convert function result to T; got %T",
296+
r.errorPrefix(),
297+
res,
298+
)
276299
}
277300
return []Binding{r.ResultKey.Bind(typed)}, nil
278301
},
@@ -341,7 +364,11 @@ func (r ReflectMulti) Build() (Task, error) {
341364
}
342365
typed, ok := res.([]Binding)
343366
if !ok {
344-
return nil, wrapStackErrorf("%s: could not convert function result to []Binding; got %T", r.errorPrefix(), res)
367+
return nil, wrapStackErrorf(
368+
"%s: could not convert function result to []Binding; got %T",
369+
r.errorPrefix(),
370+
res,
371+
)
345372
}
346373
return typed, nil
347374
},

0 commit comments

Comments
 (0)