Skip to content

Commit fc15311

Browse files
committed
Fix eval.IncompatibleDSL() to not show internal DSL
1 parent 196e67a commit fc15311

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

eval/eval.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package eval
22

33
import (
44
"fmt"
5-
"path/filepath"
65
"reflect"
76
"runtime"
87
"strings"
8+
"unicode"
99
)
1010

1111
// RunDSL iterates through the root expressions and calls WalkSets on each to
@@ -116,8 +116,7 @@ func ReportError(fm string, vals ...any) {
116116
// IncompatibleDSL should be called by DSL functions when they are invoked in an
117117
// incorrect context (e.g. "Params" in "Service").
118118
func IncompatibleDSL() {
119-
elems := strings.Split(caller(), ".")
120-
ReportError("invalid use of %s", elems[len(elems)-1])
119+
ReportError("invalid use of %s", caller())
121120
}
122121

123122
// InvalidArgError records an invalid argument error. It is used by DSL
@@ -235,13 +234,20 @@ func finalizeSet(set ExpressionSet) {
235234

236235
// caller returns the name of calling function.
237236
func caller() string {
238-
pc, file, _, ok := runtime.Caller(2)
239-
if ok && filepath.Base(file) == "current.go" {
240-
pc, _, _, ok = runtime.Caller(3)
241-
}
242-
if !ok {
243-
return "<unknown>"
237+
for skip := 2; skip <= 3; skip++ {
238+
pc, _, _, ok := runtime.Caller(skip)
239+
if !ok {
240+
break
241+
}
242+
name := runtime.FuncForPC(pc).Name()
243+
elems := strings.Split(name, ".")
244+
caller := elems[len(elems)-1]
245+
for _, first := range caller {
246+
if unicode.IsUpper(first) {
247+
return caller
248+
}
249+
break
250+
}
244251
}
245-
246-
return runtime.FuncForPC(pc).Name()
252+
return "<unknown>"
247253
}

0 commit comments

Comments
 (0)