From a99f173f30586eb111f9d2879ee4255c58c06f57 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Tue, 6 May 2025 23:00:41 +0200 Subject: [PATCH 1/8] Trimmed the intermediate output trailing newline --- tq.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tq.go b/tq.go index 1d501c6..a7980a2 100644 --- a/tq.go +++ b/tq.go @@ -93,7 +93,7 @@ func (t *Tq) Run(input io.Reader, output io.Writer, query string) error { if len(bytes) == 0 { continue } - fmt.Fprintln(output, string(bytes)) + fmt.Fprintf(output, "%s\n", strings.TrimSpace(string(bytes))) } return nil } From 665d2800d123e18b919003ef78b715cedc9058f0 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Tue, 6 May 2025 23:01:14 +0200 Subject: [PATCH 2/8] Replaced non-idiomatic code in the interpreter --- internal/interpreter/interpreter.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/interpreter/interpreter.go b/internal/interpreter/interpreter.go index 8653dc7..d5a5ec6 100644 --- a/internal/interpreter/interpreter.go +++ b/internal/interpreter/interpreter.go @@ -1,8 +1,6 @@ package interpreter import ( - "fmt" - "github.com/mdm-code/tq/v2/internal/ast" ) @@ -110,7 +108,7 @@ func (i *Interpreter) VisitSpan(e ast.Expr) { default: err = &Error{ data: d, - filter: fmt.Sprintf("%s", span), + filter: span.String(), err: ErrTOMLDataType, } } @@ -136,13 +134,11 @@ func (i *Interpreter) VisitIterator(e ast.Expr) { result = append(result, val) } case []any: - for _, val := range v { - result = append(result, val) - } + result = append(result, v...) default: err = &Error{ data: d, - filter: fmt.Sprintf("%s", iter), + filter: iter.String(), err: ErrTOMLDataType, } } @@ -172,7 +168,7 @@ func (i *Interpreter) VisitString(e ast.Expr) { default: err = &Error{ data: d, - filter: fmt.Sprintf("%s", str), + filter: str.String(), err: ErrTOMLDataType, } } @@ -201,7 +197,7 @@ func (i *Interpreter) VisitInteger(e ast.Expr) { default: err = &Error{ data: d, - filter: fmt.Sprintf("%s", integer), + filter: integer.String(), err: ErrTOMLDataType, } } From c213e692aa2957d14cbe19597d6f9b4d0f987a97 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Tue, 6 May 2025 23:34:58 +0200 Subject: [PATCH 3/8] Rendered single output strings without quotes --- cmd/tq/main_test.go | 4 ++-- example_test.go | 2 +- tq.go | 17 +++++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/tq/main_test.go b/cmd/tq/main_test.go index ff4493c..bf3cd34 100644 --- a/cmd/tq/main_test.go +++ b/cmd/tq/main_test.go @@ -44,8 +44,8 @@ func TestMain(t *testing.T) { if err != nil { t.Errorf("should not return an error: %s", err) } - want := `'red' -'green' + want := `red +green ` if have := output.String(); have != want { t.Errorf("have: %s\nwant: %s", have, want) diff --git a/example_test.go b/example_test.go index 58c59d5..d76457f 100644 --- a/example_test.go +++ b/example_test.go @@ -38,7 +38,7 @@ role = "backend" _ = tq.Run(input, &output, query) fmt.Println(output.String()) // Output: - // '10.0.0.1' + // 10.0.0.1 } // ExampleTq_Validate shows how to use the Tq struct to validate whether a diff --git a/tq.go b/tq.go index a7980a2..f0e1761 100644 --- a/tq.go +++ b/tq.go @@ -86,12 +86,17 @@ func (t *Tq) Run(input io.Reader, output io.Writer, query string) error { return err } for _, d := range filteredData { - bytes, err := t.adapter.Marshal(d) - if err != nil { - return err - } - if len(bytes) == 0 { - continue + var bytes []byte + var err error + switch v := d.(type) { + // NOTE: Single strings get quoted when marshalled, and it is misleading. + case string: + bytes = []byte(v) + default: + bytes, err = t.adapter.Marshal(v) + if err != nil { + return err + } } fmt.Fprintf(output, "%s\n", strings.TrimSpace(string(bytes))) } From e8cc32324e96c38fdee12ba14ffc6658bf2143b0 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Tue, 6 May 2025 23:41:27 +0200 Subject: [PATCH 4/8] Aligned documentation with changes done to quoting --- README.md | 6 +++--- cmd/tq/doc.go | 4 ++-- cmd/tq/usage.txt | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6d18e3f..9053a5b 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ EOF Output: -'/home/core/data/gitlab-runner/data' +/home/core/data/gitlab-runner/data ``` @@ -219,8 +219,8 @@ EOF Output: -'10.0.0.1' -'10.0.0.2' +10.0.0.1 +10.0.0.2 ``` diff --git a/cmd/tq/doc.go b/cmd/tq/doc.go index c402125..1e5deb6 100644 --- a/cmd/tq/doc.go +++ b/cmd/tq/doc.go @@ -30,8 +30,8 @@ Example: Output: - '10.0.0.1' - '10.0.0.2' + 10.0.0.1 + 10.0.0.2 Tq is a tool for querying TOML configuration files with a sequence of intuitive filters. It works as a regular Unix filter program reading input data from the diff --git a/cmd/tq/usage.txt b/cmd/tq/usage.txt index f3d6d75..89104e4 100644 --- a/cmd/tq/usage.txt +++ b/cmd/tq/usage.txt @@ -29,8 +29,8 @@ Example: Output: - '10.0.0.1' - '10.0.0.2' + 10.0.0.1 + 10.0.0.2 Tq is a tool for querying TOML configuration files with a sequence of intuitive filters. It works as a regular Unix filter program reading input data from the From b98f53e2948c09a1888f6feee3d84f227b548b4a Mon Sep 17 00:00:00 2001 From: mdm-code Date: Wed, 7 May 2025 22:02:03 +0200 Subject: [PATCH 5/8] Updated go report card in README to point to v2 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9053a5b..6b13385 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ MIT license - - Go report card + + Go report card Go package docs From 6aa92dfe00d22d8a586b00697fa6d079ebf10611 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Wed, 7 May 2025 22:18:08 +0200 Subject: [PATCH 6/8] Changed examples in the documentation --- README.md | 23 ++++++++++++++++++----- cmd/tq/doc.go | 2 +- cmd/tq/usage.txt | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6b13385..c22e2e5 100644 --- a/README.md +++ b/README.md @@ -137,13 +137,13 @@ to an iterator with `[]`, and then (5) query each element of the iterator for ```sh -tq -q ' +<< EOF tq -q ' .runners[] .kubernetes .volumes .host_path[] ."host path" -' << EOF +' [session_server] session_timeout = 1800 @@ -190,7 +190,9 @@ tq -q ' pod_annotations_overwrite_allowed = "" [runners.kubernetes.volumes] EOF +``` +```txt Output: /home/core/data/gitlab-runner/data @@ -205,7 +207,7 @@ with `[]`, and then (3) the IP address is recovered from each of the objects with the quoted key `"ip"`. ```sh -tq -q '.servers[]."ip"' < Date: Wed, 7 May 2025 22:19:05 +0200 Subject: [PATCH 7/8] Fixed typo in the code documentation --- internal/lexer/lexer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/lexer/lexer.go b/internal/lexer/lexer.go index a69f391..39cf6ec 100644 --- a/internal/lexer/lexer.go +++ b/internal/lexer/lexer.go @@ -12,7 +12,7 @@ const ( // lexerOffsetStart declares the initial Lexer offset. lexerOffsetStart = 0 - // lexerLineOffsetStart declares the inital Lexer line offset. + // lexerLineOffsetStart declares the initial Lexer line offset. lexerLineOffsetStart = 0 ) From 21f7822bfcf4dfe4fd4c237527eb13e893ec7710 Mon Sep 17 00:00:00 2001 From: mdm-code Date: Wed, 7 May 2025 22:29:06 +0200 Subject: [PATCH 8/8] Updated license file --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index a43a025..584455d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Michał Adamczyk +Copyright (c) 2025 Michał Adamczyk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal