chore(all): use string comparisons instead of length for string variables

This commit is contained in:
Quentin McGaw
2023-04-13 15:50:02 +00:00
parent 3339c838fa
commit 15c6ad4b01
32 changed files with 60 additions and 60 deletions

View File

@@ -136,8 +136,8 @@ func makeSettingsFromObject(common commonSettings, rawSettings json.RawMessage)
settingsSlice []settings.Settings, warnings []string, err error) {
provider := models.Provider(common.Provider)
if provider == constants.DuckDNS { // only hosts, no domain
if len(common.Domain) > 0 { // retro compatibility
if len(common.Host) == 0 {
if common.Domain != "" { // retro compatibility
if common.Host == "" {
common.Host = strings.TrimSuffix(common.Domain, ".duckdns.org")
warnings = append(warnings,
fmt.Sprintf("DuckDNS record should have %q specified as host instead of %q as domain",
@@ -151,7 +151,7 @@ func makeSettingsFromObject(common commonSettings, rawSettings json.RawMessage)
}
hosts := strings.Split(common.Host, ",")
if len(common.IPVersion) == 0 {
if common.IPVersion == "" {
common.IPVersion = ipversion.IP4or6.String()
}
ipVersion, err := ipversion.Parse(common.IPVersion)

View File

@@ -71,9 +71,9 @@ var (
func (db *Database) Check() error {
for _, record := range db.data.Records {
switch {
case len(record.Domain) == 0:
case record.Domain == "":
return fmt.Errorf("%w: for record %s", ErrDomainEmpty, record)
case len(record.Host) == 0:
case record.Host == "":
return fmt.Errorf("%w: for record %s", ErrHostIsEmpty, record)
}
var t time.Time

View File

@@ -16,10 +16,10 @@ func (r *Record) HTML(now time.Time) models.HTMLRow {
if r.Status == constants.UPTODATE {
message = "no IP change for " + r.History.GetDurationSinceSuccess(now)
}
if len(message) > 0 {
if message != "" {
message = fmt.Sprintf("(%s)", message)
}
if len(r.Status) == 0 {
if r.Status == "" {
row.Status = NotAvailable
} else {
row.Status = models.HTML(fmt.Sprintf("%s %s, %s",

View File

@@ -30,7 +30,7 @@ func New(settings settings.Settings, events []models.HistoryEvent) Record {
func (r *Record) String() string {
status := string(r.Status)
if len(r.Message) > 0 {
if r.Message != "" {
status += " (" + r.Message + ")"
}
return fmt.Sprintf("%s: %s %s; %s",

View File

@@ -74,21 +74,21 @@ var (
func (p *Provider) isValid() error {
switch {
case len(p.key) > 0: // email and key must be provided
case p.key != "": // email and key must be provided
switch {
case !keyRegex.MatchString(p.key):
return errors.ErrMalformedKey
case !verification.NewVerifier().MatchEmail(p.email):
return errors.ErrMalformedEmail
}
case len(p.userServiceKey) > 0: // only user service key
case p.userServiceKey != "": // only user service key
if !userServiceKeyRegex.MatchString(p.key) {
return errors.ErrMalformedUserServiceKey
}
default: // constants.API token only
}
switch {
case len(p.zoneIdentifier) == 0:
case p.zoneIdentifier == "":
return errors.ErrEmptyZoneIdentifier
case p.ttl == 0:
return errors.ErrEmptyTTL
@@ -134,11 +134,11 @@ func (p *Provider) setHeaders(request *http.Request) {
headers.SetContentType(request, "application/json")
headers.SetAccept(request, "application/json")
switch {
case len(p.token) > 0:
case p.token != "":
headers.SetAuthBearer(request, p.token)
case len(p.userServiceKey) > 0:
case p.userServiceKey != "":
request.Header.Set("X-Auth-User-Service-Key", p.userServiceKey)
case len(p.email) > 0 && len(p.key) > 0:
case p.email != "" && p.key != "":
request.Header.Set("X-Auth-Email", p.email)
request.Header.Set("X-Auth-Key", p.key)
}

View File

@@ -52,7 +52,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.password) == 0 {
if p.password == "" {
return errors.ErrEmptyPassword
}
return nil

View File

@@ -58,9 +58,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -47,7 +47,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.token) == 0 {
if p.token == "" {
return errors.ErrEmptyToken
}
return nil

View File

@@ -61,7 +61,7 @@ func (p *Provider) isValid() error {
switch {
case !regexUsername.MatchString(p.username):
return fmt.Errorf("%w: %s", errors.ErrMalformedUsername, p.username)
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
}
return nil

View File

@@ -48,7 +48,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.token) == 0 {
if p.token == "" {
return errors.ErrEmptyToken
}
return nil
@@ -158,7 +158,7 @@ func (p *Provider) Update(ctx context.Context, client *http.Client, ip net.IP) (
break
}
}
if len(recordID) == 0 {
if recordID == "" {
return nil, errors.ErrNotFound
}

View File

@@ -37,7 +37,7 @@ func New(data json.RawMessage, domain, host string,
if err != nil {
return nil, err
}
if len(host) == 0 {
if host == "" {
host = "@" // default
}
p = &Provider{
@@ -57,11 +57,11 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case len(p.name) == 0:
case p.name == "":
return errors.ErrEmptyName
case p.host != "@":
return errors.ErrHostOnlyAt

View File

@@ -62,7 +62,7 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case p.clientKey == "":
return errors.ErrEmptyPassword

View File

@@ -49,7 +49,7 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.token) == 0:
case p.token == "":
return errors.ErrEmptyToken
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -48,7 +48,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.token) == 0 {
if p.token == "" {
return errors.ErrEmptyToken
}
return nil

View File

@@ -50,7 +50,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.key) == 0 {
if p.key == "" {
return errors.ErrEmptyKey
}
return nil

View File

@@ -57,7 +57,7 @@ func (p *Provider) isValid() error {
switch {
case !keyRegex.MatchString(p.key):
return errors.ErrMalformedKey
case len(p.secret) == 0:
case p.secret == "":
return errors.ErrEmptySecret
}
return nil
@@ -153,7 +153,7 @@ func (p *Provider) Update(ctx context.Context, client *http.Client, ip net.IP) (
Message string `json:"message"`
}
jsonErr := json.Unmarshal(b, &parsedJSON)
if jsonErr != nil || len(parsedJSON.Message) == 0 {
if jsonErr != nil || parsedJSON.Message == "" {
return nil, fmt.Errorf("%w: %s", err, utils.ToSingleLine(string(b)))
}
return nil, fmt.Errorf("%w: %s", err, parsedJSON.Message)

View File

@@ -56,9 +56,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
}
return nil

View File

@@ -52,7 +52,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.password) == 0 {
if p.password == "" {
return errors.ErrEmptyPassword
}
return nil

View File

@@ -55,9 +55,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -50,7 +50,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.token) == 0 {
if p.token == "" {
return errors.ErrEmptyToken
}
return nil

View File

@@ -55,7 +55,7 @@ func (p *Provider) isValid() error {
switch {
case !verification.NewRegex().MatchEmail(p.email):
return errors.ErrMalformedEmail
case len(p.token) == 0:
case p.token == "":
return errors.ErrEmptyToken
}
return nil

View File

@@ -49,7 +49,7 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.key) == 0 {
if p.key == "" {
return errors.ErrEmptyKey
}
return nil

View File

@@ -57,11 +57,11 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
const maxUsernameLength = 50
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.username) > maxUsernameLength:
return fmt.Errorf("%w: longer than 50 characters", errors.ErrMalformedUsername)
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -54,9 +54,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -82,18 +82,18 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
if p.mode == "api" {
switch {
case len(p.appKey) == 0:
case p.appKey == "":
return errors.ErrEmptyAppKey
case len(p.consumerKey) == 0:
case p.consumerKey == "":
return errors.ErrEmptyConsumerKey
case len(p.appSecret) == 0:
case p.appSecret == "":
return errors.ErrEmptySecret
}
} else {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -55,9 +55,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.username) == 0:
case p.username == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -57,13 +57,13 @@ func New(data json.RawMessage, domain, host string,
}
func (p *Provider) isValid() error {
if len(p.token) > 0 {
if p.token != "" {
return nil
}
switch {
case len(p.user) == 0:
case p.user == "":
return errors.ErrEmptyUsername
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard
@@ -119,7 +119,7 @@ func (p *Provider) Update(ctx context.Context, client *http.Client, ip net.IP) (
} else {
values.Set("myip", ip.String())
}
if len(p.token) > 0 {
if p.token != "" {
values.Set("user", hostname)
values.Set("pass", p.token)
} else {

View File

@@ -52,7 +52,7 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -55,9 +55,9 @@ func New(data json.RawMessage, domain, host string,
func (p *Provider) isValid() error {
switch {
case len(p.email) == 0:
case p.email == "":
return errors.ErrEmptyEmail
case len(p.password) == 0:
case p.password == "":
return errors.ErrEmptyPassword
case p.host == "*":
return errors.ErrHostWildcard

View File

@@ -76,7 +76,7 @@ func Test_data(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
if len(testCase.panicMessage) > 0 {
if testCase.panicMessage != "" {
assert.PanicsWithValue(t, testCase.panicMessage, func() {
testCase.provider.data()
})

View File

@@ -100,7 +100,7 @@ func (provider Provider) url(version ipversion.IPVersion) (url string, ok bool)
url = strings.TrimPrefix(s, "url:")
}
if len(url) == 0 {
if url == "" {
return "", false
}

View File

@@ -256,7 +256,7 @@ func countryCodeToName(countryCode string) (name string) {
"zw": "Zimbabwe",
}
name = mapping[countryCode]
if len(name) == 0 {
if name == "" {
name = countryCode
}
return name