-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (59 loc) · 1.38 KB
/
main.go
File metadata and controls
66 lines (59 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"ezpkg.io/errorz"
iterjson "ezpkg.io/iter.json"
)
func main() {
_, file, _, _ := runtime.Caller(0)
data := errorz.Must(os.ReadFile(filepath.Dir(file) + "/../alice.nguyen.json"))
{
// 🐳Example: filter and print, use GetPathString()
fmt.Printf("\n--- filter: GetPathString() ---\n")
i, regexPetName := 0, regexp.MustCompile("pets.*name")
for item, err := range iterjson.Parse(data) {
i++
errorz.MustZ(err)
path := item.GetPathString()
switch {
case path == "name",
path == "email",
path == "phone",
regexPetName.MatchString(path),
strings.Contains(path, "address"):
// continue
default:
continue
}
// 👉 print with line number
fmt.Printf("%2d %20s . %s\n", i, item.Token, item.GetPath())
}
}
{
// 🐳Example: filter and print, use GetRawPath() and Match()
fmt.Printf("\n--- filter: GetRawPath() ---\n")
i := 0
for item, err := range iterjson.Parse(data) {
i++
errorz.MustZ(err)
path := item.GetRawPath()
switch {
case path.Match("name"),
path.Match("email"),
path.Match("phone"),
path.Contains("pets") && path.Contains("name"),
path.Contains("address"):
// continue
default:
continue
}
// 👉 print with line number
fmt.Printf("%2d %20s . %s\n", i, item.Token, item.GetPath())
}
}
}