Skip to content

Commit

Permalink
tidy: Use cmp.Or in a few places
Browse files Browse the repository at this point in the history
As suggested here: StyraInc#1326 (comment)

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Jan 14, 2025
1 parent 10d3c70 commit 230df83
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
13 changes: 7 additions & 6 deletions cmd/languageserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"cmp"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -35,12 +36,12 @@ func init() {
if err != nil {
fmt.Fprintln(os.Stderr, "error getting executable path:", err)
} else {
v := version.Version
if v == "" {
v = "Unknown"
}

fmt.Fprintf(os.Stderr, "Regal Language Server (path: %s, version: %s)", absPath, v)
fmt.Fprintf(
os.Stderr,
"Regal Language Server (path: %s, version: %s)",
absPath,
cmp.Or(version.Version, "Unknown"),
)
}
}

Expand Down
7 changes: 2 additions & 5 deletions internal/lsp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package lsp

import (
"cmp"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -181,14 +182,10 @@ func buildSendHandler(
}

case resp != nil:
method := getMethod(resp.ID)
method := cmp.Or(getMethod(resp.ID), "(no previous request)")

deleteMethod(resp.ID)

if method == "" {
method = "(no previous request)"
}

if !cfg.ShouldLog(method) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package lsp

import (
"cmp"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -1220,9 +1221,7 @@ func (l *LanguageServer) templateContentsForFile(fileURI string) (string, error)
}

// if we are in the root, then we can use main as a default
if pkg == "" {
pkg = "main"
}
pkg = cmp.Or(pkg, "main")

if strings.HasSuffix(fileURI, "_test.rego") {
pkg += "_test"
Expand Down
8 changes: 2 additions & 6 deletions pkg/fixer/fixes/directorypackagemismatch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fixes

import (
"cmp"
"fmt"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -32,12 +33,7 @@ func (d *DirectoryPackageMismatch) Fix(fc *FixCandidate, opts *RuntimeOptions) (
return nil, err
}

rootPath := opts.BaseDir
if rootPath == "" {
rootPath = filepath.Dir(fc.Filename)
}

rootPath = filepath.Clean(rootPath)
rootPath := filepath.Clean(cmp.Or(opts.BaseDir, filepath.Dir(fc.Filename)))

newPath := filepath.Join(rootPath, pkgPath, filepath.Base(fc.Filename))

Expand Down

0 comments on commit 230df83

Please sign in to comment.