From 230df83ca12afbdf3cf04888bf1e1b7dc5cad2fb Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Tue, 14 Jan 2025 11:36:12 +0000 Subject: [PATCH] tidy: Use cmp.Or in a few places As suggested here: https://github.com/StyraInc/regal/pull/1326#discussion_r1908895712 Signed-off-by: Charlie Egan --- cmd/languageserver.go | 13 +++++++------ internal/lsp/connection.go | 7 ++----- internal/lsp/server.go | 5 ++--- pkg/fixer/fixes/directorypackagemismatch.go | 8 ++------ 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/cmd/languageserver.go b/cmd/languageserver.go index 3acd0f90..d580b40c 100644 --- a/cmd/languageserver.go +++ b/cmd/languageserver.go @@ -1,6 +1,7 @@ package cmd import ( + "cmp" "context" "fmt" "os" @@ -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"), + ) } } diff --git a/internal/lsp/connection.go b/internal/lsp/connection.go index 83f78d82..4acef9ad 100644 --- a/internal/lsp/connection.go +++ b/internal/lsp/connection.go @@ -22,6 +22,7 @@ package lsp import ( + "cmp" "context" "fmt" "io" @@ -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 } diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 94dc5a30..1cc2dc98 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -2,6 +2,7 @@ package lsp import ( + "cmp" "context" "errors" "fmt" @@ -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" diff --git a/pkg/fixer/fixes/directorypackagemismatch.go b/pkg/fixer/fixes/directorypackagemismatch.go index 2d66c718..00d8e591 100644 --- a/pkg/fixer/fixes/directorypackagemismatch.go +++ b/pkg/fixer/fixes/directorypackagemismatch.go @@ -1,6 +1,7 @@ package fixes import ( + "cmp" "fmt" "path/filepath" "regexp" @@ -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))