-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added GoInitialismsConfig which overrides the initialisms to be regar…
…ded (#2683) * added GoInitialismsConfig which overrides the initialisms to be regarded * typo * adjusted examples and documentation * removed test with side-effects, adjustend yaml indentations, changed example entry "ID" to "CC" (again? I though I already did that) * comply with linter
- Loading branch information
Showing
7 changed files
with
162 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package config | ||
|
||
import "strings" | ||
|
||
// commonInitialisms is a set of common initialisms. | ||
// Only add entries that are highly unlikely to be non-initialisms. | ||
// For instance, "ID" is fine (Freudian code is rare), but "AND" is not. | ||
var commonInitialisms = map[string]bool{ | ||
"ACL": true, | ||
"API": true, | ||
"ASCII": true, | ||
"CPU": true, | ||
"CSS": true, | ||
"CSV": true, | ||
"DNS": true, | ||
"EOF": true, | ||
"GUID": true, | ||
"HTML": true, | ||
"HTTP": true, | ||
"HTTPS": true, | ||
"ICMP": true, | ||
"ID": true, | ||
"IP": true, | ||
"JSON": true, | ||
"KVK": true, | ||
"LHS": true, | ||
"PDF": true, | ||
"PGP": true, | ||
"QPS": true, | ||
"QR": true, | ||
"RAM": true, | ||
"RHS": true, | ||
"RPC": true, | ||
"SLA": true, | ||
"SMTP": true, | ||
"SQL": true, | ||
"SSH": true, | ||
"SVG": true, | ||
"TCP": true, | ||
"TLS": true, | ||
"TTL": true, | ||
"UDP": true, | ||
"UI": true, | ||
"UID": true, | ||
"URI": true, | ||
"URL": true, | ||
"UTF8": true, | ||
"UUID": true, | ||
"VM": true, | ||
"XML": true, | ||
"XMPP": true, | ||
"XSRF": true, | ||
"XSS": true, | ||
} | ||
|
||
// GetInitialisms returns the initialisms to capitalize in Go names. If unchanged, default initialisms will be returned | ||
var GetInitialisms = func() map[string]bool { | ||
return commonInitialisms | ||
} | ||
|
||
// GoInitialismsConfig allows to modify the default behavior of naming Go methods, types and properties | ||
type GoInitialismsConfig struct { | ||
// If true, the Initialisms won't get appended to the default ones but replace them | ||
ReplaceDefaults bool `yaml:"replace_defaults"` | ||
// Custom initialisms to be added or to replace the default ones | ||
Initialisms []string `yaml:"initialisms"` | ||
} | ||
|
||
// setInitialisms adjustes GetInitialisms based on its settings. | ||
func (i GoInitialismsConfig) setInitialisms() { | ||
toUse := i.determineGoInitialisms() | ||
GetInitialisms = func() map[string]bool { | ||
return toUse | ||
} | ||
} | ||
|
||
// determineGoInitialisms returns the Go initialims to be used, based on its settings. | ||
func (i GoInitialismsConfig) determineGoInitialisms() (initialismsToUse map[string]bool) { | ||
if i.ReplaceDefaults { | ||
initialismsToUse = make(map[string]bool, len(i.Initialisms)) | ||
for _, initialism := range i.Initialisms { | ||
initialismsToUse[strings.ToUpper(initialism)] = true | ||
} | ||
} else { | ||
initialismsToUse = make(map[string]bool, len(commonInitialisms)+len(i.Initialisms)) | ||
for initialism, value := range commonInitialisms { | ||
initialismsToUse[strings.ToUpper(initialism)] = value | ||
} | ||
for _, initialism := range i.Initialisms { | ||
initialismsToUse[strings.ToUpper(initialism)] = true | ||
} | ||
} | ||
return initialismsToUse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGoInitialismsConfig(t *testing.T) { | ||
t.Run("load go initialisms config", func(t *testing.T) { | ||
config, err := LoadConfig("testdata/cfg/goInitialisms.yml") | ||
require.NoError(t, err) | ||
require.True(t, config.GoInitialisms.ReplaceDefaults) | ||
require.Len(t, config.GoInitialisms.Initialisms, 2) | ||
}) | ||
t.Run("empty initialism config doesn't change anything", func(t *testing.T) { | ||
tt := GoInitialismsConfig{} | ||
result := tt.determineGoInitialisms() | ||
assert.Equal(t, len(commonInitialisms), len(result)) | ||
}) | ||
t.Run("initialism config appends if desired", func(t *testing.T) { | ||
tt := GoInitialismsConfig{ReplaceDefaults: false, Initialisms: []string{"ASDF"}} | ||
result := tt.determineGoInitialisms() | ||
assert.Equal(t, len(commonInitialisms)+1, len(result)) | ||
assert.True(t, result["ASDF"]) | ||
}) | ||
t.Run("initialism config replaces if desired", func(t *testing.T) { | ||
tt := GoInitialismsConfig{ReplaceDefaults: true, Initialisms: []string{"ASDF"}} | ||
result := tt.determineGoInitialisms() | ||
assert.Equal(t, 1, len(result)) | ||
assert.True(t, result["ASDF"]) | ||
}) | ||
t.Run("initialism config uppercases the initialsms", func(t *testing.T) { | ||
tt := GoInitialismsConfig{Initialisms: []string{"asdf"}} | ||
result := tt.determineGoInitialisms() | ||
assert.True(t, result["ASDF"]) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
go_initialisms: | ||
replace_defaults: true | ||
initialisms: | ||
- 'CC' | ||
- 'BCC' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters