github.com/docker/compose-on-kubernetes@v0.5.0/internal/patch/patch.go (about)

     1  package patch
     2  
     3  import "encoding/json"
     4  
     5  // Patch describes a JSONPatch operations.
     6  type Patch []operation
     7  
     8  type operation struct {
     9  	Op    string      `json:"op"`
    10  	Path  string      `json:"path"`
    11  	Value interface{} `json:"value,omitempty"`
    12  }
    13  
    14  // New creates a new patch.
    15  func New() Patch {
    16  	return Patch{}
    17  }
    18  
    19  // Replace adds a "replace" operation to the patch.
    20  func (p Patch) Replace(path string, value interface{}) Patch {
    21  	return append(p, operation{
    22  		Op:    "replace",
    23  		Path:  path,
    24  		Value: value,
    25  	})
    26  }
    27  
    28  // Remove adds a "remove" operation to the patch.
    29  func (p Patch) Remove(path string) Patch {
    30  	return append(p, operation{
    31  		Op:   "remove",
    32  		Path: path,
    33  	})
    34  }
    35  
    36  // Add adds a "Add" operation to the patch.
    37  func (p Patch) Add(path string, value interface{}) Patch {
    38  	return append(p, operation{
    39  		Op:    "add",
    40  		Path:  path,
    41  		Value: value,
    42  	})
    43  }
    44  
    45  // AddKV adds a "add" operation to the patch.
    46  func (p Patch) AddKV(path, key string, value interface{}) Patch {
    47  	return append(p, operation{
    48  		Op:   "add",
    49  		Path: path,
    50  		Value: map[string]interface{}{
    51  			key: value,
    52  		},
    53  	})
    54  }
    55  
    56  // ToJSON converts the patch to json.
    57  func (p Patch) ToJSON() ([]byte, error) {
    58  	return json.Marshal(p)
    59  }