mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
afb30d3f9d
Brace yourselves! For there are many touched files here. No changes in semantics however. Spent a long time trying out the various optional rules gocritic provides, and settled for a few of them. There are more I really like, but that would take many hours to address across the codebase. Perhaps others find gocritic too pedantic? If so, we can merge the fixes without enabling the rule. Signed-off-by: Anders Eknert <anders@styra.com>
46 lines
1.8 KiB
Go
46 lines
1.8 KiB
Go
// Copyright 2021 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package patch
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/open-policy-agent/opa/v1/storage"
|
|
)
|
|
|
|
// ParsePatchPathEscaped returns a new path for the given escaped str.
|
|
// This is based on storage.ParsePathEscaped so will do URL unescaping of
|
|
// the provided str for backwards compatibility, but also handles the
|
|
// specific escape strings defined in RFC 6901 (JSON Pointer) because
|
|
// that's what's mandated by RFC 6902 (JSON Patch).
|
|
func ParsePatchPathEscaped(str string) (path storage.Path, ok bool) {
|
|
path, ok = storage.ParsePathEscaped(str)
|
|
if !ok {
|
|
return
|
|
}
|
|
for i := range path {
|
|
// RFC 6902 section 4: "[The "path" member's] value is a string containing
|
|
// a JSON-Pointer value [RFC6901] that references a location within the
|
|
// target document (the "target location") where the operation is performed."
|
|
//
|
|
// RFC 6901 section 3: "Because the characters '~' (%x7E) and '/' (%x2F)
|
|
// have special meanings in JSON Pointer, '~' needs to be encoded as '~0'
|
|
// and '/' needs to be encoded as '~1' when these characters appear in a
|
|
// reference token."
|
|
|
|
// RFC 6901 section 4: "Evaluation of each reference token begins by
|
|
// decoding any escaped character sequence. This is performed by first
|
|
// transforming any occurrence of the sequence '~1' to '/', and then
|
|
// transforming any occurrence of the sequence '~0' to '~'. By performing
|
|
// the substitutions in this order, an implementation avoids the error of
|
|
// turning '~01' first into '~1' and then into '/', which would be
|
|
// incorrect (the string '~01' correctly becomes '~1' after transformation)."
|
|
path[i] = strings.ReplaceAll(path[i], "~1", "/")
|
|
path[i] = strings.ReplaceAll(path[i], "~0", "~")
|
|
}
|
|
|
|
return
|
|
}
|