Skip to content

Commit 454339c

Browse files
authored
feat(scaffolding): improve Go function signature detection (#3125)
* fix/typed-error * fix/testing
1 parent 6f467c8 commit 454339c

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

pkg/scaffolding/detectors.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import (
1010
"strings"
1111
)
1212

13+
type SignatureError struct {
14+
Msg string
15+
}
16+
17+
func (e *SignatureError) Error() string {
18+
return fmt.Sprintf("signature detection error: %s", e.Msg)
19+
}
20+
1321
// detector of method signatures. Each instance is for a given runtime.
1422
type detector interface {
1523
Detect(dir string) (static, instanced bool, err error)
@@ -58,7 +66,13 @@ func (d goDetector) Detect(dir string) (static, instanced bool, err error) {
5866
static = true
5967
}
6068
}
61-
return
69+
if static && instanced {
70+
return static, instanced, &SignatureError{Msg: "both static and instanced signatures found"}
71+
}
72+
if !static && !instanced {
73+
return static, instanced, &SignatureError{Msg: "no signatures found"}
74+
}
75+
return static, instanced, nil
6276
}
6377

6478
func (d goDetector) hasFunctionDeclaration(filename, function string) bool {

pkg/scaffolding/detectors_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func Handle() { }
6767
{
6868
Name: "Static and Instanced - error",
6969
Sig: UnknownSignature,
70-
Err: errors.New("error expected"), // TODO: typed error and err.Is/As
70+
Err: errors.New("error expected"),
7171
Src: `
7272
package f
7373
func Handle() { }
@@ -76,7 +76,7 @@ func New() { }
7676
{
7777
Name: "No Signatures Found - error",
7878
Sig: UnknownSignature,
79-
Err: errors.New("error expected"), // TODO: typed error and err.Is/As
79+
Err: errors.New("error expected"),
8080
Src: `
8181
package f
8282
// Intentionally Blank
@@ -127,10 +127,11 @@ func (f *MyFunction) Handle() {}
127127
}
128128

129129
if test.Err != nil {
130-
if err == nil {
131-
t.Fatal("expected error not received")
130+
var sigErr *SignatureError
131+
if !errors.As(err, &sigErr) {
132+
t.Fatalf("expected SignatureError, got:%v", err)
132133
} else {
133-
t.Logf("received expected error: %v", err)
134+
t.Logf("received expected error: %v", sigErr)
134135
}
135136
}
136137

0 commit comments

Comments
 (0)