Skip to content

Commit 9767bb0

Browse files
committed
Add levensthein suggestion on backend
Check if backend is mis-spelled (distance less than 3) and if so suggest what backend might match. Get the first match, don't iterate until the end in this case, it'll be overkill. Test plan: * Have a slightly mis-spelled backend name (e g wgte). Verify error message suggests wget instead. * Have an outrageously wrongly spelled backend (eg hateternal) and verify that no backend is suggested. * Have a correctly spelled backend and verify no regression.
1 parent bacc5f5 commit 9767bb0

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

internal/pkg/call/call.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ type backend interface {
4747
}
4848

4949
func getBackend(callData *data.Call) (backend, error) {
50-
if backendConstructor, exists := ValidBackends[callData.Backend]; exists {
50+
requestedBackend := callData.Backend
51+
52+
if backendConstructor, exists := ValidBackends[requestedBackend]; exists {
5153
return backendConstructor.constructor(callData, backendConstructor.BinaryName), nil
5254
}
5355

54-
return nil, errors.Errorf("Unknown backend: %s", callData.Backend)
56+
return nil, errors.Errorf("Unknown backend: %s", requestedBackend)
5557
}
5658

5759
func ValidBackend(backendName string) bool {

internal/pkg/parse/backend.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/jonaslu/ain/internal/pkg/call"
88
"github.com/jonaslu/ain/internal/pkg/data"
9+
"github.com/jonaslu/ain/internal/pkg/utils"
910
)
1011

1112
func parseBackendSection(template []sourceMarker, callData *data.Parse) *fatalMarker {
@@ -26,13 +27,19 @@ func parseBackendSection(template []sourceMarker, callData *data.Parse) *fatalMa
2627
}
2728
}
2829

29-
backendName := strings.ToLower(backendLines[0].lineContents)
30+
requestedBackendName := strings.ToLower(backendLines[0].lineContents)
3031

31-
if !call.ValidBackend(backendName) {
32-
return newFatalMarker(fmt.Sprintf("Unknown backend %s", backendName), backendLines[0])
32+
if !call.ValidBackend(requestedBackendName) {
33+
for backendName, _ := range call.ValidBackends {
34+
if utils.LevenshteinDistance(requestedBackendName, backendName) < 3 {
35+
return newFatalMarker(fmt.Sprintf("Unknown backend: %s. Did you mean %s", requestedBackendName, backendName), backendLines[0])
36+
}
37+
}
38+
39+
return newFatalMarker(fmt.Sprintf("Unknown backend %s", requestedBackendName), backendLines[0])
3340
}
3441

35-
callData.Backend = backendName
42+
callData.Backend = requestedBackendName
3643

3744
return nil
3845
}

0 commit comments

Comments
 (0)