Debian Codegraph @

strict/utils.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package strict
5
6 import (
7 "regexp"
8 "strconv"
9 "strings"
10 )
11
12 // buildPath creates an instance path by appending a property name to a base path.
13 // Property names containing dots or brackets use bracket notation for clarity.
14 //
15 // Examples:
16 // - buildPath("$.body", "name") → "$.body.name"
17 // - buildPath("$.body", "a.b") → "$.body['a.b']"
18 // - buildPath("$.body", "x[0]") → "$.body['x[0]']"
19 func buildPath(base, propName string) string {
20 if needsBracketNotation(propName) {
21 return base + "['" + propName + "']"
22 }
23 return base + "." + propName
24 }
25
26 // needsBracketNotation returns true if a property name contains characters
27 // that require bracket notation (dots, brackets).
28 func needsBracketNotation(name string) bool {
29 return strings.ContainsAny(name, ".[]")
30 }
31
32 // buildArrayPath creates an instance path for an array element.
33 func buildArrayPath(base string, index int) string {
34 return base + "[" + strconv.Itoa(index) + "]"
35 }
36
37 // compileIgnorePaths converts glob patterns to compiled regular expressions.
38 // Supports:
39 // - * matches single path segment (no dots or brackets)
40 // - ** matches any depth (zero or more segments)
41 // - [*] matches any array index
42 // - \* escapes literal asterisk
43 // - \*\* escapes literal double-asterisk
44 func compileIgnorePaths(patterns []string) []*regexp.Regexp {
45 if len(patterns) == 0 {
46 return nil
47 }
48
49 compiled := make([]*regexp.Regexp, 0, len(patterns))
50 for _, pattern := range patterns {
51 re := compilePattern(pattern)
52 if re != nil {
53 compiled = append(compiled, re)
54 }
55 }
56 return compiled
57 }
58
59 // compilePattern converts a single glob pattern to a regular expression.
60 func compilePattern(pattern string) *regexp.Regexp {
61 if pattern == "" {
62 return nil
63 }
64
65 var b strings.Builder
66 b.WriteString("^")
67
68 i := 0
69 for i < len(pattern) {
70 c := pattern[i]
71
72 // handle escape sequences
73 if c == '\\' && i+1 < len(pattern) {
74 next := pattern[i+1]
75 if next == '*' {
76 // check for escaped **
77 if i+2 < len(pattern) && pattern[i+2] == '\\' && i+3 < len(pattern) && pattern[i+3] == '*' {
78 b.WriteString(`\*\*`)
79 i += 4
80 continue
81 }
82 // escaped single *
83 b.WriteString(`\*`)
84 i += 2
85 continue
86 }
87 // other escape - include literally
88 b.WriteString(regexp.QuoteMeta(string(next)))
89 i += 2
90 continue
91 }
92
93 // handle ** (any depth)
94 if c == '*' && i+1 < len(pattern) && pattern[i+1] == '*' {
95 // ** matches any sequence of segments including none
96 b.WriteString(`.*`)
97 i += 2
98 continue
99 }
100
101 // handle single * (single segment)
102 if c == '*' {
103 // * matches single path segment (no dots or brackets)
104 b.WriteString(`[^.\[\]]+`)
105 i++
106 continue
107 }
108
109 // handle [*] (any array index)
110 if c == '[' && i+2 < len(pattern) && pattern[i+1] == '*' && pattern[i+2] == ']' {
111 b.WriteString(`\[\d+\]`)
112 i += 3
113 continue
114 }
115
116 // handle special regex characters
117 switch c {
118 case '.', '[', ']', '(', ')', '{', '}', '+', '?', '^', '$', '|':
119 b.WriteString(`\`)
120 b.WriteByte(c)
121 default:
122 b.WriteByte(c)
123 }
124 i++
125 }
126
127 b.WriteString("$")
128
129 re, _ := regexp.Compile(b.String())
130 return re
131 }
132
133 // TruncateValue creates a display-friendly version of a value.
134 // Long strings are truncated, complex objects show type info.
135 // This is exported for use in error messages.
136 func TruncateValue(v any) any {
137 switch val := v.(type) {
138 case string:
139 if len(val) > 50 {
140 return val[:47] + "..."
141 }
142 return val
143 case map[string]any:
144 if len(val) > 3 {
145 return "{...}"
146 }
147 return val
148 case []any:
149 if len(val) > 3 {
150 return "[...]"
151 }
152 return val
153 default:
154 return v
155 }
156 }
157
158 // truncateValue is an internal alias for TruncateValue.
159 func truncateValue(v any) any {
160 return TruncateValue(v)
161 }