From a977617797f4746f3e5fd5cd33351f6fad93ebdd Mon Sep 17 00:00:00 2001 From: John Kerl Date: Sat, 1 Jan 2022 14:28:19 -0500 Subject: [PATCH] Address some staticcheck issues (#823) * address some staticcheck issues * address some staticcheck issues * address some staticcheck issues * address some staticcheck issues --- Makefile | 7 +- .../cli-parser/arch/args_notwindows.go | 0 .../cli-parser/arch/args_windows.go | 0 .../experiments}/cli-parser/cliparse.go | 0 .../experiments}/cli-parser/go.mod | 0 .../experiments}/cli-parser/go.sum | 0 .../experiments}/cli-parser/msys2in.sh | 0 .../experiments}/cli-parser/msys2out.bat | 0 .../experiments}/dsl-parser/.vimrc | 0 .../experiments}/dsl-parser/README.md | 0 .../experiments}/dsl-parser/one/build | 0 .../experiments}/dsl-parser/one/go.mod | 0 .../experiments}/dsl-parser/one/go.sum | 0 .../experiments}/dsl-parser/one/main.go | 0 .../experiments}/dsl-parser/one/run | 0 .../experiments}/dsl-parser/one/semi1.bnf | 0 .../experiments}/dsl-parser/one/semi2.bnf | 0 .../experiments}/dsl-parser/two/brackets.bnf | 0 .../experiments}/dsl-parser/two/build | 0 .../experiments}/dsl-parser/two/emit01.bnf | 0 .../experiments}/dsl-parser/two/emit02.bnf | 0 .../experiments}/dsl-parser/two/go.mod | 0 .../experiments}/dsl-parser/two/go.sum | 0 .../experiments}/dsl-parser/two/main.go | 0 .../experiments}/dsl-parser/two/mktags | 0 .../experiments}/dsl-parser/two/run | 0 .../experiments}/dsl-parser/two/semi3.bnf | 0 .../experiments}/dsl-parser/two/temp.bnf | 0 .../experiments}/gc-raw-notes.txt | 0 .../experiments}/line-parser/README.txt | 0 .../experiments}/line-parser/read-string.go | 0 .../experiments}/line-parser/scanner.go | 0 .../experiments}/line-parser/splitter.go | 0 internal/pkg/auxents/auxents.go | 8 - internal/pkg/auxents/regtest/invoker.go | 3 +- internal/pkg/auxents/regtest/regtester.go | 27 +-- internal/pkg/dsl/ast_build.go | 8 +- internal/pkg/dsl/cst/block-exit.go | 4 +- internal/pkg/dsl/cst/builtin_functions.go | 189 ++++++++---------- internal/pkg/dsl/cst/cond.go | 4 +- internal/pkg/dsl/cst/dump.go | 16 +- internal/pkg/dsl/cst/emit_emitp.go | 31 +-- internal/pkg/dsl/cst/emitf.go | 22 +- internal/pkg/dsl/cst/evaluable.go | 5 +- internal/pkg/dsl/cst/for.go | 42 ++-- internal/pkg/dsl/cst/if.go | 4 +- internal/pkg/dsl/cst/leaves.go | 14 +- internal/pkg/dsl/cst/lvalues.go | 49 ++--- internal/pkg/dsl/cst/print.go | 15 +- internal/pkg/dsl/cst/root.go | 11 +- internal/pkg/dsl/cst/statements.go | 22 +- internal/pkg/dsl/cst/tee.go | 22 +- internal/pkg/dsl/cst/udf.go | 31 ++- internal/pkg/dsl/cst/uds.go | 23 +-- internal/pkg/dsl/cst/validate.go | 65 ++---- internal/pkg/dsl/cst/while.go | 6 +- internal/pkg/input/pseudo_reader_gen.go | 5 +- internal/pkg/input/record_reader_csv.go | 15 +- internal/pkg/input/record_reader_csvlite.go | 21 +- internal/pkg/input/record_reader_factory.go | 3 +- internal/pkg/input/record_reader_json.go | 23 +-- internal/pkg/input/record_reader_xtab.go | 8 +- internal/pkg/lib/file-readers.go | 8 +- internal/pkg/mlrval/mlrmap_accessors.go | 17 +- internal/pkg/mlrval/mlrval_format.go | 9 +- internal/pkg/mlrval/mlrval_json.go | 58 +++--- internal/pkg/mlrval/mlrval_output.go | 11 - internal/pkg/output/file-output-handlers.go | 9 +- internal/pkg/output/record_writer_csv.go | 6 +- internal/pkg/output/record_writer_factory.go | 3 +- internal/pkg/output/record_writer_nidx.go | 2 - internal/pkg/runtime/stack.go | 17 +- internal/pkg/transformers/label.go | 8 +- internal/pkg/transformers/merge-fields.go | 9 +- internal/pkg/transformers/put-or-filter.go | 8 +- internal/pkg/transformers/rename.go | 3 +- internal/pkg/transformers/seqgen.go | 26 +-- internal/pkg/transformers/stats1.go | 8 +- internal/pkg/transformers/stats2.go | 8 +- internal/pkg/transformers/step.go | 11 +- .../transformers/utils/stats1-accumulators.go | 17 +- internal/pkg/types/mlrval_typing.go | 15 +- test/cases/verb-stats1/0018/experr | 1 - 83 files changed, 316 insertions(+), 641 deletions(-) rename {experiments => cmd/experiments}/cli-parser/arch/args_notwindows.go (100%) rename {experiments => cmd/experiments}/cli-parser/arch/args_windows.go (100%) rename {experiments => cmd/experiments}/cli-parser/cliparse.go (100%) rename {experiments => cmd/experiments}/cli-parser/go.mod (100%) rename {experiments => cmd/experiments}/cli-parser/go.sum (100%) rename {experiments => cmd/experiments}/cli-parser/msys2in.sh (100%) rename {experiments => cmd/experiments}/cli-parser/msys2out.bat (100%) rename {experiments => cmd/experiments}/dsl-parser/.vimrc (100%) rename {experiments => cmd/experiments}/dsl-parser/README.md (100%) rename {experiments => cmd/experiments}/dsl-parser/one/build (100%) rename {experiments => cmd/experiments}/dsl-parser/one/go.mod (100%) rename {experiments => cmd/experiments}/dsl-parser/one/go.sum (100%) rename {experiments => cmd/experiments}/dsl-parser/one/main.go (100%) rename {experiments => cmd/experiments}/dsl-parser/one/run (100%) rename {experiments => cmd/experiments}/dsl-parser/one/semi1.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/one/semi2.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/two/brackets.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/two/build (100%) rename {experiments => cmd/experiments}/dsl-parser/two/emit01.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/two/emit02.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/two/go.mod (100%) rename {experiments => cmd/experiments}/dsl-parser/two/go.sum (100%) rename {experiments => cmd/experiments}/dsl-parser/two/main.go (100%) rename {experiments => cmd/experiments}/dsl-parser/two/mktags (100%) rename {experiments => cmd/experiments}/dsl-parser/two/run (100%) rename {experiments => cmd/experiments}/dsl-parser/two/semi3.bnf (100%) rename {experiments => cmd/experiments}/dsl-parser/two/temp.bnf (100%) rename {experiments => cmd/experiments}/gc-raw-notes.txt (100%) rename {experiments => cmd/experiments}/line-parser/README.txt (100%) rename {experiments => cmd/experiments}/line-parser/read-string.go (100%) rename {experiments => cmd/experiments}/line-parser/scanner.go (100%) rename {experiments => cmd/experiments}/line-parser/splitter.go (100%) diff --git a/Makefile b/Makefile index 263ace6c8..2d3f1d19e 100644 --- a/Makefile +++ b/Makefile @@ -59,11 +59,6 @@ bench-input: regression-test: go test -v regression_test.go -# ---------------------------------------------------------------- -# Experimental executables: -scan: - go build github.com/johnkerl/miller/cmd/scan - # ---------------------------------------------------------------- # Formatting # go fmt ./... finds experimental C files which we want to ignore. @@ -121,4 +116,4 @@ release_tarball: build check # ================================================================ # Go does its own dependency management, outside of make. -.PHONY: build mlr scan check unit_test regression_test bench fmt staticcheck dev docs +.PHONY: build mlr check unit_test regression_test bench fmt staticcheck dev docs diff --git a/experiments/cli-parser/arch/args_notwindows.go b/cmd/experiments/cli-parser/arch/args_notwindows.go similarity index 100% rename from experiments/cli-parser/arch/args_notwindows.go rename to cmd/experiments/cli-parser/arch/args_notwindows.go diff --git a/experiments/cli-parser/arch/args_windows.go b/cmd/experiments/cli-parser/arch/args_windows.go similarity index 100% rename from experiments/cli-parser/arch/args_windows.go rename to cmd/experiments/cli-parser/arch/args_windows.go diff --git a/experiments/cli-parser/cliparse.go b/cmd/experiments/cli-parser/cliparse.go similarity index 100% rename from experiments/cli-parser/cliparse.go rename to cmd/experiments/cli-parser/cliparse.go diff --git a/experiments/cli-parser/go.mod b/cmd/experiments/cli-parser/go.mod similarity index 100% rename from experiments/cli-parser/go.mod rename to cmd/experiments/cli-parser/go.mod diff --git a/experiments/cli-parser/go.sum b/cmd/experiments/cli-parser/go.sum similarity index 100% rename from experiments/cli-parser/go.sum rename to cmd/experiments/cli-parser/go.sum diff --git a/experiments/cli-parser/msys2in.sh b/cmd/experiments/cli-parser/msys2in.sh similarity index 100% rename from experiments/cli-parser/msys2in.sh rename to cmd/experiments/cli-parser/msys2in.sh diff --git a/experiments/cli-parser/msys2out.bat b/cmd/experiments/cli-parser/msys2out.bat similarity index 100% rename from experiments/cli-parser/msys2out.bat rename to cmd/experiments/cli-parser/msys2out.bat diff --git a/experiments/dsl-parser/.vimrc b/cmd/experiments/dsl-parser/.vimrc similarity index 100% rename from experiments/dsl-parser/.vimrc rename to cmd/experiments/dsl-parser/.vimrc diff --git a/experiments/dsl-parser/README.md b/cmd/experiments/dsl-parser/README.md similarity index 100% rename from experiments/dsl-parser/README.md rename to cmd/experiments/dsl-parser/README.md diff --git a/experiments/dsl-parser/one/build b/cmd/experiments/dsl-parser/one/build similarity index 100% rename from experiments/dsl-parser/one/build rename to cmd/experiments/dsl-parser/one/build diff --git a/experiments/dsl-parser/one/go.mod b/cmd/experiments/dsl-parser/one/go.mod similarity index 100% rename from experiments/dsl-parser/one/go.mod rename to cmd/experiments/dsl-parser/one/go.mod diff --git a/experiments/dsl-parser/one/go.sum b/cmd/experiments/dsl-parser/one/go.sum similarity index 100% rename from experiments/dsl-parser/one/go.sum rename to cmd/experiments/dsl-parser/one/go.sum diff --git a/experiments/dsl-parser/one/main.go b/cmd/experiments/dsl-parser/one/main.go similarity index 100% rename from experiments/dsl-parser/one/main.go rename to cmd/experiments/dsl-parser/one/main.go diff --git a/experiments/dsl-parser/one/run b/cmd/experiments/dsl-parser/one/run similarity index 100% rename from experiments/dsl-parser/one/run rename to cmd/experiments/dsl-parser/one/run diff --git a/experiments/dsl-parser/one/semi1.bnf b/cmd/experiments/dsl-parser/one/semi1.bnf similarity index 100% rename from experiments/dsl-parser/one/semi1.bnf rename to cmd/experiments/dsl-parser/one/semi1.bnf diff --git a/experiments/dsl-parser/one/semi2.bnf b/cmd/experiments/dsl-parser/one/semi2.bnf similarity index 100% rename from experiments/dsl-parser/one/semi2.bnf rename to cmd/experiments/dsl-parser/one/semi2.bnf diff --git a/experiments/dsl-parser/two/brackets.bnf b/cmd/experiments/dsl-parser/two/brackets.bnf similarity index 100% rename from experiments/dsl-parser/two/brackets.bnf rename to cmd/experiments/dsl-parser/two/brackets.bnf diff --git a/experiments/dsl-parser/two/build b/cmd/experiments/dsl-parser/two/build similarity index 100% rename from experiments/dsl-parser/two/build rename to cmd/experiments/dsl-parser/two/build diff --git a/experiments/dsl-parser/two/emit01.bnf b/cmd/experiments/dsl-parser/two/emit01.bnf similarity index 100% rename from experiments/dsl-parser/two/emit01.bnf rename to cmd/experiments/dsl-parser/two/emit01.bnf diff --git a/experiments/dsl-parser/two/emit02.bnf b/cmd/experiments/dsl-parser/two/emit02.bnf similarity index 100% rename from experiments/dsl-parser/two/emit02.bnf rename to cmd/experiments/dsl-parser/two/emit02.bnf diff --git a/experiments/dsl-parser/two/go.mod b/cmd/experiments/dsl-parser/two/go.mod similarity index 100% rename from experiments/dsl-parser/two/go.mod rename to cmd/experiments/dsl-parser/two/go.mod diff --git a/experiments/dsl-parser/two/go.sum b/cmd/experiments/dsl-parser/two/go.sum similarity index 100% rename from experiments/dsl-parser/two/go.sum rename to cmd/experiments/dsl-parser/two/go.sum diff --git a/experiments/dsl-parser/two/main.go b/cmd/experiments/dsl-parser/two/main.go similarity index 100% rename from experiments/dsl-parser/two/main.go rename to cmd/experiments/dsl-parser/two/main.go diff --git a/experiments/dsl-parser/two/mktags b/cmd/experiments/dsl-parser/two/mktags similarity index 100% rename from experiments/dsl-parser/two/mktags rename to cmd/experiments/dsl-parser/two/mktags diff --git a/experiments/dsl-parser/two/run b/cmd/experiments/dsl-parser/two/run similarity index 100% rename from experiments/dsl-parser/two/run rename to cmd/experiments/dsl-parser/two/run diff --git a/experiments/dsl-parser/two/semi3.bnf b/cmd/experiments/dsl-parser/two/semi3.bnf similarity index 100% rename from experiments/dsl-parser/two/semi3.bnf rename to cmd/experiments/dsl-parser/two/semi3.bnf diff --git a/experiments/dsl-parser/two/temp.bnf b/cmd/experiments/dsl-parser/two/temp.bnf similarity index 100% rename from experiments/dsl-parser/two/temp.bnf rename to cmd/experiments/dsl-parser/two/temp.bnf diff --git a/experiments/gc-raw-notes.txt b/cmd/experiments/gc-raw-notes.txt similarity index 100% rename from experiments/gc-raw-notes.txt rename to cmd/experiments/gc-raw-notes.txt diff --git a/experiments/line-parser/README.txt b/cmd/experiments/line-parser/README.txt similarity index 100% rename from experiments/line-parser/README.txt rename to cmd/experiments/line-parser/README.txt diff --git a/experiments/line-parser/read-string.go b/cmd/experiments/line-parser/read-string.go similarity index 100% rename from experiments/line-parser/read-string.go rename to cmd/experiments/line-parser/read-string.go diff --git a/experiments/line-parser/scanner.go b/cmd/experiments/line-parser/scanner.go similarity index 100% rename from experiments/line-parser/scanner.go rename to cmd/experiments/line-parser/scanner.go diff --git a/experiments/line-parser/splitter.go b/cmd/experiments/line-parser/splitter.go similarity index 100% rename from experiments/line-parser/splitter.go rename to cmd/experiments/line-parser/splitter.go diff --git a/internal/pkg/auxents/auxents.go b/internal/pkg/auxents/auxents.go index ef87d510e..b9cc3f64f 100644 --- a/internal/pkg/auxents/auxents.go +++ b/internal/pkg/auxents/auxents.go @@ -62,14 +62,6 @@ func Dispatch(args []string) { // Else, return control to main for the rest of Miller. } -// auxListUsage shows the available auxents. -func auxListUsage(verbName string, o *os.File, exitCode int) { - fmt.Fprintf(o, "Usage: mlr %s [options]\n", verbName) - fmt.Fprintf(o, "Options:\n") - fmt.Fprintf(o, "-h or --help: print this message\n") - os.Exit(exitCode) -} - // auxListMain is the handler for 'mlr aux-list'. func auxListMain(args []string) int { ShowAuxEntries(os.Stdout) diff --git a/internal/pkg/auxents/regtest/invoker.go b/internal/pkg/auxents/regtest/invoker.go index 3067cb1b0..fdbac801b 100644 --- a/internal/pkg/auxents/regtest/invoker.go +++ b/internal/pkg/auxents/regtest/invoker.go @@ -24,7 +24,6 @@ func RunMillerCommand( stdout string, stderr string, exitCode int, - executionError error, // failure to even start the process ) { argsString = strings.TrimRight(argsString, "\n") argsString = strings.TrimRight(argsString, "\r") @@ -59,7 +58,7 @@ func RunMillerCommand( } } - return stdout, stderr, exitCode, nil + return stdout, stderr, exitCode } // RunDiffCommandOnStrings runs either diff or fc (not-Windows / Windows diff --git a/internal/pkg/auxents/regtest/regtester.go b/internal/pkg/auxents/regtest/regtester.go index 6a2a340a0..191c613b4 100644 --- a/internal/pkg/auxents/regtest/regtester.go +++ b/internal/pkg/auxents/regtest/regtester.go @@ -57,7 +57,6 @@ package regtest import ( "container/list" - "errors" "fmt" "io/ioutil" "os" @@ -422,14 +421,6 @@ func (regtester *RegTester) executeSingleCmdFile( expectFailFileName := caseDir + slash + ShouldFailName postCompareFileName := caseDir + slash + PostCompareName - cmd, err = regtester.loadFile(cmdFilePath, caseDir) - if err != nil { - if verbosityLevel >= 2 { - fmt.Printf("%s: %v\n", cmdFilePath, err) - } - return false - } - if verbosityLevel >= 2 { fmt.Println("Command:") fmt.Println(cmd) @@ -502,7 +493,7 @@ func (regtester *RegTester) executeSingleCmdFile( // **************************************************************** // HERE IS WHERE WE RUN THE MILLER COMMAND LINE FOR THE TEST CASE - actualStdout, actualStderr, actualExitCode, err := RunMillerCommand(regtester.exeName, cmd) + actualStdout, actualStderr, actualExitCode := RunMillerCommand(regtester.exeName, cmd) // **************************************************************** // Unset any case-specific environment variables after running the case. @@ -859,11 +850,9 @@ func (regtester *RegTester) loadEnvFile( } fields := strings.SplitN(line, "=", 2) if len(fields) != 2 { - return nil, errors.New( - fmt.Sprintf( - "mlr: could not parse line \"%s\" from file \"%s\".\n", - line, filename, - ), + return nil, fmt.Errorf( + "mlr: could not parse line \"%s\" from file \"%s\".\n", + line, filename, ) } keyValuePairs.Put(fields[0], fields[1]) @@ -898,11 +887,9 @@ func (regtester *RegTester) loadStringPairFile( } fields := strings.SplitN(line, " ", 2) // TODO: split on multi-space if len(fields) != 2 { - return nil, errors.New( - fmt.Sprintf( - "mlr: could not parse line \"%s\" from file \"%s\".\n", - line, filename, - ), + return nil, fmt.Errorf( + "mlr: could not parse line \"%s\" from file \"%s\".\n", + line, filename, ) } pair := stringPair{first: fields[0], second: fields[1]} diff --git a/internal/pkg/dsl/ast_build.go b/internal/pkg/dsl/ast_build.go index 3a1a14edc..512a47942 100644 --- a/internal/pkg/dsl/ast_build.go +++ b/internal/pkg/dsl/ast_build.go @@ -6,7 +6,6 @@ package dsl import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/lib" @@ -255,12 +254,7 @@ func (node *ASTNode) CheckArity( arity int, ) error { if len(node.Children) != arity { - return errors.New( - fmt.Sprintf( - "AST node arity %d, expected %d", - len(node.Children), arity, - ), - ) + return fmt.Errorf("expected AST node arity %d, got %d", arity, len(node.Children)) } else { return nil } diff --git a/internal/pkg/dsl/cst/block-exit.go b/internal/pkg/dsl/cst/block-exit.go index d255cd9a8..3a912a990 100644 --- a/internal/pkg/dsl/cst/block-exit.go +++ b/internal/pkg/dsl/cst/block-exit.go @@ -6,7 +6,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" "github.com/johnkerl/miller/internal/pkg/lib" @@ -70,7 +70,7 @@ func (root *RootNode) BuildReturnNode(astNode *dsl.ASTNode) (*ReturnNode, error) } else { lib.InternalCodingErrorIf(true) } - return nil, errors.New("Internal coding error: Statement should not be reached.") + return nil, fmt.Errorf("internal coding error: statement should not be reached.") } func (node *ReturnNode) Execute(state *runtime.State) (*BlockExitPayload, error) { diff --git a/internal/pkg/dsl/cst/builtin_functions.go b/internal/pkg/dsl/cst/builtin_functions.go index bc110b406..922e4fdcc 100644 --- a/internal/pkg/dsl/cst/builtin_functions.go +++ b/internal/pkg/dsl/cst/builtin_functions.go @@ -5,7 +5,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/bifs" @@ -53,9 +52,9 @@ func (root *RootNode) BuildBuiltinFunctionCallsiteNode( } else if builtinFunctionInfo.variadicFuncWithState != nil { return root.BuildVariadicFunctionWithStateCallsiteNode(astNode, builtinFunctionInfo) } else { - return nil, errors.New( - "CST BuildFunctionCallsiteNode: builtin function not implemented yet: " + - functionName, + return nil, fmt.Errorf( + "at CST BuildFunctionCallsiteNode: builtin function not implemented yet: %s", + functionName, ) } } @@ -79,11 +78,9 @@ func (root *RootNode) BuildMultipleArityFunctionCallsiteNode( return root.BuildTernaryFunctionCallsiteNode(astNode, builtinFunctionInfo) } - return nil, errors.New( - fmt.Sprintf( - "CST BuildMultipleArityFunctionCallsiteNode: function name not found: " + - builtinFunctionInfo.name, - ), + return nil, fmt.Errorf( + "at CST BuildMultipleArityFunctionCallsiteNode: function name not found: " + + builtinFunctionInfo.name, ) } @@ -99,14 +96,12 @@ func (root *RootNode) BuildZaryFunctionCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 0 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -134,14 +129,12 @@ func (root *RootNode) BuildUnaryFunctionCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 1 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -175,14 +168,12 @@ func (root *RootNode) BuildUnaryFunctionWithContextCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 1 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -217,14 +208,12 @@ func (root *RootNode) BuildBinaryFunctionCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 2 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -293,14 +282,12 @@ func (root *RootNode) BuildBinaryFunctionWithStateCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 2 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -345,14 +332,12 @@ func (root *RootNode) BuildTernaryFunctionWithStateCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 3 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -433,14 +418,12 @@ func (root *RootNode) BuildRegexCaptureBinaryFunctionCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 2 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -487,14 +470,12 @@ func (root *RootNode) BuildDotCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 2 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - ".", - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + ".", + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -551,14 +532,12 @@ func (root *RootNode) BuildTernaryFunctionCallsiteNode( callsiteArity := len(astNode.Children) expectedArity := 3 if callsiteArity != expectedArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - builtinFunctionInfo.name, - callsiteArity, - lib.Plural(callsiteArity), - expectedArity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + builtinFunctionInfo.name, + callsiteArity, + lib.Plural(callsiteArity), + expectedArity, ) } @@ -618,25 +597,21 @@ func (root *RootNode) BuildVariadicFunctionCallsiteNode( callsiteArity := len(astNode.Children) if callsiteArity < builtinFunctionInfo.minimumVariadicArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s takes minimum argument count %d; got %d.\n", - builtinFunctionInfo.name, - builtinFunctionInfo.minimumVariadicArity, - callsiteArity, - ), + return nil, fmt.Errorf( + "mlr: function %s takes minimum argument count %d; got %d.\n", + builtinFunctionInfo.name, + builtinFunctionInfo.minimumVariadicArity, + callsiteArity, ) } if builtinFunctionInfo.maximumVariadicArity != 0 { if callsiteArity > builtinFunctionInfo.maximumVariadicArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s takes maximum argument count %d; got %d.\n", - builtinFunctionInfo.name, - builtinFunctionInfo.maximumVariadicArity, - callsiteArity, - ), + return nil, fmt.Errorf( + "mlr: function %s takes maximum argument count %d; got %d.\n", + builtinFunctionInfo.name, + builtinFunctionInfo.maximumVariadicArity, + callsiteArity, ) } } @@ -680,25 +655,21 @@ func (root *RootNode) BuildVariadicFunctionWithStateCallsiteNode( callsiteArity := len(astNode.Children) if callsiteArity < builtinFunctionInfo.minimumVariadicArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s takes minimum argument count %d; got %d.\n", - builtinFunctionInfo.name, - builtinFunctionInfo.minimumVariadicArity, - callsiteArity, - ), + return nil, fmt.Errorf( + "mlr: function %s takes minimum argument count %d; got %d.\n", + builtinFunctionInfo.name, + builtinFunctionInfo.minimumVariadicArity, + callsiteArity, ) } if builtinFunctionInfo.maximumVariadicArity != 0 { if callsiteArity > builtinFunctionInfo.maximumVariadicArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s takes maximum argument count %d; got %d.\n", - builtinFunctionInfo.name, - builtinFunctionInfo.maximumVariadicArity, - callsiteArity, - ), + return nil, fmt.Errorf( + "mlr: function %s takes maximum argument count %d; got %d.\n", + builtinFunctionInfo.name, + builtinFunctionInfo.maximumVariadicArity, + callsiteArity, ) } } diff --git a/internal/pkg/dsl/cst/cond.go b/internal/pkg/dsl/cst/cond.go index 28e6cf336..030bf9f4f 100644 --- a/internal/pkg/dsl/cst/cond.go +++ b/internal/pkg/dsl/cst/cond.go @@ -6,7 +6,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" "github.com/johnkerl/miller/internal/pkg/lib" @@ -57,7 +57,7 @@ func (node *CondBlockNode) Execute( boolValue = false } else if !isBool { // TODO: line-number/token info for the DSL expression. - return nil, errors.New("mlr: conditional expression did not evaluate to boolean.") + return nil, fmt.Errorf("mlr: conditional expression did not evaluate to boolean.") } if boolValue == true { diff --git a/internal/pkg/dsl/cst/dump.go b/internal/pkg/dsl/cst/dump.go index b0353132e..a53e43a1b 100644 --- a/internal/pkg/dsl/cst/dump.go +++ b/internal/pkg/dsl/cst/dump.go @@ -17,7 +17,6 @@ package cst import ( "bytes" - "errors" "fmt" "os" "strings" @@ -138,12 +137,7 @@ func (root *RootNode) buildDumpxStatementNode( } else if redirectorNode.Type == dsl.NodeTypeRedirectPipe { retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions) } else { - return nil, errors.New( - fmt.Sprintf( - "%s: unhandled redirector node type %s.", - "mlr", string(redirectorNode.Type), - ), - ) + return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type)) } } } @@ -216,11 +210,9 @@ func (node *DumpStatementNode) dumpToFileOrPipe( ) error { redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state) if !redirectorTarget.IsString() { - return errors.New( - fmt.Sprintf( - "%s: output redirection yielded %s, not string.", - "mlr", redirectorTarget.GetTypeName(), - ), + return fmt.Errorf( + "mlr: output redirection yielded %s, not string.", + redirectorTarget.GetTypeName(), ) } outputFileName := redirectorTarget.String() diff --git a/internal/pkg/dsl/cst/emit_emitp.go b/internal/pkg/dsl/cst/emit_emitp.go index 9433ca51b..9af7a63f7 100644 --- a/internal/pkg/dsl/cst/emit_emitp.go +++ b/internal/pkg/dsl/cst/emit_emitp.go @@ -39,7 +39,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/cli" @@ -171,11 +170,9 @@ func (root *RootNode) buildEmitXStatementNode( retval.topLevelEvaluableMap = evaluable } else { - return nil, errors.New( - fmt.Sprintf( - "mlr: unlashe-demit node types must be local variables, field names, oosvars, or maps; got %s.", - childNode.Type, - ), + return nil, fmt.Errorf( + "mlr: unlashed-emit node types must be local variables, field names, oosvars, or maps; got %s.", + childNode.Type, ) } @@ -183,11 +180,9 @@ func (root *RootNode) buildEmitXStatementNode( retval.isLashed = true for _, childNode := range emittablesNode.Children { if !EMITX_NAMED_NODE_TYPES[childNode.Type] { - return nil, errors.New( - fmt.Sprintf( - "mlr: lashed-emit node types must be local variables, field names, or oosvars; got %s.", - childNode.Type, - ), + return nil, fmt.Errorf( + "mlr: lashed-emit node types must be local variables, field names, or oosvars; got %s.", + childNode.Type, ) } } @@ -276,12 +271,7 @@ func (root *RootNode) buildEmitXStatementNode( } else if redirectorNode.Type == dsl.NodeTypeRedirectPipe { retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions) } else { - return nil, errors.New( - fmt.Sprintf( - "%s: unhandled redirector node type %s.", - "mlr", string(redirectorNode.Type), - ), - ) + return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type)) } } } @@ -999,12 +989,7 @@ func (node *EmitXStatementNode) emitRecordToFileOrPipe( ) error { redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state) if !redirectorTarget.IsString() { - return errors.New( - fmt.Sprintf( - "%s: output redirection yielded %s, not string.", - "mlr", redirectorTarget.GetTypeName(), - ), - ) + return fmt.Errorf("mlr: output redirection yielded %s, not string.", redirectorTarget.GetTypeName()) } outputFileName := redirectorTarget.String() diff --git a/internal/pkg/dsl/cst/emitf.go b/internal/pkg/dsl/cst/emitf.go index 33426f784..213149e86 100644 --- a/internal/pkg/dsl/cst/emitf.go +++ b/internal/pkg/dsl/cst/emitf.go @@ -6,7 +6,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" @@ -120,12 +119,7 @@ func (root *RootNode) BuildEmitFStatementNode(astNode *dsl.ASTNode) (IExecutable } else if redirectorNode.Type == dsl.NodeTypeRedirectPipe { retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions) } else { - return nil, errors.New( - fmt.Sprintf( - "%s: unhandled redirector node type %s.", - "mlr", string(redirectorNode.Type), - ), - ) + return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type)) } } } @@ -169,12 +163,7 @@ func getNameFromNamedNode(astNode *dsl.ASTNode, description string) (string, err } else if astNode.Type == dsl.NodeTypeDirectFieldValue { return string(astNode.Token.Lit), nil } - return "", errors.New( - fmt.Sprintf( - "%s: can't get name of node type \"%s\" for %s.", - "mlr", string(astNode.Type), description, - ), - ) + return "", fmt.Errorf("mlr: can't get name of node type \"%s\" for %s.", string(astNode.Type), description) } // ---------------------------------------------------------------- @@ -198,12 +187,7 @@ func (node *EmitFStatementNode) emitfToFileOrPipe( ) error { redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state) if !redirectorTarget.IsString() { - return errors.New( - fmt.Sprintf( - "%s: output redirection yielded %s, not string.", - "mlr", redirectorTarget.GetTypeName(), - ), - ) + return fmt.Errorf("mlr: output redirection yielded %s, not string.", redirectorTarget.GetTypeName()) } outputFileName := redirectorTarget.String() diff --git a/internal/pkg/dsl/cst/evaluable.go b/internal/pkg/dsl/cst/evaluable.go index bcca85559..203ee438b 100644 --- a/internal/pkg/dsl/cst/evaluable.go +++ b/internal/pkg/dsl/cst/evaluable.go @@ -7,7 +7,6 @@ package cst import ( - "errors" "fmt" "os" @@ -80,8 +79,8 @@ func (root *RootNode) BuildEvaluableNode(astNode *dsl.ASTNode) (IEvaluable, erro return root.BuildUnnamedUDFNode(astNode) } - return nil, errors.New( - "CST BuildEvaluableNode: unhandled AST node type " + string(astNode.Type), + return nil, fmt.Errorf( + "at CST BuildEvaluableNode: unhandled AST node type %s", string(astNode.Type), ) } diff --git a/internal/pkg/dsl/cst/for.go b/internal/pkg/dsl/cst/for.go index 14008bf3f..1cad2fadb 100644 --- a/internal/pkg/dsl/cst/for.go +++ b/internal/pkg/dsl/cst/for.go @@ -5,7 +5,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" "github.com/johnkerl/miller/internal/pkg/lib" @@ -195,11 +195,9 @@ func (node *ForLoopOneVariableNode) Execute(state *runtime.State) (*BlockExitPay // silent zero-pass. But maybe we should surface it as an error. Maybe // with a "mlr put --errors" flag or something. // } else { - // return nil, errors.New( - // fmt.Sprintf( - // "mlr: looped-over item is not a map or array; got %s", - // indexMlrval.GetTypeName(), - // ), + // return nil, fmt.Errorf( + // "mlr: looped-over item is not a map or array; got %s", + // indexMlrval.GetTypeName(), // ) return nil, nil @@ -390,11 +388,9 @@ func (node *ForLoopTwoVariableNode) Execute(state *runtime.State) (*BlockExitPay // silent zero-pass. But maybe we should surface it as an error. Maybe // with a "mlr put --errors" flag or something. // } else { - // return nil, errors.New( - // fmt.Sprintf( - // "mlr: looped-over item is not a map or array; got %s", - // indexMlrval.GetTypeName(), - // ), + // return nil, fmt.Errorf( + // "mlr: looped-over item is not a map or array; got %s", + // indexMlrval.GetTypeName(), // ) return nil, nil @@ -605,11 +601,9 @@ func (node *ForLoopMultivariableNode) executeOuter( // silent zero-pass. But maybe we should surface it as an error. Maybe // with a "mlr put --errors" flag or something. // } else { - // return nil, errors.New( - // fmt.Sprintf( - // "mlr: looped-over item is not a map or array; got %s", - // mv.GetTypeName(), - // ), + // return nil, fmt.Errorf( + // "mlr: looped-over item is not a map or array; got %s", + // mv.GetTypeName(), // ) return nil, nil @@ -702,11 +696,9 @@ func (node *ForLoopMultivariableNode) executeInner( // silent zero-pass. But maybe we should surface it as an error. Maybe // with a "mlr put --errors" flag or something. // } else { - // return nil, errors.New( - // fmt.Sprintf( - // "mlr: looped-over item is not a map or array; got %s", - // mv.GetTypeName(), - // ), + // return nil, fmt.Errorf( + // "mlr: looped-over item is not a map or array; got %s", + // mv.GetTypeName(), // ) return nil, nil @@ -808,7 +800,7 @@ func (root *RootNode) BuildTripleForLoopNode(astNode *dsl.ASTNode) (*TripleForLo precontinuationAssignments = make([]IExecutable, n-1) for i := 0; i < n-1; i++ { if continuationExpressionASTNode.Children[i].Type != dsl.NodeTypeAssignment { - return nil, errors.New( + return nil, fmt.Errorf( "mlr: the non-final triple-for continutation statements must be assignments.", ) } @@ -825,11 +817,11 @@ func (root *RootNode) BuildTripleForLoopNode(astNode *dsl.ASTNode) (*TripleForLo bareBooleanASTNode := continuationExpressionASTNode.Children[n-1] if bareBooleanASTNode.Type != dsl.NodeTypeBareBoolean { if n == 1 { - return nil, errors.New( + return nil, fmt.Errorf( "mlr: the triple-for continutation statement must be a bare boolean.", ) } else { - return nil, errors.New( + return nil, fmt.Errorf( "mlr: the final triple-for continutation statement must be a bare boolean.", ) } @@ -902,7 +894,7 @@ func (node *TripleForLoopNode) Execute(state *runtime.State) (*BlockExitPayload, boolValue, isBool := continuationValue.GetBoolValue() if !isBool { // TODO: propagate line-number context - return nil, errors.New("mlr: for-loop continuation did not evaluate to boolean.") + return nil, fmt.Errorf("mlr: for-loop continuation did not evaluate to boolean.") } if boolValue == false { break diff --git a/internal/pkg/dsl/cst/if.go b/internal/pkg/dsl/cst/if.go index 853cb8b95..d49def006 100644 --- a/internal/pkg/dsl/cst/if.go +++ b/internal/pkg/dsl/cst/if.go @@ -5,7 +5,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" "github.com/johnkerl/miller/internal/pkg/lib" @@ -127,7 +127,7 @@ func (node *IfChainNode) Execute(state *runtime.State) (*BlockExitPayload, error boolValue, isBool := condition.GetBoolValue() if !isBool { // TODO: line-number/token info for the DSL expression. - return nil, errors.New("mlr: conditional expression did not evaluate to boolean.") + return nil, fmt.Errorf("mlr: conditional expression did not evaluate to boolean.") } if boolValue == true { blockExitPayload, err := ifItem.statementBlockNode.Execute(state) diff --git a/internal/pkg/dsl/cst/leaves.go b/internal/pkg/dsl/cst/leaves.go index 7726493f8..0063d5924 100644 --- a/internal/pkg/dsl/cst/leaves.go +++ b/internal/pkg/dsl/cst/leaves.go @@ -5,7 +5,7 @@ package cst import ( - "errors" + "fmt" "math" "github.com/johnkerl/miller/internal/pkg/dsl" @@ -97,9 +97,7 @@ func (root *RootNode) BuildLeafNode( break } - return nil, errors.New( - "CST BuildLeafNode: unhandled AST node " + string(astNode.Type), - ) + return nil, fmt.Errorf("at CST BuildLeafNode: unhandled AST node %s", string(astNode.Type)) } // ---------------------------------------------------------------- @@ -443,9 +441,7 @@ func (root *RootNode) BuildContextVariableNode(astNode *dsl.ASTNode) (IEvaluable } - return nil, errors.New( - "CST BuildContextVariableNode: unhandled context variable " + sval, - ) + return nil, fmt.Errorf("at CST BuildContextVariableNode: unhandled context variable %s", sval) } // ---------------------------------------------------------------- @@ -620,9 +616,7 @@ func (root *RootNode) BuildConstantNode(astNode *dsl.ASTNode) (IEvaluable, error } - return nil, errors.New( - "CST BuildContextVariableNode: unhandled context variable " + sval, - ) + return nil, fmt.Errorf("at CST BuildContextVariableNode: unhandled context variable %s", sval) } // ---------------------------------------------------------------- diff --git a/internal/pkg/dsl/cst/lvalues.go b/internal/pkg/dsl/cst/lvalues.go index 34dae1532..a13739557 100644 --- a/internal/pkg/dsl/cst/lvalues.go +++ b/internal/pkg/dsl/cst/lvalues.go @@ -6,7 +6,6 @@ package cst import ( - "errors" "fmt" "os" @@ -54,12 +53,12 @@ func (root *RootNode) BuildAssignableNode( break case dsl.NodeTypeArrayOrMapPositionalNameAccess: - return nil, errors.New( + return nil, fmt.Errorf( "mlr: '[[...]]' is allowed on assignment left-hand sides only when immediately preceded by '$'.", ) break case dsl.NodeTypeArrayOrMapPositionalValueAccess: - return nil, errors.New( + return nil, fmt.Errorf( "mlr: '[[[...]]]' is allowed on assignment left-hand sides only when immediately preceded by '$'.", ) break @@ -77,8 +76,8 @@ func (root *RootNode) BuildAssignableNode( break } - return nil, errors.New( - "CST BuildAssignableNode: unhandled AST node " + string(astNode.Type), + return nil, fmt.Errorf( + "at CST BuildAssignableNode: unhandled AST node " + string(astNode.Type), ) } @@ -121,7 +120,7 @@ func (node *DirectFieldValueLvalueNode) AssignIndexed( // print inrec attributes. Also, a UDF/UDS invoked from begin/end could try // to access the inrec, and that would get past the validator. if state.Inrec == nil { - return errors.New("There is no current record to assign to.") + return fmt.Errorf("there is no current record to assign to.") } // AssignmentNode checks for absent, so we just assign whatever we get @@ -220,7 +219,7 @@ func (node *IndirectFieldValueLvalueNode) AssignIndexed( // print inrec attributes. Also, a UDF/UDS invoked from begin/end could try // to access the inrec, and that would get past the validator. if state.Inrec == nil { - return errors.New("There is no current record to assign to.") + return fmt.Errorf("there is no current record to assign to.") } lhsFieldName := node.lhsFieldNameExpression.Evaluate(state) @@ -313,7 +312,7 @@ func (node *PositionalFieldNameLvalueNode) Assign( // print inrec attributes. Also, a UDF/UDS invoked from begin/end could try // to access the inrec, and that would get past the validator. if state.Inrec == nil { - return errors.New("There is no current record to assign to.") + return fmt.Errorf("there is no current record to assign to.") } lhsFieldIndex := node.lhsFieldIndexExpression.Evaluate(state) @@ -324,11 +323,9 @@ func (node *PositionalFieldNameLvalueNode) Assign( state.Inrec.PutNameWithPositionalIndex(index, rvalue) return nil } else { - return errors.New( - fmt.Sprintf( - "mlr: positional index for $[[...]] assignment must be integer; got %s.", - lhsFieldIndex.GetTypeName(), - ), + return fmt.Errorf( + "mlr: positional index for $[[...]] assignment must be integer; got %s.", + lhsFieldIndex.GetTypeName(), ) } } @@ -340,7 +337,7 @@ func (node *PositionalFieldNameLvalueNode) AssignIndexed( ) error { // TODO: reconsider this if /when we decide to allow string-slice // assignments. - return errors.New( + return fmt.Errorf( "mlr: $[[...]] = ... expressions are not indexable.", ) } @@ -433,7 +430,7 @@ func (node *PositionalFieldValueLvalueNode) AssignIndexed( // print inrec attributes. Also, a UDF/UDS invoked from begin/end could try // to access the inrec, and that would get past the validator. if state.Inrec == nil { - return errors.New("There is no current record to assign to.") + return fmt.Errorf("there is no current record to assign to.") } lhsFieldIndex := node.lhsFieldIndexExpression.Evaluate(state) @@ -450,11 +447,9 @@ func (node *PositionalFieldValueLvalueNode) AssignIndexed( state.Inrec.PutCopyWithPositionalIndex(index, rvalue) return nil } else { - return errors.New( - fmt.Sprintf( - "mlr: positional index for $[[[...]]] assignment must be integer; got %s.", - lhsFieldIndex.GetTypeName(), - ), + return fmt.Errorf( + "mlr: positional index for $[[[...]]] assignment must be integer; got %s.", + lhsFieldIndex.GetTypeName(), ) } } else { @@ -536,7 +531,7 @@ func (node *FullSrecLvalueNode) AssignIndexed( // print inrec attributes. Also, a UDF/UDS invoked from begin/end could try // to access the inrec, and that would get past the validator. if state.Inrec == nil { - return errors.New("There is no current record to assign to.") + return fmt.Errorf("there is no current record to assign to.") } // AssignmentNode checks for absentness of the rvalue, so we just assign @@ -1095,12 +1090,10 @@ func (node *EnvironmentVariableLvalueNode) Assign( } if !name.IsString() { - return errors.New( - fmt.Sprintf( - "ENV[...] assignments must have string names; got %s \"%s\"\n", - name.GetTypeName(), - name.String(), - ), + return fmt.Errorf( + "assignments to ENV[...] must have string names; got %s \"%s\"\n", + name.GetTypeName(), + name.String(), ) } @@ -1118,7 +1111,7 @@ func (node *EnvironmentVariableLvalueNode) AssignIndexed( indices []*mlrval.Mlrval, state *runtime.State, ) error { - return errors.New("mlr: ENV[...] cannot be indexed.") + return fmt.Errorf("mlr: ENV[...] cannot be indexed.") } func (node *EnvironmentVariableLvalueNode) Unassign( diff --git a/internal/pkg/dsl/cst/print.go b/internal/pkg/dsl/cst/print.go index bf3789e11..a7318c398 100644 --- a/internal/pkg/dsl/cst/print.go +++ b/internal/pkg/dsl/cst/print.go @@ -6,7 +6,6 @@ package cst import ( "bytes" - "errors" "fmt" "os" @@ -281,12 +280,7 @@ func (root *RootNode) buildPrintxStatementNode( } else if redirectorNode.Type == dsl.NodeTypeRedirectPipe { retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions) } else { - return nil, errors.New( - fmt.Sprintf( - "%s: unhandled redirector node type %s.", - "mlr", string(redirectorNode.Type), - ), - ) + return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type)) } } } @@ -362,12 +356,7 @@ func (node *PrintStatementNode) printToFileOrPipe( ) error { redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state) if !redirectorTarget.IsString() { - return errors.New( - fmt.Sprintf( - "%s: output redirection yielded %s, not string.", - "mlr", redirectorTarget.GetTypeName(), - ), - ) + return fmt.Errorf("mlr: output redirection yielded %s, not string.", redirectorTarget.GetTypeName()) } outputFileName := redirectorTarget.String() diff --git a/internal/pkg/dsl/cst/root.go b/internal/pkg/dsl/cst/root.go index 27636bc02..4a959f722 100644 --- a/internal/pkg/dsl/cst/root.go +++ b/internal/pkg/dsl/cst/root.go @@ -7,7 +7,6 @@ package cst import ( "container/list" - "errors" "fmt" "os" "strings" @@ -148,7 +147,7 @@ func (root *RootNode) IngestAST( warningsAreFatal bool, ) error { if ast.RootNode == nil { - return errors.New("Cannot build CST from nil AST root") + return fmt.Errorf("cannot build CST from nil AST root") } // Check for things that are syntax errors but not done in the AST for @@ -283,9 +282,7 @@ func (root *RootNode) regexProtectPrePassAux(astNode *dsl.ASTNode) { func (root *RootNode) buildMainPass(ast *dsl.AST, isReplImmediate bool) error { if ast.RootNode.Type != dsl.NodeTypeStatementBlock { - return errors.New( - "CST root build: non-statement-block AST root node unhandled", - ) + return fmt.Errorf("at CST root build: non-statement-block AST root node unhandled") } astChildren := ast.RootNode.Children @@ -411,9 +408,7 @@ func (root *RootNode) resolveSubroutineCallsites() error { return err } if uds == nil { - return errors.New( - "mlr: subroutine name not found: " + subroutineName, - ) + return fmt.Errorf("mlr: subroutine name not found: " + subroutineName) } unresolvedSubroutineCallsite.uds = uds diff --git a/internal/pkg/dsl/cst/statements.go b/internal/pkg/dsl/cst/statements.go index e5577e071..1ea5ca026 100644 --- a/internal/pkg/dsl/cst/statements.go +++ b/internal/pkg/dsl/cst/statements.go @@ -6,7 +6,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" ) @@ -67,13 +67,9 @@ func (root *RootNode) BuildStatementNode( return root.BuildEmitPStatementNode(astNode) case dsl.NodeTypeBeginBlock: - return nil, errors.New( - "mlr: begin blocks may only be declared at top level.", - ) + return nil, fmt.Errorf("mlr: begin blocks may only be declared at top level.") case dsl.NodeTypeEndBlock: - return nil, errors.New( - "mlr: end blocks may only be declared at top level.", - ) + return nil, fmt.Errorf("mlr: end blocks may only be declared at top level.") case dsl.NodeTypeIfChain: return root.BuildIfChainNode(astNode) @@ -93,13 +89,9 @@ func (root *RootNode) BuildStatementNode( return root.BuildTripleForLoopNode(astNode) case dsl.NodeTypeNamedFunctionDefinition: - return nil, errors.New( - "mlr: functions may only be declared at top level.", - ) + return nil, fmt.Errorf("mlr: functions may only be declared at top level.") case dsl.NodeTypeSubroutineDefinition: - return nil, errors.New( - "mlr: subroutines may only be declared at top level.", - ) + return nil, fmt.Errorf("mlr: subroutines may only be declared at top level.") case dsl.NodeTypeSubroutineCallsite: return root.BuildSubroutineCallsiteNode(astNode) @@ -111,9 +103,7 @@ func (root *RootNode) BuildStatementNode( return root.BuildReturnNode(astNode) default: - return nil, errors.New( - "CST BuildStatementNode: unhandled AST node " + string(astNode.Type), - ) + return nil, fmt.Errorf("at CST BuildStatementNode: unhandled AST node %s", string(astNode.Type)) break } return statement, nil diff --git a/internal/pkg/dsl/cst/tee.go b/internal/pkg/dsl/cst/tee.go index c433bce02..b76ac4320 100644 --- a/internal/pkg/dsl/cst/tee.go +++ b/internal/pkg/dsl/cst/tee.go @@ -5,7 +5,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" @@ -122,12 +121,7 @@ func (root *RootNode) BuildTeeStatementNode(astNode *dsl.ASTNode) (IExecutable, } else if redirectorNode.Type == dsl.NodeTypeRedirectPipe { retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions) } else { - return nil, errors.New( - fmt.Sprintf( - "%s: unhandled redirector node type %s.", - "mlr", string(redirectorNode.Type), - ), - ) + return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type)) } } @@ -144,12 +138,7 @@ func (root *RootNode) BuildTeeStatementNode(astNode *dsl.ASTNode) (IExecutable, func (node *TeeStatementNode) Execute(state *runtime.State) (*BlockExitPayload, error) { expression := node.expressionEvaluable.Evaluate(state) if !expression.IsMap() { - return nil, errors.New( - fmt.Sprintf( - "%s: tee-evaluaiton yielded %s, not map.", - "mlr", expression.GetTypeName(), - ), - ) + return nil, fmt.Errorf("mlr: tee-evaluaiton yielded %s, not map.", expression.GetTypeName()) } err := node.teeToRedirectFunc(expression.GetMap(), state) return nil, err @@ -162,12 +151,7 @@ func (node *TeeStatementNode) teeToFileOrPipe( ) error { redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state) if !redirectorTarget.IsString() { - return errors.New( - fmt.Sprintf( - "%s: output redirection yielded %s, not string.", - "mlr", redirectorTarget.GetTypeName(), - ), - ) + return fmt.Errorf("mlr: output redirection yielded %s, not string.", redirectorTarget.GetTypeName()) } outputFileName := redirectorTarget.String() diff --git a/internal/pkg/dsl/cst/udf.go b/internal/pkg/dsl/cst/udf.go index 734f0f72d..40b33b3e5 100644 --- a/internal/pkg/dsl/cst/udf.go +++ b/internal/pkg/dsl/cst/udf.go @@ -5,7 +5,6 @@ package cst import ( - "errors" "fmt" "os" @@ -324,14 +323,12 @@ func (manager *UDFManager) LookUp(functionName string, callsiteArity int) (*UDF, return nil, nil } if udf.signature.arity != callsiteArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: function %s invoked with %d argument%s; expected %d", - functionName, - callsiteArity, - lib.Plural(callsiteArity), - udf.signature.arity, - ), + return nil, fmt.Errorf( + "mlr: function %s invoked with %d argument%s; expected %d", + functionName, + callsiteArity, + lib.Plural(callsiteArity), + udf.signature.arity, ) } return udf, nil @@ -391,21 +388,17 @@ func (root *RootNode) BuildAndInstallUDF(astNode *dsl.ASTNode) error { functionName := string(astNode.Token.Lit) if BuiltinFunctionManagerInstance.LookUp(functionName) != nil { - return errors.New( - fmt.Sprintf( - "mlr: function named \"%s\" must not override a built-in function of the same name.", - functionName, - ), + return fmt.Errorf( + "mlr: function named \"%s\" must not override a built-in function of the same name.", + functionName, ) } if !root.allowUDFUDSRedefinitions { if root.udfManager.ExistsByName(functionName) { - return errors.New( - fmt.Sprintf( - "mlr: function named \"%s\" has already been defined.", - functionName, - ), + return fmt.Errorf( + "mlr: function named \"%s\" has already been defined.", + functionName, ) } } diff --git a/internal/pkg/dsl/cst/uds.go b/internal/pkg/dsl/cst/uds.go index 7bc8ec91a..944db7a11 100644 --- a/internal/pkg/dsl/cst/uds.go +++ b/internal/pkg/dsl/cst/uds.go @@ -5,7 +5,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" @@ -174,14 +173,12 @@ func (manager *UDSManager) LookUp(subroutineName string, callsiteArity int) (*UD return nil, nil } if uds.signature.arity != callsiteArity { - return nil, errors.New( - fmt.Sprintf( - "mlr: subroutine %s invoked with %d argument%s; expected %d", - subroutineName, - callsiteArity, - lib.Plural(callsiteArity), - uds.signature.arity, - ), + return nil, fmt.Errorf( + "mlr: subroutine %s invoked with %d argument%s; expected %d", + subroutineName, + callsiteArity, + lib.Plural(callsiteArity), + uds.signature.arity, ) } return uds, nil @@ -244,11 +241,9 @@ func (root *RootNode) BuildAndInstallUDS(astNode *dsl.ASTNode) error { if !root.allowUDFUDSRedefinitions { if root.udsManager.ExistsByName(subroutineName) { - return errors.New( - fmt.Sprintf( - "mlr: subroutine named \"%s\" has already been defined.", - subroutineName, - ), + return fmt.Errorf( + "mlr: subroutine named \"%s\" has already been defined.", + subroutineName, ) } } diff --git a/internal/pkg/dsl/cst/validate.go b/internal/pkg/dsl/cst/validate.go index 9ed13adf0..23a0047ed 100644 --- a/internal/pkg/dsl/cst/validate.go +++ b/internal/pkg/dsl/cst/validate.go @@ -7,7 +7,6 @@ package cst import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" @@ -32,7 +31,7 @@ func ValidateAST( // But filter '' is an error. if ast.RootNode.Children == nil || len(ast.RootNode.Children) == 0 { if dslInstanceType == DSLInstanceTypeFilter { - return errors.New("mlr: filter statement must not be empty.") + return fmt.Errorf("mlr: filter statement must not be empty.") } } @@ -82,7 +81,7 @@ func validateASTAux( if astNode.Type == dsl.NodeTypeFilterStatement { if dslInstanceType == DSLInstanceTypeFilter { - return errors.New( + return fmt.Errorf( "mlr: filter expressions must not also contain the \"filter\" keyword.", ) } @@ -91,21 +90,21 @@ func validateASTAux( // Check: begin/end/func/subr must be at top-level if astNode.Type == dsl.NodeTypeBeginBlock { if !atTopLevel { - return errors.New( + return fmt.Errorf( "mlr: begin blocks can only be at top level.", ) } nextLevelInBeginOrEnd = true } else if astNode.Type == dsl.NodeTypeEndBlock { if !atTopLevel { - return errors.New( + return fmt.Errorf( "mlr: end blocks can only be at top level.", ) } nextLevelInBeginOrEnd = true } else if astNode.Type == dsl.NodeTypeNamedFunctionDefinition { if !atTopLevel { - return errors.New( + return fmt.Errorf( "mlr: func blocks can only be at top level.", ) } @@ -114,7 +113,7 @@ func validateASTAux( nextLevelInUDF = true } else if astNode.Type == dsl.NodeTypeSubroutineDefinition { if !atTopLevel { - return errors.New( + return fmt.Errorf( "mlr: subr blocks can only be at top level.", ) } @@ -136,7 +135,7 @@ func validateASTAux( if astNode.Type == dsl.NodeTypeDirectFieldValue || astNode.Type == dsl.NodeTypeIndirectFieldValue || astNode.Type == dsl.NodeTypeFullSrec { - return errors.New( + return fmt.Errorf( "mlr: begin/end blocks cannot refer to records via $x, $*, etc.", ) } @@ -145,7 +144,7 @@ func validateASTAux( // Check: break/continue outside of loop if !inLoop { if astNode.Type == dsl.NodeTypeBreak { - return errors.New( + return fmt.Errorf( "mlr: break statements are only valid within for/do/while loops.", ) } @@ -153,7 +152,7 @@ func validateASTAux( if !inLoop { if astNode.Type == dsl.NodeTypeContinue { - return errors.New( + return fmt.Errorf( "mlr: break statements are only valid within for/do/while loops.", ) } @@ -171,7 +170,7 @@ func validateASTAux( // Check: return outside of func/subr if !inUDF && !inUDS { if astNode.Type == dsl.NodeTypeReturn { - return errors.New( + return fmt.Errorf( "mlr: return statements are only valid within func/subr blocks.", ) } @@ -181,14 +180,14 @@ func validateASTAux( if astNode.Type == dsl.NodeTypeReturn { if inUDF { if len(astNode.Children) != 1 { - return errors.New( + return fmt.Errorf( "mlr: return statements in func blocks must return a value.", ) } } if inUDS { if len(astNode.Children) != 0 { - return errors.New( + return fmt.Errorf( "mlr: return statements in subr blocks must not return a value.", ) } @@ -199,11 +198,9 @@ func validateASTAux( if isAssignmentLHS { ok := VALID_LHS_NODE_TYPES[astNode.Type] if !ok { - return errors.New( - fmt.Sprintf( - "mlr: %s is not valid on the left-hand side of an assignment.", - astNode.Type, - ), + return fmt.Errorf( + "mlr: %s is not valid on the left-hand side of an assignment.", + astNode.Type, ) } } @@ -212,11 +209,9 @@ func validateASTAux( if isUnset { ok := VALID_LHS_NODE_TYPES[astNode.Type] if !ok { - return errors.New( - fmt.Sprintf( - "mlr: %s is not valid for unset statement.", - astNode.Type, - ), + return fmt.Errorf( + "mlr: %s is not valid for unset statement.", + astNode.Type, ) } } @@ -268,13 +263,7 @@ func validateForLoopTwoVariableUniqueNames(astNode *dsl.ASTNode) error { keyVarName := string(keyVarNode.Token.Lit) valVarName := string(valVarNode.Token.Lit) if keyVarName == valVarName { - return errors.New( - fmt.Sprintf( - "%s: redefinition of variable %s in the same scope.", - "mlr", - keyVarName, - ), - ) + return fmt.Errorf("mlr: redefinition of variable %s in the same scope.", keyVarName) } else { return nil } @@ -304,26 +293,14 @@ func validateForLoopMultivariableUniqueNames(astNode *dsl.ASTNode) error { name := string(keyVarNode.Token.Lit) _, present := seen[name] if present { - return errors.New( - fmt.Sprintf( - "%s: redefinition of variable %s in the same scope.", - "mlr", - name, - ), - ) + return fmt.Errorf("mlr: redefinition of variable %s in the same scope.", name) } seen[name] = true } valVarName := string(valVarNode.Token.Lit) if seen[valVarName] { - return errors.New( - fmt.Sprintf( - "%s: redefinition of variable %s in the same scope.", - "mlr", - valVarName, - ), - ) + return fmt.Errorf("mlr: redefinition of variable %s in the same scope.", valVarName) } return nil diff --git a/internal/pkg/dsl/cst/while.go b/internal/pkg/dsl/cst/while.go index e3d0d2eda..d9ec69e5c 100644 --- a/internal/pkg/dsl/cst/while.go +++ b/internal/pkg/dsl/cst/while.go @@ -5,7 +5,7 @@ package cst import ( - "errors" + "fmt" "github.com/johnkerl/miller/internal/pkg/dsl" "github.com/johnkerl/miller/internal/pkg/lib" @@ -54,7 +54,7 @@ func (node *WhileLoopNode) Execute(state *runtime.State) (*BlockExitPayload, err boolValue, isBool := condition.GetBoolValue() if !isBool { // TODO: line-number/token info for the DSL expression. - return nil, errors.New("mlr: conditional expression did not evaluate to boolean.") + return nil, fmt.Errorf("mlr: conditional expression did not evaluate to boolean.") } if boolValue != true { break @@ -144,7 +144,7 @@ func (node *DoWhileLoopNode) Execute(state *runtime.State) (*BlockExitPayload, e boolValue, isBool := condition.GetBoolValue() if !isBool { // TODO: line-number/token info for the DSL expression. - return nil, errors.New("mlr: conditional expression did not evaluate to boolean.") + return nil, fmt.Errorf("mlr: conditional expression did not evaluate to boolean.") } if boolValue == false { break diff --git a/internal/pkg/input/pseudo_reader_gen.go b/internal/pkg/input/pseudo_reader_gen.go index 21172583d..003c75b40 100644 --- a/internal/pkg/input/pseudo_reader_gen.go +++ b/internal/pkg/input/pseudo_reader_gen.go @@ -2,7 +2,6 @@ package input import ( "container/list" - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/bifs" @@ -124,9 +123,7 @@ func (reader *PseudoReaderGen) tryParse( ) (*mlrval.Mlrval, error) { mvalue := mlrval.FromDeferredType(svalue) if mvalue == nil || !mvalue.IsNumeric() { - return nil, errors.New( - fmt.Sprintf("mlr: gen: %s \"%s\" is not parseable as number", name, svalue), - ) + return nil, fmt.Errorf("mlr: gen: %s \"%s\" is not parseable as number", name, svalue) } return mvalue, nil } diff --git a/internal/pkg/input/record_reader_csv.go b/internal/pkg/input/record_reader_csv.go index 1aef46b8d..b1e93cefa 100644 --- a/internal/pkg/input/record_reader_csv.go +++ b/internal/pkg/input/record_reader_csv.go @@ -4,7 +4,6 @@ import ( "bytes" "container/list" "encoding/csv" - "errors" "fmt" "io" "strconv" @@ -33,10 +32,10 @@ func NewRecordReaderCSV( recordsPerBatch int, ) (*RecordReaderCSV, error) { if readerOptions.IRS != "\n" && readerOptions.IRS != "\r\n" { - return nil, errors.New("CSV IRS cannot be altered; LF vs CR/LF is autodetected") + return nil, fmt.Errorf("for CSV, IRS cannot be altered; LF vs CR/LF is autodetected") } if len(readerOptions.IFS) != 1 { - return nil, errors.New("CSV IFS can only be a single character") + return nil, fmt.Errorf("for CSV, IFS can only be a single character") } return &RecordReaderCSV{ readerOptions: readerOptions, @@ -236,12 +235,10 @@ func (reader *RecordReaderCSV) getRecordBatch( } else { if !reader.readerOptions.AllowRaggedCSVInput { - err := errors.New( - fmt.Sprintf( - "mlr: CSV header/data length mismatch %d != %d "+ - "at filename %s row %d.\n", - nh, nd, reader.filename, reader.rowNumber, - ), + err := fmt.Errorf( + "mlr: CSV header/data length mismatch %d != %d "+ + "at filename %s row %d.\n", + nh, nd, reader.filename, reader.rowNumber, ) errorChannel <- err return diff --git a/internal/pkg/input/record_reader_csvlite.go b/internal/pkg/input/record_reader_csvlite.go index aaca53239..e42fc25bb 100644 --- a/internal/pkg/input/record_reader_csvlite.go +++ b/internal/pkg/input/record_reader_csvlite.go @@ -20,7 +20,6 @@ package input import ( "container/list" - "errors" "fmt" "io" "strconv" @@ -228,12 +227,10 @@ func getRecordBatchExplicitCSVHeader( // Get data lines on subsequent loop iterations } else { if !reader.readerOptions.AllowRaggedCSVInput && len(reader.headerStrings) != len(fields) { - err := errors.New( - fmt.Sprintf( - "mlr: CSV header/data length mismatch %d != %d "+ - "at filename %s line %d.\n", - len(reader.headerStrings), len(fields), filename, reader.inputLineNumber, - ), + err := fmt.Errorf( + "mlr: CSV header/data length mismatch %d != %d "+ + "at filename %s line %d.\n", + len(reader.headerStrings), len(fields), filename, reader.inputLineNumber, ) errorChannel <- err return @@ -348,12 +345,10 @@ func getRecordBatchImplicitCSVHeader( } } else { if !reader.readerOptions.AllowRaggedCSVInput && len(reader.headerStrings) != len(fields) { - err := errors.New( - fmt.Sprintf( - "mlr: CSV header/data length mismatch %d != %d "+ - "at filename %s line %d.\n", - len(reader.headerStrings), len(fields), filename, reader.inputLineNumber, - ), + err := fmt.Errorf( + "mlr: CSV header/data length mismatch %d != %d "+ + "at filename %s line %d.\n", + len(reader.headerStrings), len(fields), filename, reader.inputLineNumber, ) errorChannel <- err return diff --git a/internal/pkg/input/record_reader_factory.go b/internal/pkg/input/record_reader_factory.go index b6f4fa829..9505c8687 100644 --- a/internal/pkg/input/record_reader_factory.go +++ b/internal/pkg/input/record_reader_factory.go @@ -1,7 +1,6 @@ package input import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/cli" @@ -26,6 +25,6 @@ func Create(readerOptions *cli.TReaderOptions, recordsPerBatch int) (IRecordRead case "gen": return NewPseudoReaderGen(readerOptions, recordsPerBatch) default: - return nil, errors.New(fmt.Sprintf("input file format \"%s\" not found", readerOptions.InputFileFormat)) + return nil, fmt.Errorf("input file format \"%s\" not found", readerOptions.InputFileFormat) } } diff --git a/internal/pkg/input/record_reader_json.go b/internal/pkg/input/record_reader_json.go index 7084e95c8..eeb87f1ab 100644 --- a/internal/pkg/input/record_reader_json.go +++ b/internal/pkg/input/record_reader_json.go @@ -3,7 +3,6 @@ package input import ( "bufio" "container/list" - "errors" "fmt" "io" "strings" @@ -126,7 +125,7 @@ func (reader *RecordReaderJSON) processHandle( // TODO: make a helper method record := mlrval.GetMap() if record == nil { - errorChannel <- errors.New("Internal coding error detected in JSON record-reader") + errorChannel <- fmt.Errorf("internal coding error detected in JSON record-reader") return } context.UpdateForInputRecord() @@ -140,24 +139,22 @@ func (reader *RecordReaderJSON) processHandle( } else if mlrval.IsArray() { records := mlrval.GetArray() if records == nil { - errorChannel <- errors.New("Internal coding error detected in JSON record-reader") + errorChannel <- fmt.Errorf("internal coding error detected in JSON record-reader") return } for _, mlrval := range records { if !mlrval.IsMap() { // TODO: more context - errorChannel <- errors.New( - fmt.Sprintf( - "Valid but unmillerable JSON. Expected map (JSON object); got %s.", - mlrval.GetTypeName(), - ), + errorChannel <- fmt.Errorf( + "valid but unmillerable JSON. Expected map (JSON object); got %s.", + mlrval.GetTypeName(), ) return } record := mlrval.GetMap() if record == nil { - errorChannel <- errors.New("Internal coding error detected in JSON record-reader") + errorChannel <- fmt.Errorf("internal coding error detected in JSON record-reader") return } context.UpdateForInputRecord() @@ -170,11 +167,9 @@ func (reader *RecordReaderJSON) processHandle( } } else { - errorChannel <- errors.New( - fmt.Sprintf( - "Valid but unmillerable JSON. Expected map (JSON object); got %s.", - mlrval.GetTypeName(), - ), + errorChannel <- fmt.Errorf( + "valid but unmillerable JSON. Expected map (JSON object); got %s.", + mlrval.GetTypeName(), ) return } diff --git a/internal/pkg/input/record_reader_xtab.go b/internal/pkg/input/record_reader_xtab.go index c2ddc7e68..c8c78e0d2 100644 --- a/internal/pkg/input/record_reader_xtab.go +++ b/internal/pkg/input/record_reader_xtab.go @@ -3,7 +3,7 @@ package input import ( "bufio" "container/list" - "errors" + "fmt" "io" "regexp" "strings" @@ -311,7 +311,7 @@ func (s *tXTABIPSSplitter) Split(input string) (key, value string, err error) { // Empty string is a length-0 return value. n := len(input) if n == 0 { - return "", "", errors.New("mlr: internal coding error in XTAB reader") + return "", "", fmt.Errorf("internal coding error in XTAB reader") } // ' abc 123' splits as key '', value 'abc 123'. @@ -360,12 +360,12 @@ type tXTABIPSRegexSplitter struct { func (s *tXTABIPSRegexSplitter) Split(input string) (key, value string, err error) { kv := lib.RegexSplitString(s.ipsRegex, input, 2) if len(kv) == 0 { - return "", "", errors.New("mlr: internal coding error in XTAB reader") + return "", "", fmt.Errorf("internal coding error in XTAB reader") } else if len(kv) == 1 { return kv[0], "", nil } else if len(kv) == 2 { return kv[0], kv[1], nil } else { - return "", "", errors.New("mlr: internal coding error in XTAB reader") + return "", "", fmt.Errorf("internal coding error in XTAB reader") } } diff --git a/internal/pkg/lib/file-readers.go b/internal/pkg/lib/file-readers.go index f497fbdc3..ffd38762c 100644 --- a/internal/pkg/lib/file-readers.go +++ b/internal/pkg/lib/file-readers.go @@ -24,7 +24,7 @@ import ( "compress/bzip2" "compress/gzip" "compress/zlib" - "errors" + "fmt" "io" "net/http" "os" @@ -236,10 +236,10 @@ func IsUpdateableInPlace( if strings.HasPrefix(filename, "http://") || strings.HasPrefix(filename, "https://") || strings.HasPrefix(filename, "file://") { - return errors.New("http://, https://, and file:// URLs are not updateable in place.") + return fmt.Errorf("http://, https://, and file:// URLs are not updateable in place.") } if prepipe != "" { - return errors.New("input with --prepipe or --prepipex is not updateable in place.") + return fmt.Errorf("input with --prepipe or --prepipex is not updateable in place.") } return nil } @@ -281,7 +281,7 @@ func WrapOutputHandle( ) (io.WriteCloser, bool, error) { switch inputFileEncoding { case FileInputEncodingBzip2: - return fileWriteHandle, false, errors.New("bzip2 is not currently supported for in-place mode.") + return fileWriteHandle, false, fmt.Errorf("bzip2 is not currently supported for in-place mode.") case FileInputEncodingGzip: return gzip.NewWriter(fileWriteHandle), true, nil case FileInputEncodingZlib: diff --git a/internal/pkg/mlrval/mlrmap_accessors.go b/internal/pkg/mlrval/mlrmap_accessors.go index 9a02bae9e..982f80dea 100644 --- a/internal/pkg/mlrval/mlrmap_accessors.go +++ b/internal/pkg/mlrval/mlrmap_accessors.go @@ -2,7 +2,6 @@ package mlrval import ( "bytes" - "errors" "fmt" "strconv" @@ -84,9 +83,7 @@ func (mlrmap *Mlrmap) PutReferenceMaybeDedupe(key string, value *Mlrval, dedupe return newKey, nil } } - return key, errors.New( - fmt.Sprintf("record has too many input fields named \"%s\"", key), - ) + return key, fmt.Errorf("record has too many input fields named \"%s\"", key) } // PutCopy copies the key and value (deep-copying in case the value is array/map). @@ -250,8 +247,8 @@ func (mlrmap *Mlrmap) PutCopyWithMlrvalIndex(key *Mlrval, value *Mlrval) error { mlrmap.PutCopy(key.String(), value) return nil } else { - return errors.New( - "mlr: record/map indices must be string, int, or array thereof; got " + key.GetTypeName(), + return fmt.Errorf( + "mlr: record/map indices must be string, int, or array thereof; got %s", key.GetTypeName(), ) } } @@ -352,9 +349,7 @@ func (mlrmap *Mlrmap) getWithMlrvalArrayIndex(index *Mlrval) (*Mlrval, error) { } if i < n-1 { if !next.IsMap() { - return nil, errors.New( - "mlr: cannot multi-index non-map.", - ) + return nil, fmt.Errorf("mlr: cannot multi-index non-map.") } current = next.mapval } else { @@ -371,8 +366,8 @@ func (mlrmap *Mlrmap) getWithMlrvalSingleIndex(index *Mlrval) (*Mlrval, error) { } else if index.IsInt() { return mlrmap.Get(index.String()), nil } else { - return nil, errors.New( - "Record/map indices must be string, int, or array thereof; got " + index.GetTypeName(), + return nil, fmt.Errorf( + "Record/map indices must be string, int, or array thereof; got %s", index.GetTypeName(), ) } } diff --git a/internal/pkg/mlrval/mlrval_format.go b/internal/pkg/mlrval/mlrval_format.go index 8ecc8df93..9f4cb2fe6 100644 --- a/internal/pkg/mlrval/mlrval_format.go +++ b/internal/pkg/mlrval/mlrval_format.go @@ -1,7 +1,6 @@ package mlrval import ( - "errors" "fmt" "strconv" "strings" @@ -85,13 +84,11 @@ func newFormatter( ) (IFormatter, error) { numPercents := strings.Count(userLevelFormatString, "%") if numPercents < 1 { - return nil, errors.New( - fmt.Sprintf("unhandled format string \"%s\": no leading \"%%\"", userLevelFormatString), - ) + return nil, fmt.Errorf("unhandled format string \"%s\": no leading \"%%\"", userLevelFormatString) } if numPercents > 1 { - return nil, errors.New( - fmt.Sprintf("unhandled format string \"%s\": needs no \"%%\" after the first", userLevelFormatString), + return nil, fmt.Errorf( + "unhandled format string \"%s\": needs no \"%%\" after the first", userLevelFormatString, ) } diff --git a/internal/pkg/mlrval/mlrval_json.go b/internal/pkg/mlrval/mlrval_json.go index 3da3368aa..2215bacb9 100644 --- a/internal/pkg/mlrval/mlrval_json.go +++ b/internal/pkg/mlrval/mlrval_json.go @@ -11,7 +11,7 @@ package mlrval import ( "bytes" "encoding/json" - "errors" + "fmt" "io" "github.com/johnkerl/miller/internal/pkg/colorizer" @@ -105,7 +105,7 @@ func (mv *Mlrval) UnmarshalJSON(inputBytes []byte) error { decoder := json.NewDecoder(bytes.NewReader(inputBytes)) pmv, eof, err := MlrvalDecodeFromJSON(decoder) if eof { - return errors.New("Miller JSON parser: unexpected premature EOF.") + return fmt.Errorf("mlr: JSON parser: unexpected premature EOF.") } if err != nil { return err @@ -155,8 +155,8 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( return mlrval, false, nil } - return nil, false, errors.New( - "Miller JSON reader internal coding error: non-delimiter token unhandled", + return nil, false, fmt.Errorf( + "mlr: JSON reader internal coding error: non-delimiter token unhandled", ) } else { @@ -173,8 +173,8 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( expectedClosingDelimiter = '}' collectionType = "JSON object`" } else { - return nil, false, errors.New( - "Miller JSON reader: Unhandled opening delimiter \"" + string(delimiter) + "\"", + return nil, false, fmt.Errorf( + "mlr: JSON reader: Unhandled opening delimiter \"%s\"", string(delimiter), ) } @@ -186,7 +186,7 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( element, eof, err := MlrvalDecodeFromJSON(decoder) if eof { // xxx constify - return nil, false, errors.New("Miller JSON parser: unexpected premature EOF.") + return nil, false, fmt.Errorf("mlr: JSON parser: unexpected premature EOF.") } if err != nil { return nil, false, err @@ -201,22 +201,22 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( key, eof, err := MlrvalDecodeFromJSON(decoder) if eof { // xxx constify - return nil, false, errors.New("Miller JSON parser: unexpected premature EOF.") + return nil, false, fmt.Errorf("mlr: JSON parser: unexpected premature EOF.") } if err != nil { return nil, false, err } if !key.IsString() { - return nil, false, errors.New( + return nil, false, fmt.Errorf( // TODO: print out what was gotten - "Miller JSON reader: obejct keys must be string-valued.", + "mlr JSON reader: obejct keys must be string-valued.", ) } value, eof, err := MlrvalDecodeFromJSON(decoder) if eof { // xxx constify - return nil, false, errors.New("Miller JSON parser: unexpected premature EOF.") + return nil, false, fmt.Errorf("mlr: JSON parser: unexpected premature EOF.") } if err != nil { return nil, false, err @@ -227,16 +227,15 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( } } - imbalanceError := errors.New( - "Miller JSON reader: did not find closing token '" + - string(expectedClosingDelimiter) + - "' for " + - collectionType, + imbalanceError := fmt.Errorf( + "mlr: JSON reader: did not find closing token \"%s\" for %s", + string(expectedClosingDelimiter), + collectionType, ) endToken, err := decoder.Token() if err == io.EOF { - return nil, false, errors.New("Miller JSON parser: unexpected premature EOF.") + return nil, false, fmt.Errorf("mlr: JSON parser: unexpected premature EOF.") } if err != nil { return nil, false, err @@ -255,7 +254,7 @@ func MlrvalDecodeFromJSON(decoder *json.Decoder) ( return mv, false, nil } - return nil, false, errors.New("unimplemented") + return nil, false, fmt.Errorf("mlr: unimplemented") } // ================================================================ @@ -274,41 +273,30 @@ func (mv *Mlrval) marshalJSONAux( switch mv.Type() { case MT_PENDING: return mv.marshalJSONPending(outputIsStdout) - break case MT_ERROR: return mv.marshalJSONError(outputIsStdout) - break case MT_ABSENT: return mv.marshalJSONAbsent(outputIsStdout) - break case MT_NULL: return mv.marshalJSONNull(outputIsStdout) - break case MT_VOID: return mv.marshalJSONVoid(outputIsStdout) - break case MT_STRING: return mv.marshalJSONString(outputIsStdout) - break case MT_INT: return mv.marshalJSONInt(outputIsStdout) - break case MT_FLOAT: return mv.marshalJSONFloat(outputIsStdout) - break case MT_BOOL: return mv.marshalJSONBool(outputIsStdout) - break case MT_ARRAY: return mv.marshalJSONArray(jsonFormatting, elementNestingDepth, outputIsStdout) - break case MT_MAP: return mv.marshalJSONMap(jsonFormatting, elementNestingDepth, outputIsStdout) - break case MT_DIM: // MT_DIM is one past the last valid type - return "", errors.New("mlr: internal coding error detected") + return "", fmt.Errorf("mlr: internal coding error detected") } - return "", errors.New("mlr: Internal coding error detected") + return "", fmt.Errorf("mlr: Internal coding error detected") } // ================================================================ @@ -317,8 +305,8 @@ func (mv *Mlrval) marshalJSONAux( // ---------------------------------------------------------------- func (mv *Mlrval) marshalJSONPending(outputIsStdout bool) (string, error) { lib.InternalCodingErrorIf(mv.mvtype != MT_PENDING) - return "", errors.New( - "Miller internal coding error: pending-values should not have been produced", + return "", fmt.Errorf( + "mlr: internal coding error: pending-values should not have been produced", ) } @@ -331,8 +319,8 @@ func (mv *Mlrval) marshalJSONError(outputIsStdout bool) (string, error) { // ---------------------------------------------------------------- func (mv *Mlrval) marshalJSONAbsent(outputIsStdout bool) (string, error) { lib.InternalCodingErrorIf(mv.mvtype != MT_ABSENT) - return "", errors.New( - "Miller internal coding error: absent-values should not have been assigned", + return "", fmt.Errorf( + "mlr: internal coding error: absent-values should not have been assigned", ) } diff --git a/internal/pkg/mlrval/mlrval_output.go b/internal/pkg/mlrval/mlrval_output.go index 437f77347..709e6e101 100644 --- a/internal/pkg/mlrval/mlrval_output.go +++ b/internal/pkg/mlrval/mlrval_output.go @@ -42,33 +42,27 @@ func (mv *Mlrval) setPrintRep() { // clearly visually if it should (buggily) slip through to // user-level visibility. mv.printrep = "(bug-if-you-see-this:case=3)" // xxx constdef at top of file - break case MT_ERROR: mv.printrep = "(error)" // xxx constdef at top of file - break case MT_ABSENT: // Callsites should be using absence to do non-assigns, so flag // this clearly visually if it should (buggily) slip through to // user-level visibility. mv.printrep = "(bug-if-you-see-this:case=4)" // xxx constdef at top of file - break case MT_VOID: mv.printrep = "" // xxx constdef at top of file - break case MT_STRING: break case MT_INT: mv.printrep = strconv.Itoa(mv.intval) - break case MT_FLOAT: mv.printrep = strconv.FormatFloat(mv.floatval, 'f', -1, 64) - break case MT_BOOL: if mv.boolval == true { @@ -76,7 +70,6 @@ func (mv *Mlrval) setPrintRep() { } else { mv.printrep = "false" } - break case MT_ARRAY: bytes, err := mv.MarshalJSON(JSON_MULTILINE, false) @@ -87,8 +80,6 @@ func (mv *Mlrval) setPrintRep() { } mv.printrep = string(bytes) - break - case MT_MAP: bytes, err := mv.MarshalJSON(JSON_MULTILINE, false) // maybe just InternalCodingErrorIf(err != nil) @@ -97,8 +88,6 @@ func (mv *Mlrval) setPrintRep() { os.Exit(1) } mv.printrep = string(bytes) - - break } mv.printrepValid = true } diff --git a/internal/pkg/output/file-output-handlers.go b/internal/pkg/output/file-output-handlers.go index d187c9985..75916dfd4 100644 --- a/internal/pkg/output/file-output-handlers.go +++ b/internal/pkg/output/file-output-handlers.go @@ -15,7 +15,6 @@ package output import ( "bufio" "container/list" - "errors" "fmt" "io" "os" @@ -283,13 +282,7 @@ func NewPipeWriteOutputHandler( ) (*FileOutputHandler, error) { writePipe, err := lib.OpenOutboundHalfPipe(commandString) if err != nil { - return nil, errors.New( - fmt.Sprintf( - "%s: could not launch command \"%s\" for pipe-to.", - "mlr", - commandString, - ), - ) + return nil, fmt.Errorf("could not launch command \"%s\" for pipe-to.", commandString) } return newOutputHandlerCommon( diff --git a/internal/pkg/output/record_writer_csv.go b/internal/pkg/output/record_writer_csv.go index 2ebdfd3e2..2767da864 100644 --- a/internal/pkg/output/record_writer_csv.go +++ b/internal/pkg/output/record_writer_csv.go @@ -3,7 +3,7 @@ package output import ( "bufio" "encoding/csv" - "errors" + "fmt" "strings" "github.com/johnkerl/miller/internal/pkg/cli" @@ -23,10 +23,10 @@ type RecordWriterCSV struct { func NewRecordWriterCSV(writerOptions *cli.TWriterOptions) (*RecordWriterCSV, error) { if len(writerOptions.OFS) != 1 { - return nil, errors.New("CSV OFS can only be a single character") + return nil, fmt.Errorf("for CSV, OFS can only be a single character") } if writerOptions.ORS != "\n" && writerOptions.ORS != "\r\n" { - return nil, errors.New("CSV ORS cannot be altered") + return nil, fmt.Errorf("for CSV, ORS cannot be altered") } return &RecordWriterCSV{ writerOptions: writerOptions, diff --git a/internal/pkg/output/record_writer_factory.go b/internal/pkg/output/record_writer_factory.go index b95324716..3649f25bb 100644 --- a/internal/pkg/output/record_writer_factory.go +++ b/internal/pkg/output/record_writer_factory.go @@ -1,7 +1,6 @@ package output import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/cli" @@ -26,6 +25,6 @@ func Create(writerOptions *cli.TWriterOptions) (IRecordWriter, error) { case "xtab": return NewRecordWriterXTAB(writerOptions) default: - return nil, errors.New(fmt.Sprintf("output file format \"%s\" not found", writerOptions.OutputFileFormat)) + return nil, fmt.Errorf("output file format \"%s\" not found", writerOptions.OutputFileFormat) } } diff --git a/internal/pkg/output/record_writer_nidx.go b/internal/pkg/output/record_writer_nidx.go index d7625ca4b..a0e350958 100644 --- a/internal/pkg/output/record_writer_nidx.go +++ b/internal/pkg/output/record_writer_nidx.go @@ -9,8 +9,6 @@ import ( type RecordWriterNIDX struct { writerOptions *cli.TWriterOptions - ofs string - ors string } func NewRecordWriterNIDX(writerOptions *cli.TWriterOptions) (*RecordWriterNIDX, error) { diff --git a/internal/pkg/runtime/stack.go b/internal/pkg/runtime/stack.go index d3db2b0a5..a5cf2499e 100644 --- a/internal/pkg/runtime/stack.go +++ b/internal/pkg/runtime/stack.go @@ -27,7 +27,6 @@ package runtime import ( "container/list" - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/lib" @@ -407,11 +406,9 @@ func (frame *StackFrame) defineTyped( frame.namesToOffsets[stackVariable.name] = offsetInFrame return nil } else { - return errors.New( - fmt.Sprintf( - "%s: variable %s has already been defined in the same scope.", - "mlr", stackVariable.name, - ), + return fmt.Errorf( + "%s: variable %s has already been defined in the same scope.", + "mlr", stackVariable.name, ) } } @@ -431,11 +428,9 @@ func (frame *StackFrame) setIndexed( newval.PutIndexed(indices, mv) return frame.set(stackVariable, newval) } else { - return errors.New( - fmt.Sprintf( - "%s: map indices must be int or string; got %s.\n", - "mlr", leadingIndex.GetTypeName(), - ), + return fmt.Errorf( + "%s: map indices must be int or string; got %s.\n", + "mlr", leadingIndex.GetTypeName(), ) } } else { diff --git a/internal/pkg/transformers/label.go b/internal/pkg/transformers/label.go index c4b5875f5..62439885c 100644 --- a/internal/pkg/transformers/label.go +++ b/internal/pkg/transformers/label.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "strings" @@ -108,12 +107,7 @@ func NewTransformerLabel( for _, newName := range newNames { _, ok := uniquenessChecker[newName] if ok { - return nil, errors.New( - fmt.Sprintf( - "mlr label: labels must be unique; got duplicate \"%s\"\n", - newName, - ), - ) + return nil, fmt.Errorf("mlr label: labels must be unique; got duplicate \"%s\"\n", newName) } uniquenessChecker[newName] = true } diff --git a/internal/pkg/transformers/merge-fields.go b/internal/pkg/transformers/merge-fields.go index 84f421b32..1a9c17ad5 100644 --- a/internal/pkg/transformers/merge-fields.go +++ b/internal/pkg/transformers/merge-fields.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "regexp" @@ -257,11 +256,9 @@ func NewTransformerMergeFields( for _, accumulatorName := range accumulatorNameList { if !utils.ValidateStats1AccumulatorName(accumulatorName) { - return nil, errors.New( - fmt.Sprintf( - "%s %s: accumulator \"%s\" not found.\n", - "mlr", verbNameMergeFields, accumulatorName, - ), + return nil, fmt.Errorf( + "mlr %s: accumulator \"%s\" not found.\n", + verbNameMergeFields, accumulatorName, ) } } diff --git a/internal/pkg/transformers/put-or-filter.go b/internal/pkg/transformers/put-or-filter.go index c7a84901f..4b441593f 100644 --- a/internal/pkg/transformers/put-or-filter.go +++ b/internal/pkg/transformers/put-or-filter.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "strings" @@ -447,12 +446,7 @@ func NewTransformerPut( for _, preset := range presets { pair := strings.SplitN(preset, "=", 2) if len(pair) != 2 { - return nil, errors.New( - fmt.Sprintf( - "mlr: missing \"=\" in preset expression \"%s\".", - preset, - ), - ) + return nil, fmt.Errorf("missing \"=\" in preset expression \"%s\".", preset) } key := pair[0] svalue := pair[1] diff --git a/internal/pkg/transformers/rename.go b/internal/pkg/transformers/rename.go index b422bdc61..c46363c8b 100644 --- a/internal/pkg/transformers/rename.go +++ b/internal/pkg/transformers/rename.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "regexp" @@ -149,7 +148,7 @@ func NewTransformerRename( doGsub bool, ) (*TransformerRename, error) { if len(names)%2 != 0 { - return nil, errors.New("Rename: names string must have even length.") + return nil, fmt.Errorf("mlr rename: names string must have even length") } oldToNewNames := lib.NewOrderedMap() diff --git a/internal/pkg/transformers/seqgen.go b/internal/pkg/transformers/seqgen.go index 427fe1a36..4eddc659b 100644 --- a/internal/pkg/transformers/seqgen.go +++ b/internal/pkg/transformers/seqgen.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "strings" @@ -140,32 +139,17 @@ func NewTransformerSeqgen( fstart, startIsNumeric := start.GetNumericToFloatValue() if !startIsNumeric { - return nil, errors.New( - fmt.Sprintf( - "mlr seqgen: start value should be number; got \"%s\"", - startString, - ), - ) + return nil, fmt.Errorf("mlr seqgen: start value should be number; got \"%s\"", startString) } fstop, stopIsNumeric := stop.GetNumericToFloatValue() if !stopIsNumeric { - return nil, errors.New( - fmt.Sprintf( - "mlr seqgen: stop value should be number; got \"%s\"", - stopString, - ), - ) + return nil, fmt.Errorf("mlr seqgen: stop value should be number; got \"%s\"", stopString) } fstep, stepIsNumeric := step.GetNumericToFloatValue() if !stepIsNumeric { - return nil, errors.New( - fmt.Sprintf( - "mlr seqgen: step value should be number; got \"%s\"", - stepString, - ), - ) + return nil, fmt.Errorf("mlr seqgen: step value should be number; got \"%s\"", stepString) } if fstep > 0 { @@ -176,9 +160,7 @@ func NewTransformerSeqgen( if fstart == fstop { doneComparator = bifs.BIF_equals } else { - return nil, errors.New( - "mlr seqgen: step must not be zero unless start == stop.", - ) + return nil, fmt.Errorf("mlr seqgen: step must not be zero unless start == stop.") } } diff --git a/internal/pkg/transformers/stats1.go b/internal/pkg/transformers/stats1.go index d047c6bfb..542e23f4b 100644 --- a/internal/pkg/transformers/stats1.go +++ b/internal/pkg/transformers/stats1.go @@ -3,7 +3,6 @@ package transformers import ( "bytes" "container/list" - "errors" "fmt" "os" "regexp" @@ -316,12 +315,7 @@ func NewTransformerStats1( ) (*TransformerStats1, error) { for _, name := range accumulatorNameList { if !utils.ValidateStats1AccumulatorName(name) { - return nil, errors.New( - fmt.Sprintf( - "%s stats1: accumulator \"%s\" not found.\n", - "mlr", name, - ), - ) + return nil, fmt.Errorf("mlr stats1: accumulator \"%s\" not found.", name) } } diff --git a/internal/pkg/transformers/stats2.go b/internal/pkg/transformers/stats2.go index ca7b9be1f..0e66c24c7 100644 --- a/internal/pkg/transformers/stats2.go +++ b/internal/pkg/transformers/stats2.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "strings" @@ -210,12 +209,7 @@ func NewTransformerStats2( ) (*TransformerStats2, error) { for _, name := range accumulatorNameList { if !utils.ValidateStats2AccumulatorName(name) { - return nil, errors.New( - fmt.Sprintf( - "%s stats2: accumulator \"%s\" not found.\n", - "mlr", name, - ), - ) + return nil, fmt.Errorf("mlr stats2: accumulator \"%s\" not found.", name) } } diff --git a/internal/pkg/transformers/step.go b/internal/pkg/transformers/step.go index 3f763eb45..f8e4122af 100644 --- a/internal/pkg/transformers/step.go +++ b/internal/pkg/transformers/step.go @@ -2,7 +2,6 @@ package transformers import ( "container/list" - "errors" "fmt" "os" "strings" @@ -180,16 +179,12 @@ func NewTransformerStep( ) (*TransformerStep, error) { if len(stepperNames) == 0 || len(valueFieldNames) == 0 { - return nil, errors.New( - // TODO: parameterize verb here somehow - "mlr step: -a and -f are both required arguments.", - ) + return nil, fmt.Errorf("mlr %s: -a and -f are both required arguments.", verbNameStep) } if len(stringAlphas) != 0 && len(ewmaSuffixes) != 0 { if len(ewmaSuffixes) != len(stringAlphas) { - return nil, errors.New( - // TODO: parameterize verb here somehow - "mlr step: If -d and -o are provided, their values must have the same length.", + return nil, fmt.Errorf( + "mlr %s: If -d and -o are provided, their values must have the same length.", verbNameStep, ) } } diff --git a/internal/pkg/transformers/utils/stats1-accumulators.go b/internal/pkg/transformers/utils/stats1-accumulators.go index 08f21a02e..21a6f2ced 100644 --- a/internal/pkg/transformers/utils/stats1-accumulators.go +++ b/internal/pkg/transformers/utils/stats1-accumulators.go @@ -219,16 +219,13 @@ func (factory *Stats1AccumulatorFactory) MakeNamedAccumulator( valueFieldName, doInterpolatedPercentiles, ) - // We don't return errors.New here. The nominal case is that the stats1 - // verb has already pre-validated accumulator names, and this is just a - // fallback. The accumulators are instantiated for every unique combination - // of group-by field values in the record stream, only as those values are - // encountered: for example, with 'mlr stats1 -a count,sum -f x,y -g - // color,shape', we make a new accumulator the first time we find a record - // with 'color=blue,shape=square' and another the first time we find a - // record with 'color=red,shape=circle', and so on. The right thing is to - // pre-validate names once when the stats1 transformer is being - // instantiated. + // We don't return an error here -- we fatal. The nominal case is that the stats1 verb has already + // pre-validated accumulator names, and this is just a fallback. The accumulators are instantiated for + // every unique combination of group-by field values in the record stream, only as those values are + // encountered: for example, with 'mlr stats1 -a count,sum -f x,y -g color,shape', we make a new + // accumulator the first time we find a record with 'color=blue,shape=square' and another the first time + // we find a record with 'color=red,shape=circle', and so on. The right thing is to pre-validate names + // once when the stats1 transformer is being instantiated. lib.InternalCodingErrorIf(accumulator == nil) return NewStats1NamedAccumulator( diff --git a/internal/pkg/types/mlrval_typing.go b/internal/pkg/types/mlrval_typing.go index 334241c3c..efb4180c2 100644 --- a/internal/pkg/types/mlrval_typing.go +++ b/internal/pkg/types/mlrval_typing.go @@ -6,7 +6,6 @@ package types import ( - "errors" "fmt" "github.com/johnkerl/miller/internal/pkg/mlrval" @@ -25,11 +24,7 @@ func NewTypeGatedMlrvalName( ) (*TypeGatedMlrvalName, error) { typeMask, ok := mlrval.TypeNameToMask(typeName) if !ok { - return nil, errors.New( - fmt.Sprintf( - "mlr: couldn't resolve type name \"%s\".", typeName, - ), - ) + return nil, fmt.Errorf("mlr: couldn't resolve type name \"%s\".", typeName) } return &TypeGatedMlrvalName{ Name: name, @@ -43,11 +38,9 @@ func (tname *TypeGatedMlrvalName) Check(value *mlrval.Mlrval) error { if bit&tname.TypeMask != 0 { return nil } else { - return errors.New( - fmt.Sprintf( - "mlr: couldn't assign variable %s %s from value %s %s\n", - tname.TypeName, tname.Name, value.GetTypeName(), value.String(), - ), + return fmt.Errorf( + "mlr: couldn't assign variable %s %s from value %s %s\n", + tname.TypeName, tname.Name, value.GetTypeName(), value.String(), ) } } diff --git a/test/cases/verb-stats1/0018/experr b/test/cases/verb-stats1/0018/experr index 10ea04fe2..03f44410a 100644 --- a/test/cases/verb-stats1/0018/experr +++ b/test/cases/verb-stats1/0018/experr @@ -1,2 +1 @@ mlr stats1: accumulator "nonesuch" not found. -