Decode native diff paths without revalidation
This commit is contained in:
@@ -8,39 +8,17 @@ import (
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func decodeJSONPointer(pointer string) ([]string, error) {
|
||||
func decodeJSONPointer(path configkit.Path) []string {
|
||||
pointer := path.String()
|
||||
if pointer == "" {
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
if pointer[0] != '/' {
|
||||
return nil, fmt.Errorf("decode JSON pointer %q: path must start with /", pointer)
|
||||
}
|
||||
|
||||
parts := strings.Split(pointer[1:], "/")
|
||||
decoder := strings.NewReplacer("~1", "/", "~0", "~")
|
||||
for index, part := range parts {
|
||||
var decoded strings.Builder
|
||||
decoded.Grow(len(part))
|
||||
for offset := 0; offset < len(part); offset++ {
|
||||
if part[offset] != '~' {
|
||||
decoded.WriteByte(part[offset])
|
||||
continue
|
||||
}
|
||||
if offset+1 >= len(part) {
|
||||
return nil, fmt.Errorf("decode JSON pointer %q: incomplete escape in token %d", pointer, index)
|
||||
}
|
||||
offset++
|
||||
switch part[offset] {
|
||||
case '0':
|
||||
decoded.WriteByte('~')
|
||||
case '1':
|
||||
decoded.WriteByte('/')
|
||||
default:
|
||||
return nil, fmt.Errorf("decode JSON pointer %q: invalid escape in token %d", pointer, index)
|
||||
}
|
||||
}
|
||||
parts[index] = decoded.String()
|
||||
parts[index] = decoder.Replace(part)
|
||||
}
|
||||
return parts, nil
|
||||
return parts
|
||||
}
|
||||
|
||||
func placeReportValue(root any, path []string, value any) (any, error) {
|
||||
@@ -135,10 +113,7 @@ func renderDiffReport(differences []configkit.Difference) (any, error) {
|
||||
if !difference.Right.Present {
|
||||
continue
|
||||
}
|
||||
tokens, err := decodeJSONPointer(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("difference at %q: %w", path, err)
|
||||
}
|
||||
tokens := decodeJSONPointer(difference.Path)
|
||||
value, err := commandkit.DecodeYAMLReport(strings.NewReader(difference.Right.YAML))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("difference at %q: %w", path, err)
|
||||
|
||||
Reference in New Issue
Block a user