jsonpatch

package
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2022 License: MIT Imports: 7 Imported by: 0

README

Query for JSON patch

With patch operations patches can be specified in detail base on RFC6902 (test operation is not implemented - use RSQL Parser instead) There are five operations available:

Operation Description
remove remove the value at the target location.
add add a value or array to an array at the target location.
replace replaces the value at the target location with a new value.
move removes the value at a specified location and adds it to the target location.
copy copies the value from a specified location to the target location.

This features are supported for MongoDB 4.2+.

Additionally, simple rules can be set:

Policy Description
DisallowPathPolicy specifies a path that is not allowed.
DisallowOperationOnPathPolicy disallows specified operation on path.
ForceTypeOnPathPolicy forces the value of a specif path to be from given type.
ForceRegexMatchPolicy forces the value of a specif path to match expression.
StrictPathPolicy forces path to be strictly one of.
The path fields can be set to * for any field name. E.g. *.version will match product.version but not version.

How to

Basic usage

import (
  "github.com/StevenCyb/goapiutils/parser/mongo/jsonpatch"

  "go.mongodb.org/mongo-driver/mongo/options"
)
// ...

  var operations []jsonpatch.OperationSpec
  err = json.NewDecoder(req.Body).Decode(&operations)
  // ...

  parser := jsonpatch.NewParser()
  query, err := parser.Parse(operations...)
  // ...

  result := collection.FindOneAndUpdate(ctx, filter, query, updateOptions)
  // ...

Policy usage

  parser := jsonpatch.NewParser(
    DisallowPathPolicy{Details: "illegal ID modification", Path: "_id"},
    ForceTypeOnPathPolicy{Details: "age as number", Path: "user.age", Kind: reflect.Int64},
  ),

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DisallowOperationOnPathPolicy

type DisallowOperationOnPathPolicy struct {
	Details    string
	Path       Path
	Operations []Operation
}

DisallowOperationOnPathPolicy disallows specified operations on path.

func (DisallowOperationOnPathPolicy) GetDetails

func (d DisallowOperationOnPathPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (DisallowOperationOnPathPolicy) Test

func (d DisallowOperationOnPathPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

type DisallowPathPolicy

type DisallowPathPolicy struct {
	Details string
	Path    Path
}

DisallowPathPolicy specifies a path that is not allowed.

func (DisallowPathPolicy) GetDetails

func (d DisallowPathPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (DisallowPathPolicy) Test

func (d DisallowPathPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

type ForceOperationOnPathPolicy added in v1.2.1

type ForceOperationOnPathPolicy struct {
	Details   string
	Path      Path
	Operation Operation
}

ForceOperationOnPathPolicy force specified operation on path.

func (ForceOperationOnPathPolicy) GetDetails added in v1.2.1

func (d ForceOperationOnPathPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (ForceOperationOnPathPolicy) Test added in v1.2.1

func (d ForceOperationOnPathPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

type ForceRegexMatchPolicy

type ForceRegexMatchPolicy struct {
	Details    string
	Path       Path
	Expression regexp.Regexp
}

ForceRegexMatchPolicy forces the value of a specif path to match expression.

func (ForceRegexMatchPolicy) GetDetails

func (f ForceRegexMatchPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (ForceRegexMatchPolicy) Test

func (f ForceRegexMatchPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

type ForceTypeOnPathPolicy

type ForceTypeOnPathPolicy struct {
	Details string
	Path    Path
	Kind    reflect.Kind
}

ForceTypeOnPathPolicy forces the value of a specif path to be from given type.

func (ForceTypeOnPathPolicy) GetDetails

func (f ForceTypeOnPathPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (ForceTypeOnPathPolicy) Test

func (f ForceTypeOnPathPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

type Operation

type Operation string
const (
	// RemoveOperation is an operation to remove the value at the target location.
	// Requires `path`.
	RemoveOperation Operation = "remove"
	// AddOperation add a value or array to an array at the target location.
	// Requires `path` and `value`. Target of the path must be an array.
	AddOperation Operation = "add"
	// ReplaceOperation replaces the value at the target location
	// with a new value. Requires `path` and `value`.
	ReplaceOperation Operation = "replace"
	// MoveOperation removes the value at a specified location and
	// adds it to the target location. Requires `from` and `path`.
	MoveOperation Operation = "move"
	// CopyOperation copies the value from a specified location to the
	// target location. Requires `from` and `path`.
	CopyOperation Operation = "copy"
)

type OperationSpec

type OperationSpec struct {
	From      Path        `json:"from"`
	Path      Path        `json:"path"`
	Value     interface{} `json:"value"`
	Operation Operation   `json:"op"` //nolint:tagliatelle
}

OperationSpec specify an path operation.

func (OperationSpec) Valid

func (o OperationSpec) Valid() bool

Valid check if operation is valid.

type Parser

type Parser struct {
	Policies []Policy
}

Parser that can parse patch operation to generate mongo queries.

func NewParser

func NewParser(policies ...Policy) *Parser

NewParser creates a new parser.

func (Parser) Parse

func (p Parser) Parse(operationSpecs ...OperationSpec) (bson.A, error)

Parse given operation spec to generate mongo queries if not violating policies.

type Path

type Path string

func (Path) Equal added in v1.2.2

func (p Path) Equal(p2 Path) bool

Equal check if path is equal to given path. Single fields can be set to `*` for wildcard.

func (Path) Valid

func (p Path) Valid() bool

Valid check if given path is in valid format.

type Policy

type Policy interface {
	GetDetails() string
	Test(operationSpec OperationSpec) bool
}

Policy specifies the interface for an policy.

type StrictPathPolicy

type StrictPathPolicy struct {
	Details string
	Path    []Path
}

StrictPathPolicy forces path to be strictly one of.

func (StrictPathPolicy) GetDetails

func (s StrictPathPolicy) GetDetails() string

GetDetails returns the name of this policy.

func (StrictPathPolicy) Test

func (s StrictPathPolicy) Test(operationSpec OperationSpec) bool

Test if given operation specification is valid or not.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL