gocmputils

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

go-cmp-utils

Some utils for https://github.com/google/go-cmp/ library.

Filter keys when compare two map[string]any

For filter keys (with deep map keys) by key path you can create cmp.Option with function MapKeysFilter.

For compare every key part you can use two comparators:

  • MapPathStringComparator - match every key part as full equal two string
  • MapPathReComparator - match every key part as regexp match

Examples

See in examples dir.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SkipNotEmptyRe
	// regexp for filter any not empty key
	SkipNotEmptyRe = regexp.MustCompile(`.+`)
	// SkipWithEmptyRe
	// regexp for filter any key (include empty)
	SkipWithEmptyRe = regexp.MustCompile(`.*`)
)

Functions

func MapKeysFilter

func MapKeysFilter(paths ...MapPath) cmp.Option

MapKeysFilter returns go-cmp option for ignore golang map keys by path Every MapPath are paths to keys With it you can filter multiple keys by own path For create MapPath you can use NewMapPathStringComparator MapPathReComparator

func RepeatCompileRe

func RepeatCompileRe(r string, count int) ([]*regexp.Regexp, error)

RepeatCompileRe compile regexp and create list of regexp with count len

func RepeatRe

func RepeatRe(r *regexp.Regexp, count int) []*regexp.Regexp

RepeatRe create list of regexp with count len

Types

type MapPath

type MapPath MapPathComparator

MapPath alias to MapPathComparator

type MapPathComparator

type MapPathComparator interface {
	// Compare
	// compare key part with index
	Compare(indx int, keyPathPart string) bool
	// Parts
	// returns number of parts
	Parts() int
}

MapPathComparator interface for compare key parts

type MapPathReComparator

type MapPathReComparator []*regexp.Regexp

MapPathReComparator implementation of MapPathComparator This comparator compare every path part as regexp match

func NewMapPathReComparator

func NewMapPathReComparator(parts ...string) (MapPathReComparator, error)

NewMapPathReComparator creates MapPathReComparator parts is regexp compatibility strings all parts will compile to regexp for example, for filter all keys "second" and "first" key as single regexp in map:

map[string]any {
  "root": map[string]any{
    "first": 42,
    "second": "val",
    "another": []string{"hello"}
  }
}

you can use next call NewMapPathReComparator("root", "^(second|first)$") function returns all compile errors as single error

func NewMapPathReComparatorFromRe

func NewMapPathReComparatorFromRe(parts ...*regexp.Regexp) MapPathReComparator

NewMapPathReComparator creates MapPathReComparator parts is regexp instances

func (MapPathReComparator) Compare

func (c MapPathReComparator) Compare(indx int, keyPathPart string) bool

func (MapPathReComparator) Parts

func (c MapPathReComparator) Parts() int

type MapPathStringComparator

type MapPathStringComparator []string

MapPathStringComparator implementation of MapPathComparator This comparator compare every path part as full string equals

func NewMapPathStringComparator

func NewMapPathStringComparator(parts ...string) MapPathStringComparator

NewMapPathStringComparator creates MapPathStringComparator parts is full path to key for filter for example, for filter "second" key in map:

map[string]any {
  "root": map[string]any{
    "first": 42,
    "second": "val",
  }
}

you can use next call NewMapPathStringComparator("root", "second")

func (MapPathStringComparator) Compare

func (c MapPathStringComparator) Compare(indx int, keyPathPart string) bool

func (MapPathStringComparator) Parts

func (c MapPathStringComparator) Parts() int

Jump to

Keyboard shortcuts

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