Documentation
¶
Overview ¶
Package compact implements a compact array-based codec for JSON Patch operations. This codec provides significant space savings compared to standard JSON format while maintaining full compatibility with all operation types.
Package compact implements a compact array-based codec for JSON Patch operations. This codec uses arrays instead of objects to represent operations, significantly reducing the physical space required for encoding while maintaining readability.
Index ¶
- Variables
- func Decode(compactOps []Op, opts ...DecoderOption) ([]internal.Op, error)
- func DecodeJSON(data []byte, opts ...DecoderOption) ([]internal.Op, error)
- func EncodeJSON(ops []internal.Op, opts ...EncoderOption) ([]byte, error)
- type Decoder
- type DecoderOption
- type DecoderOptions
- type Encoder
- type EncoderOption
- type EncoderOptions
- type Op
- type OpCode
- type Operation
Constants ¶
This section is empty.
Variables ¶
var ( // Base operation errors ErrCompactOperationMinLength = errors.New("compact operation must have at least opcode and path") ErrCompactOperationPathNotString = errors.New("compact operation path must be a string") // Operation-specific errors ErrAddOperationRequiresValue = errors.New("add operation requires value") ErrReplaceOperationRequiresValue = errors.New("replace operation requires value") ErrMoveOperationRequiresFrom = errors.New("move operation requires from path") ErrMoveOperationFromNotString = errors.New("move operation from must be a string") ErrCopyOperationRequiresFrom = errors.New("copy operation requires from path") ErrCopyOperationFromNotString = errors.New("copy operation from must be a string") ErrTestOperationRequiresValue = errors.New("test operation requires value") ErrIncOperationRequiresDelta = errors.New("inc operation requires delta") ErrIncOperationDeltaNotNumber = errors.New("inc operation delta must be a number") ErrContainsOperationRequiresValue = errors.New("contains operation requires value") ErrContainsOperationValueNotString = errors.New("contains operation value must be a string") ErrStartsOperationRequiresValue = errors.New("starts operation requires value") ErrStartsOperationValueNotString = errors.New("starts operation value must be a string") ErrEndsOperationRequiresValue = errors.New("ends operation requires value") ErrEndsOperationValueNotString = errors.New("ends operation value must be a string") // Type operation errors ErrTypeOperationRequiresType = errors.New("type operation requires type") ErrTypeOperationTypeNotString = errors.New("type operation type must be a string") // Test type operation errors ErrTestTypeOperationRequiresTypes = errors.New("test_type operation requires types") ErrTestTypeOperationTypesNotArray = errors.New("test_type operation types must be an array") // Test string operation errors ErrTestStringOperationRequiresStr = errors.New("test_string operation requires str") ErrTestStringOperationStrNotString = errors.New("test_string operation str must be a string") // Test string len operation errors ErrTestStringLenOperationRequiresLen = errors.New("test_string_len operation requires len") ErrTestStringLenOperationLenNotNumber = errors.New("test_string_len operation len must be a number") // In operation errors ErrInOperationRequiresValues = errors.New("in operation requires values") ErrInOperationValuesNotArray = errors.New("in operation values must be an array") // Less operation errors ErrLessOperationRequiresValue = errors.New("less operation requires value") ErrLessOperationValueNotNumber = errors.New("less operation value must be a number") // More operation errors ErrMoreOperationRequiresValue = errors.New("more operation requires value") ErrMoreOperationValueNotNumber = errors.New("more operation value must be a number") // Matches operation errors ErrMatchesOperationRequiresPattern = errors.New("matches operation requires pattern") ErrMatchesOperationPatternNotString = errors.New("matches operation pattern must be a string") // Composite operation errors ErrAndOperationRequiresOps = errors.New("and operation requires ops") ErrOrOperationRequiresOps = errors.New("or operation requires ops") ErrNotOperationRequiresOps = errors.New("not operation requires ops") ErrPredicateOpsNotArray = errors.New("predicate ops must be an array") ErrPredicateOpNotArray = errors.New("predicate op must be an array") ErrDecodedOpNotPredicate = errors.New("decoded operation is not a predicate") // String operation errors ErrStrInsOperationRequiresPosAndStr = errors.New("str_ins operation requires pos and str") ErrStrInsOperationPosNotNumber = errors.New("str_ins operation pos must be a number") ErrStrInsOperationStrNotString = errors.New("str_ins operation str must be a string") ErrStrDelOperationRequiresPosAndLen = errors.New("str_del operation requires pos and len") ErrStrDelOperationPosNotNumber = errors.New("str_del operation pos must be a number") ErrStrDelOperationLenNotNumber = errors.New("str_del operation len must be a number") // Split/Merge/Extend operation errors ErrSplitOperationRequiresPos = errors.New("split operation requires pos") ErrSplitOperationPosNotNumber = errors.New("split operation pos must be a number") ErrMergeOperationRequiresPos = errors.New("merge operation requires pos") ErrMergeOperationPosNotNumber = errors.New("merge operation pos must be a number") ErrExtendOperationRequiresProps = errors.New("extend operation requires props") ErrExtendOperationPropsNotObject = errors.New("extend operation props must be an object") )
Predefined errors for compact codec operations
var ( ErrUnsupportedOperationType = errors.New("unsupported operation type") ErrUnknownStringOpcode = errors.New("unknown string opcode") ErrInvalidOpcodeType = errors.New("invalid opcode type") ErrUnknownNumericOpcode = errors.New("unknown numeric opcode") )
Base errors for dynamic data
var ( ErrCannotConvertToFloat64 = errors.New("cannot convert to float64") ErrExpectedArray = errors.New("expected array") ErrExpectedStringInArray = errors.New("expected string in array") )
Type conversion errors
var ( // NewEncoder creates a new compact encoder with the given options NewCompactEncoder = NewEncoder // NewDecoder creates a new compact decoder with the given options NewCompactDecoder = NewDecoder // EncodeCompact encodes operations into compact format using default options EncodeCompact = Encode // DecodeCompact decodes compact format operations using default options DecodeCompact = Decode // EncodeCompactJSON encodes operations into compact format JSON bytes EncodeCompactJSON = EncodeJSON // DecodeCompactJSON decodes compact format JSON bytes into operations DecodeCompactJSON = DecodeJSON )
Re-export main functions for convenience
var ( DefaultEncoderOptions = EncoderOptions{ StringOpcode: false, } DefaultDecoderOptions = DecoderOptions{} )
Default options
Functions ¶
func Decode ¶
func Decode(compactOps []Op, opts ...DecoderOption) ([]internal.Op, error)
Decode decodes compact format operations using default options
func DecodeJSON ¶
func DecodeJSON(data []byte, opts ...DecoderOption) ([]internal.Op, error)
DecodeJSON decodes compact format JSON bytes into operations
func EncodeJSON ¶
func EncodeJSON(ops []internal.Op, opts ...EncoderOption) ([]byte, error)
EncodeJSON encodes operations into compact format JSON bytes
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes compact format operations into operation instances
func NewDecoder ¶
func NewDecoder(opts ...DecoderOption) *Decoder
NewDecoder creates a new compact decoder with the given options
type DecoderOption ¶
type DecoderOption func(*DecoderOptions)
DecoderOption is a functional option for configuring the decoder
type DecoderOptions ¶
type DecoderOptions struct {
}
DecoderOptions configures the compact decoder behavior
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes operations into compact format
func NewEncoder ¶
func NewEncoder(opts ...EncoderOption) *Encoder
NewEncoder creates a new compact encoder with the given options
type EncoderOption ¶
type EncoderOption func(*EncoderOptions)
EncoderOption is a functional option for configuring the encoder
func WithStringOpcode ¶
func WithStringOpcode(useString bool) EncoderOption
WithStringOpcode configures the encoder to use string opcodes
type EncoderOptions ¶
type EncoderOptions struct {
// StringOpcode determines whether to use string opcodes instead of numeric ones
StringOpcode bool
}
EncoderOptions configures the compact encoder behavior
type Op ¶ added in v0.4.3
type Op []interface{}
Op represents a compact format operation as an array
type OpCode ¶
type OpCode int
OpCode represents operation codes for compact format
const ( // JSON Patch (RFC 6902) operations - match internal/constants.go OpCodeAdd OpCode = 0 OpCodeRemove OpCode = 1 OpCodeReplace OpCode = 2 OpCodeCopy OpCode = 3 OpCodeMove OpCode = 4 OpCodeTest OpCode = 5 // String editing OpCodeStrIns OpCode = 6 OpCodeStrDel OpCode = 7 // Extra OpCodeFlip OpCode = 8 OpCodeInc OpCode = 9 // Slate.js OpCodeSplit OpCode = 10 OpCodeMerge OpCode = 11 OpCodeExtend OpCode = 12 // JSON Predicate operations OpCodeContains OpCode = 30 OpCodeDefined OpCode = 31 OpCodeEnds OpCode = 32 OpCodeIn OpCode = 33 OpCodeLess OpCode = 34 OpCodeMatches OpCode = 35 OpCodeMore OpCode = 36 OpCodeStarts OpCode = 37 OpCodeUndefined OpCode = 38 OpCodeTestType OpCode = 39 OpCodeTestString OpCode = 40 OpCodeTestStringLen OpCode = 41 OpCodeType OpCode = 42 OpCodeAnd OpCode = 43 OpCodeNot OpCode = 44 OpCodeOr OpCode = 45 )
Operation codes for compact format
type Operation ¶
type Operation = internal.CompactOperation
Operation represents a compact format operation.