blob: 5f9c20d3e025d17c527862407519a09a8d86a4c2 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brunf042e6f2020-06-24 16:46:09 +02002// SPDX-License-Identifier: Apache-2.0
Lorenz Brunf042e6f2020-06-24 16:46:09 +02003
Serge Bazanski216fe7b2021-05-21 18:36:16 +02004// Package jsonpatch contains data structures and encoders for JSON Patch (RFC
5// 6902) and JSON Pointers (RFC 6901)
Lorenz Brunf042e6f2020-06-24 16:46:09 +02006package jsonpatch
7
8import "strings"
9
Tim Windelschmidt8732d432024-04-18 23:20:05 +020010// JsonPatchOp describes a JSON Patch operation (RFC 6902 Section 4)
Lorenz Brunf042e6f2020-06-24 16:46:09 +020011type JsonPatchOp struct {
12 Operation string `json:"op"`
13 Path string `json:"path"` // Technically a JSON Pointer, but called Path in the RFC
14 From string `json:"from,omitempty"`
15 Value interface{} `json:"value,omitempty"`
16}
17
Serge Bazanski216fe7b2021-05-21 18:36:16 +020018// EncodeJSONRefToken encodes a JSON reference token as part of a JSON Pointer
19// (RFC 6901 Section 2)
Lorenz Brunf042e6f2020-06-24 16:46:09 +020020func EncodeJSONRefToken(token string) string {
21 x := strings.ReplaceAll(token, "~", "~0")
22 return strings.ReplaceAll(x, "/", "~1")
23}
24
25// PointerFromParts returns an encoded JSON Pointer from parts
26func PointerFromParts(pathParts []string) string {
27 var encodedParts []string
28 encodedParts = append(encodedParts, "")
29 for _, part := range pathParts {
30 encodedParts = append(encodedParts, EncodeJSONRefToken(part))
31 }
32 return strings.Join(encodedParts, "/")
33}