]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go
update vendor and go.mod
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / hcl2 / hcl / json / didyoumean.go
1 package json
2
3 import (
4 "github.com/agext/levenshtein"
5 )
6
7 var keywords = []string{"false", "true", "null"}
8
9 // keywordSuggestion tries to find a valid JSON keyword that is close to the
10 // given string and returns it if found. If no keyword is close enough, returns
11 // the empty string.
12 func keywordSuggestion(given string) string {
13 return nameSuggestion(given, keywords)
14 }
15
16 // nameSuggestion tries to find a name from the given slice of suggested names
17 // that is close to the given name and returns it if found. If no suggestion
18 // is close enough, returns the empty string.
19 //
20 // The suggestions are tried in order, so earlier suggestions take precedence
21 // if the given string is similar to two or more suggestions.
22 //
23 // This function is intended to be used with a relatively-small number of
24 // suggestions. It's not optimized for hundreds or thousands of them.
25 func nameSuggestion(given string, suggestions []string) string {
26 for _, suggestion := range suggestions {
27 dist := levenshtein.Distance(given, suggestion, nil)
28 if dist < 3 { // threshold determined experimentally
29 return suggestion
30 }
31 }
32 return ""
33 }