Skip to content

Commit

Permalink
fix(sg): remove -fm suffix in Fn name
Browse files Browse the repository at this point in the history
It seems common to pass arguments to Deps and SerialDeps as below:
```go
type namespace sg.Namespace

func (n namespace) Default(ctx context.Context) error {
	sg.Deps(ctx, n.FirstTarget, n.SecondTarget)
	return nil
}

```

Currently this leads to logs in the dep targets to be prefixed with
`[namespace:first-target-fm]` and `[namespace:second-target]`, because
of how the compiler implements methods bound to specific receivers. If
instead the dependencies were specified as:
```go
sg.Deps(ctx, namespace.FirstTarget, namespace.SecondTarget)
```
the names would log prefixes would simply be `[namespace:first-target]`
and `[namespace:second-target]`.

This commit removes the `-fm` suffix. This is safe as no function name
in go can include a hyphen.
  • Loading branch information
ericwenn committed Feb 25, 2024
1 parent 01c2b90 commit 088ea9e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion sg/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"runtime"
"strings"
)

// Target represents a target function that can be run with Deps.
Expand Down Expand Up @@ -79,8 +80,14 @@ func newFn(f interface{}, args ...interface{}) (Target, error) {
return nil, fmt.Errorf("failed to generate JSON name for args: %w", err)
}
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
// If f is a method bound to a specific receiver the compiler
// will wrap the function and add `-fm` to the end of its name.
// Since there is no way to name a function in go with a hyphen, the
// suffix can simply be removed.
// See: https://stackoverflow.com/questions/32925344/why-is-there-a-fm-suffix-when-getting-a-functions-name-in-go
trimmedName := strings.TrimSuffix(name, "-fm")
return fn{
name: name,
name: trimmedName,
id: name + "(" + string(argsID) + ")",
f: func(ctx context.Context) error {
callArgs := make([]reflect.Value, 0, argCount)
Expand Down
2 changes: 1 addition & 1 deletion sg/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestFn_Name(t *testing.T) {
{
name: "namespace value",
fn: ns.MyFunc,
expected: "go.einride.tech/sage/sg.namespace.MyFunc-fm",
expected: "go.einride.tech/sage/sg.namespace.MyFunc",
},
} {
tt := tt
Expand Down

0 comments on commit 088ea9e

Please sign in to comment.