aboutsummaryrefslogtreecommitdiff
path: root/internal/path/path.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-06-20 20:27:48 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-06-20 20:27:48 +0200
commit6e285f65c5a5ed8e27857a3688a7c86b37688b13 (patch)
tree3050315491ff0c53ec6eab0affaf5fbea5a8ef97 /internal/path/path.go
parentc0ca4d891f08a5baef22bee16a1a4feb0089bbae (diff)
downloadgo-gg-6e285f65c5a5ed8e27857a3688a7c86b37688b13.tar.gz
assert: improve output of Equal() for slice types with only partial diffs
Diffstat (limited to 'internal/path/path.go')
-rw-r--r--internal/path/path.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/internal/path/path.go b/internal/path/path.go
index 333b3b2..fa3363e 100644
--- a/internal/path/path.go
+++ b/internal/path/path.go
@@ -42,17 +42,27 @@ type Element struct {
Index int
// only used by package assert (panics when used with AsJSONPointer())
+ Slice Option[SliceBounds]
MapKey Option[any] // holds a value of type K for traversing into types ~map[K]V
TypeCast string // holds a %T formatting of a type
Dereference bool // marks when a pointer is dereferenced
}
+// SliceBounds appears in type [Element].
+type SliceBounds struct {
+ Start int
+ End int
+}
+
// KeyElement is a shorthand for constructing an Element with the Key field set.
func KeyElement(key string) Element { return Element{Key: Some(key)} }
// IndexElement is a shorthand for constructing an Element with the Index field set.
func IndexElement(idx int) Element { return Element{Index: idx} }
+// SliceElement is a shorthand for constructing an Element with the Slice field set.
+func SliceElement(start, end int) Element { return Element{Slice: Some(SliceBounds{start, end})} }
+
// MapKeyElement is a shorthand for constructing an Element with the MapKey field set.
func MapKeyElement(key any) Element { return Element{MapKey: Some(key)} }
@@ -79,6 +89,9 @@ func (p Path) AsJSONPointer() string {
if elem.MapKey.IsSome() {
panic("MapKey elements cannot be used with AsJSONPointer()")
}
+ if elem.Slice.IsSome() {
+ panic("Slice elements cannot be used with AsJSONPointer()")
+ }
if key, ok := elem.Key.Unpack(); ok {
fragments[idx+1] = keyIntoPointerFragment(key)
} else {
@@ -115,6 +128,8 @@ func (p Path) AsGoExpression(baseVariable string) string {
fmt.Fprintf(b, `.(%s)`, elem.TypeCast)
} else if mapKey, ok := elem.MapKey.Unpack(); ok {
fmt.Fprintf(b, `[%#v]`, mapKey)
+ } else if bounds, ok := elem.Slice.Unpack(); ok {
+ fmt.Fprintf(b, `[%d:%d]`, bounds.Start, bounds.End)
} else if key, ok := elem.Key.Unpack(); ok {
fmt.Fprintf(b, `.%s`, key)
} else {