github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/jsonschemax/pointer.go (about) 1 package jsonschemax 2 3 import ( 4 "net/url" 5 "strings" 6 7 "github.com/pkg/errors" 8 ) 9 10 // JSONPointerToDotNotation converts JSON Pointer "#/foo/bar" to dot-notation "foo.bar". 11 func JSONPointerToDotNotation(pointer string) (string, error) { 12 if !strings.HasPrefix(pointer, "#/") { 13 return pointer, errors.Errorf("remote JSON pointers are not supported: %s", pointer) 14 } 15 16 var path []string 17 for _, item := range strings.Split(strings.TrimPrefix(pointer, "#/"), "/") { 18 item = strings.Replace(item, "~1", "/", -1) 19 item = strings.Replace(item, "~0", "~", -1) 20 item, err := url.PathUnescape(item) 21 if err != nil { 22 return "", err 23 } 24 path = append(path, strings.ReplaceAll(item, ".", "\\.")) 25 } 26 27 return strings.Join(path, "."), nil 28 }