blob: be3d302551fbbab6f0c4da4e5c05a5f67ba9a13d [file] [log] [blame]
Lorenz Brunf042e6f2020-06-24 16:46:09 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
Serge Bazanski216fe7b2021-05-21 18:36:16 +020017// Package jsonpatch contains data structures and encoders for JSON Patch (RFC
18// 6902) and JSON Pointers (RFC 6901)
Lorenz Brunf042e6f2020-06-24 16:46:09 +020019package jsonpatch
20
21import "strings"
22
23// JSON Patch operation (RFC 6902 Section 4)
24type JsonPatchOp struct {
25 Operation string `json:"op"`
26 Path string `json:"path"` // Technically a JSON Pointer, but called Path in the RFC
27 From string `json:"from,omitempty"`
28 Value interface{} `json:"value,omitempty"`
29}
30
Serge Bazanski216fe7b2021-05-21 18:36:16 +020031// EncodeJSONRefToken encodes a JSON reference token as part of a JSON Pointer
32// (RFC 6901 Section 2)
Lorenz Brunf042e6f2020-06-24 16:46:09 +020033func EncodeJSONRefToken(token string) string {
34 x := strings.ReplaceAll(token, "~", "~0")
35 return strings.ReplaceAll(x, "/", "~1")
36}
37
38// PointerFromParts returns an encoded JSON Pointer from parts
39func PointerFromParts(pathParts []string) string {
40 var encodedParts []string
41 encodedParts = append(encodedParts, "")
42 for _, part := range pathParts {
43 encodedParts = append(encodedParts, EncodeJSONRefToken(part))
44 }
45 return strings.Join(encodedParts, "/")
46}