Skip to content

Commit

Permalink
fix: ensure unique IDs for functions with different args
Browse files Browse the repository at this point in the history
Regression from the mage-tools migration, fixed by introducing a unique
ID different from the display name.
  • Loading branch information
odsod committed Jan 30, 2022
1 parent 31a9343 commit 04b1c0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sg/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Deps(ctx context.Context, functions ...interface{}) {
}
wg.Done()
}()
errs[i] = runner.RunOnce(WithLogger(ctx, NewLogger(f.Name())), f.Name(), f.Run)
errs[i] = runner.RunOnce(WithLogger(ctx, NewLogger(f.Name())), f.ID(), f.Run)
}()
}
wg.Wait()
Expand Down
20 changes: 18 additions & 2 deletions sg/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package sg

import (
"context"
"encoding/json"
"fmt"
"reflect"
"runtime"
)

// Function represents a function that can be run with Deps.
type Function interface {
// Name is a unique identifier and display name for the function.
// Name is a non-unique display name for the function.
Name() string

// ID is a unique identifier for the function.
ID() string

// Run the function.
Run(ctx context.Context) error
}
Expand Down Expand Up @@ -70,8 +74,14 @@ func newFn(f interface{}, args ...interface{}) (Function, error) {
if hasNamespace {
argCount++ // +1 for the namespace
}
argsID, err := json.Marshal(args)
if err != nil {
return nil, fmt.Errorf("failed to generate JSON name for args: %w", err)
}
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
return fn{
name: runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(),
name: name,
id: name + "(" + string(argsID) + ")",
f: func(ctx context.Context) error {
callArgs := make([]reflect.Value, 0, argCount)
if hasNamespace {
Expand All @@ -92,9 +102,15 @@ func newFn(f interface{}, args ...interface{}) (Function, error) {

type fn struct {
name string
id string
f func(ctx context.Context) error
}

// ID implements Function.
func (f fn) ID() string {
return f.id
}

// Name implements Function.
func (f fn) Name() string {
return f.name
Expand Down

0 comments on commit 04b1c0f

Please sign in to comment.