jqfmt icon indicating copy to clipboard operation
jqfmt copied to clipboard

jqfmt deleting parts of a file

Open LeonardoMor opened this issue 1 year ago • 8 comments

I have a file named count_query.jq with the following contents:

def count_uplinks(leaves):
  reduce leaves[] as $leaf ({}; .[$leaf.leaf_switch] += 1);

.[0]
  | map(
      map_values(
        count_uplinks(.)
      )
    )

Here's what happens when I try jqfmt on this file:

❯ jqfmt <count_query.jq
.[0] | map(map_values(count_uplinks(.)))[ble: EOF]

It deletes the function.

Tested with some of its options, but got similar results.

LeonardoMor avatar Aug 28 '24 23:08 LeonardoMor

Thanks, I'll take a look. Probably related to some func-related logic that I started but didn't finish.

On Wed, Aug 28, 2024, 7:17 PM Leonardo Mora @.***> wrote:

I have a file named count_query.jq with the following contents:

def count_uplinks(leafs): reduce leafs[] as $leaf ({}; .[$leaf.leaf_switch] += 1); .[0] | map( map_values( count_uplinks(.) ) )

Here's what happens when I try jqfmt on this file:

❯ jqfmt <count_query.jq .[0] | map(map_values(count_uplinks(.)))[ble: EOF]

It deletes the function.

Tested with some of its options, but got similar results.

— Reply to this email directly, view it on GitHub https://github.com/noperator/jqfmt/issues/5, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABN2P5EYXAFNEFHST3KIRBDZTZLABAVCNFSM6AAAAABNJH3CIKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ4TGMJTGI3TSNI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

noperator avatar Aug 28 '24 23:08 noperator

Oof, I remember running into this one as well while doing some crazy jq stuff at work, but I forgot to mention it since my brain manages to separate work and life quite well lately :)

I also looked a bit into what jqfmt and gojq do, but was unsuccessful in figuring out what was causing this.

I could take one more dive in the code, but it would take too long to remember everything I learnt back then. Waiting for news on this one as well then :)

ervinpopescu avatar Aug 29 '24 11:08 ervinpopescu

Think I got it working.

$ cat iss-5.jq | go run cmd/jqfmt/main.go
def count_uplinks(leaves): reduce leaves[] as $leaf ({}; .[$leaf.leaf_switch] += 1); .[0] | map(map_values(count_uplinks(.)))

Uncommented some func-printed stuff I'd inadvertently removed.

diff --git a/lib.go b/lib.go
index 652127b..012da75 100644
--- a/lib.go
+++ b/lib.go
@@ -166,20 +167,20 @@ func (e *Query) writeTo(s *strings.Builder) {
 		im.writeTo(s)
 		node = prevNode
 	}
-	// for i, fd := range e.FuncDefs {
-	// 	if _, ok := funcDefs[fd.Name]; !ok {
-	// 		funcDefs[fd.Name] = fd.String()
-	// 	}
-	// 	if i > 0 {
-	// 		s.WriteByte(' ')
-	// 	}
-	// 	node += ".FuncDefs"
-	// 	fd.writeTo(s)
-	// 	node = prevNode
-	// }
-	// if len(e.FuncDefs) > 0 {
-	// 	s.WriteByte(' ')
-	// }
+	for i, fd := range e.FuncDefs {
+		// if _, ok := funcDefs[fd.Name]; !ok {
+		// 	funcDefs[fd.Name] = fd.String()
+		// }
+		if i > 0 {
+			s.WriteByte(' ')
+		}
+		node += ".FuncDefs"
+		fd.writeTo(s)
+		node = prevNode
+	}
+	if len(e.FuncDefs) > 0 {
+		s.WriteByte(' ')
+	}
 	if e.Func != "" {
 		s.WriteString(e.Func)
 	} else if e.Term != nil {

noperator avatar Aug 30 '24 20:08 noperator

@LeonardoMor can you please open a PR so we can easily test this and potentially get it merged?

ervinpopescu avatar Oct 18 '24 11:10 ervinpopescu

@ervinpopescu I was going to.

Doing this edit ensures that the function is no longer deleted. But then everything is formatted to be in one line. For functions, I would expect the body of the function to be indented and so on, just like functions in other languages are formatted.

See:

asciicast

LeonardoMor avatar Oct 18 '24 17:10 LeonardoMor

Ah okay, there's more work needed then. Just wanted to remind you about this and get your pulse on it, overall jqfmt feels pretty robust, except for this issue...

Take your time and let me/us know if you need help with anything!

ervinpopescu avatar Oct 18 '24 17:10 ervinpopescu

Submitted #7 to fix this.

The way functions should be formatted truthfully is a feature request. I'd do it myself, but I don't know go. Yet :)

LeonardoMor avatar Oct 18 '24 18:10 LeonardoMor

LOL, same as me then. I don't have the time or patience to dedicate time to learning go (yet :) ). This will be stalled for a while, then, unless @noperator has time to get started on it.

ervinpopescu avatar Oct 18 '24 18:10 ervinpopescu