@@ -5,7 +5,7 @@ package main
5
5
import (
6
6
"flag"
7
7
"fmt"
8
- "io/ioutil "
8
+ "io"
9
9
"os"
10
10
"path/filepath"
11
11
"sort"
38
38
safe = false
39
39
mongo = false
40
40
omit = false
41
+ dig = false
42
+ annotate = false
41
43
42
44
// If true wrap extracts with an array.
43
45
wrapExtract = false
@@ -72,13 +74,16 @@ func init() {
72
74
flag .BoolVar (& lazy , "z" , lazy , "lazy mode accepts Simple Encoding Notation (quotes and commas mostly optional)" )
73
75
flag .BoolVar (& senOut , "sen" , senOut , "output in Simple Encoding Notation" )
74
76
flag .BoolVar (& tab , "t" , tab , "indent with tabs" )
77
+ flag .BoolVar (& annotate , "annotate" , annotate , "annotate dig extracts with a path comment" )
75
78
flag .Var (& exValue {}, "x" , "extract path" )
76
79
flag .Var (& matchValue {}, "m" , "match equation/script" )
77
80
flag .Var (& delValue {}, "d" , "delete path" )
81
+ flag .BoolVar (& dig , "dig" , dig , "dig into a large document using the tokenizer" )
78
82
flag .BoolVar (& showVersion , "version" , showVersion , "display version and exit" )
79
83
flag .StringVar (& planDef , "a" , planDef , "assembly plan or plan file using @<plan>" )
80
84
flag .BoolVar (& showRoot , "r" , showRoot , "print root if an assemble plan provided" )
81
- flag .StringVar (& prettyOpt , "p" , prettyOpt , `pretty print with the width, depth, and align as <width>.<max-depth>.<align>` )
85
+ flag .StringVar (& prettyOpt , "p" , prettyOpt ,
86
+ `pretty print with the width, depth, and align as <width>.<max-depth>.<align>` )
82
87
flag .BoolVar (& html , "html" , html , "output colored output as HTML" )
83
88
flag .BoolVar (& safe , "safe" , safe , "escape &, <, and > for HTML inclusion" )
84
89
flag .StringVar (& confFile , "f" , confFile , "configuration file (see -help-config), - indicates no file" )
@@ -271,7 +276,7 @@ func run() (err error) {
271
276
if 0 < len (planDef ) {
272
277
if planDef [0 ] != '[' {
273
278
var b []byte
274
- if b , err = ioutil .ReadFile (planDef ); err != nil {
279
+ if b , err = os .ReadFile (planDef ); err != nil {
275
280
return err
276
281
}
277
282
planDef = string (b )
@@ -290,7 +295,11 @@ func run() (err error) {
290
295
var f * os.File
291
296
for _ , file := range files {
292
297
if f , err = os .Open (file ); err == nil {
293
- _ , err = p .ParseReader (f , write )
298
+ if dig {
299
+ err = digParse (f )
300
+ } else {
301
+ _ , err = p .ParseReader (f , write )
302
+ }
294
303
_ = f .Close ()
295
304
}
296
305
if err != nil {
@@ -304,7 +313,12 @@ func run() (err error) {
304
313
}
305
314
}
306
315
if len (files ) == 0 && len (input ) == 0 {
307
- if _ , err = p .ParseReader (os .Stdin , write ); err != nil {
316
+ if dig {
317
+ err = digParse (os .Stdin )
318
+ } else {
319
+ _ , err = p .ParseReader (os .Stdin , write )
320
+ }
321
+ if err != nil {
308
322
panic (err )
309
323
}
310
324
}
@@ -317,6 +331,79 @@ func run() (err error) {
317
331
return
318
332
}
319
333
334
+ func digParse (r io.Reader ) error {
335
+ var fn func (path jp.Expr , data any )
336
+ annotateColor := ""
337
+
338
+ if color {
339
+ annotateColor = ojg .Gray
340
+ }
341
+ // Pick a function that satisfies omit, annotate, and senOut
342
+ // values. Determining the function before the actual calling means few
343
+ // conditional paths during the repeated calls later.
344
+ if omit {
345
+ if annotate {
346
+ if senOut {
347
+ fn = func (path jp.Expr , data any ) {
348
+ if data != nil && data != "" {
349
+ fmt .Printf ("%s// %s\n " , annotateColor , path )
350
+ writeSEN (data )
351
+ }
352
+ }
353
+ } else {
354
+ fn = func (path jp.Expr , data any ) {
355
+ if data != nil && data != "" {
356
+ fmt .Printf ("%s// %s\n " , annotateColor , path )
357
+ writeJSON (data )
358
+ }
359
+ }
360
+ }
361
+ } else {
362
+ if senOut {
363
+ fn = func (path jp.Expr , data any ) {
364
+ if data != nil && data != "" {
365
+ writeSEN (data )
366
+ }
367
+ }
368
+ } else {
369
+ fn = func (path jp.Expr , data any ) {
370
+ if data != nil && data != "" {
371
+ writeJSON (data )
372
+ }
373
+ }
374
+ }
375
+ }
376
+ } else {
377
+ if annotate {
378
+ if senOut {
379
+ fn = func (path jp.Expr , data any ) {
380
+ fmt .Printf ("%s// %s\n " , annotateColor , path )
381
+ writeSEN (data )
382
+ }
383
+ } else {
384
+ fn = func (path jp.Expr , data any ) {
385
+ fmt .Printf ("%s// %s\n " , annotateColor , path )
386
+ writeJSON (data )
387
+ }
388
+ }
389
+ } else {
390
+ if senOut {
391
+ fn = func (path jp.Expr , data any ) {
392
+ writeSEN (data )
393
+ }
394
+ } else {
395
+ fn = func (path jp.Expr , data any ) {
396
+ writeJSON (data )
397
+ }
398
+ }
399
+ }
400
+ }
401
+ if lazy {
402
+ return sen .MatchLoad (r , fn , extracts ... )
403
+ }
404
+ return oj .MatchLoad (r , fn , extracts ... )
405
+ }
406
+
320
407
func write (v any ) bool {
321
408
if conv != nil {
322
409
v = conv .Convert (v )
0 commit comments