From 78402a08fae7ace557b7aa9d207ad825b3d9d45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Bustarret?= Date: Wed, 15 Jul 2026 07:57:11 +0000 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refacto(jq):=20isolate=20j?= =?UTF-8?q?son=20conversion=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/output/filter/jq.go | 16 ++------------- pkg/output/filter/json.go | 43 +++++++++++++++++++++++++++++++++++++++ pkg/output/flags.go | 3 +++ 3 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 pkg/output/filter/json.go diff --git a/pkg/output/filter/jq.go b/pkg/output/filter/jq.go index d78d2290..b29aad1b 100644 --- a/pkg/output/filter/jq.go +++ b/pkg/output/filter/jq.go @@ -6,7 +6,6 @@ package filter import ( "context" - "encoding/json" "fmt" "iter" @@ -21,7 +20,7 @@ type JQ struct { func NewJQ(s string) (JQ, error) { query, err := gojq.Parse(s) if err != nil { - return JQ{}, fmt.Errorf("parse jq filter: %w", err) + return JQ{}, fmt.Errorf("invalid jq filter: %w", err) } return JQ{query: query}, nil } @@ -33,18 +32,7 @@ func (j JQ) Filter(ctx context.Context, seq iter.Seq[result.Result]) iter.Seq[re _ = yield(v) return } - buf, err := json.Marshal(v.Ok) - if err != nil { - _ = yield(result.Result{Error: fmt.Errorf("jq to JSON: %w", err)}) - return - } - var raw any - err = json.Unmarshal(buf, &raw) - if err != nil { - _ = yield(result.Result{Error: fmt.Errorf("jq from JSON: %w", err)}) - return - } - iter := j.query.RunWithContext(ctx, raw) + iter := j.query.RunWithContext(ctx, v.Ok) for { v, ok := iter.Next() if !ok { diff --git a/pkg/output/filter/json.go b/pkg/output/filter/json.go new file mode 100644 index 00000000..ab07a8d4 --- /dev/null +++ b/pkg/output/filter/json.go @@ -0,0 +1,43 @@ +/* +SPDX-FileCopyrightText: 2026 Outscale SAS +SPDX-License-Identifier: BSD-3-Clause +*/ +package filter + +import ( + "context" + "encoding/json" + "fmt" + "iter" + + "github.com/outscale/octl/pkg/output/result" +) + +type JSON struct{} + +func (j JSON) Filter(ctx context.Context, seq iter.Seq[result.Result]) iter.Seq[result.Result] { + return func(yield func(result.Result) bool) { + for v := range seq { + if v.Error != nil { + _ = yield(v) + return + } + buf, err := json.Marshal(v.Ok) + if err != nil { + _ = yield(result.Result{Error: fmt.Errorf("to JSON: %w", err)}) + return + } + var raw any + err = json.Unmarshal(buf, &raw) + if err != nil { + _ = yield(result.Result{Error: fmt.Errorf("from JSON: %w", err)}) + return + } + if !yield(result.Result{Ok: raw}) { + return + } + } + } +} + +var _ Interface = JSON{} diff --git a/pkg/output/flags.go b/pkg/output/flags.go index ae130e5f..d72d13bc 100644 --- a/pkg/output/flags.go +++ b/pkg/output/flags.go @@ -45,6 +45,9 @@ func NewFromFlags(fs *pflag.FlagSet, out, contentField string, cols config.Colum } filters = append(filters, jqf) } + if len(filters) > 0 { + filters = slices.Insert(filters, 0, filter.Interface(filter.JSON{})) + } var fmter format.Interface switch out {