@@ -19,9 +19,11 @@ def time: .created_at | gmtime | strftime("%H:%M:%S");
1919def datetime: .created_at | gmtime | strftime("%Y-%m-%dT%H:%M:%SZ");
2020`
2121
22- type jqProcessor func (nostr.Event ) (any , bool , error )
22+ // jqProcessor runs the compiled jq expression against an event and returns the
23+ // rendered output string. When matches is false the event should be skipped.
24+ type jqProcessor func (nostr.Event ) (out string , matches bool , err error )
2325
24- func jqPrepare (expr string ) (jqProcessor , error ) {
26+ func jqPrepare (expr string , raw bool ) (jqProcessor , error ) {
2527 if expr == "" {
2628 return nil , nil
2729 }
@@ -36,30 +38,43 @@ func jqPrepare(expr string) (jqProcessor, error) {
3638 return nil , fmt .Errorf ("failed to compile jq expression: %w" , err )
3739 }
3840
39- return func (evt nostr.Event ) (any , bool , error ) {
41+ return func (evt nostr.Event ) (string , bool , error ) {
4042 input , err := toJQInput (evt )
4143 if err != nil {
42- return nil , false , err
44+ return "" , false , err
4345 }
4446
4547 iter := code .Run (input )
4648 for {
4749 v , ok := iter .Next ()
4850 if ! ok {
49- return v , false , nil
51+ return "" , false , nil
5052 }
5153
5254 if err , ok := v .(error ); ok {
53- return v , false , err
55+ return "" , false , err
5456 }
5557
5658 if jqTruthy (v ) {
57- return v , true , nil
59+ return jqStringify ( v , raw ) , true , nil
5860 }
5961 }
6062 }, nil
6163}
6264
65+ // jqStringify renders a jq result value. In raw mode string results are printed
66+ // without JSON quoting (like `jq -r`); non-string results are always
67+ // JSON-encoded.
68+ func jqStringify (v any , raw bool ) string {
69+ if raw {
70+ if s , ok := v .(string ); ok {
71+ return s
72+ }
73+ }
74+ out , _ := json .MarshalToString (v )
75+ return out
76+ }
77+
6378func toJQInput (v any ) (any , error ) {
6479 data , err := json .Marshal (v )
6580 if err != nil {
0 commit comments