compare

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: GPL-3.0 Imports: 0 Imported by: 0

Documentation

Overview

Package compare provides generic comparison utilities for structural equality testing.

This package offers a set of helper functions that eliminate boilerplate code when implementing Equal() methods on structs. It handles common patterns like nil checking, pointer comparisons, slice comparisons, and map comparisons.

Key Features

  • Generic functions that work with any type
  • Nil-safe pointer comparisons
  • Slice comparisons with custom equality functions
  • Map comparisons with value equality
  • Reduces boilerplate code by 60-80%

Usage Examples

Replace repetitive nil checks:

// Before (6 lines):
if x == nil && other == nil {
    return true
}
if x == nil || other == nil {
    return false
}

// After (2 lines):
if eq, done := compare.NilCheck(x, other); !done {
    return eq
}

Compare pointer fields:

// Before (12 lines for 2 fields):
if (t.Field1 != nil) != (other.Field1 != nil) {
    return false
}
if t.Field1 != nil && *t.Field1 != *other.Field1 {
    return false
}
// ... repeat for Field2

// After (2 lines):
return compare.Pointers(t.Field1, other.Field1) &&
       compare.Pointers(t.Field2, other.Field2)

Compare slices with element equality:

// Before (8 lines):
if len(a.Items) != len(other.Items) {
    return false
}
for i := range a.Items {
    if !a.Items[i].Equal(&other.Items[i]) {
        return false
    }
}
return true

// After (3 lines):
return compare.Slices(a.Items, other.Items, func(x, y Item) bool {
    return x.Equal(&y)
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Maps

func Maps[K comparable, V comparable](a, b map[K]V) bool

Maps compares two maps for equality. Returns true if both maps have the same keys and all corresponding values are equal.

Example:

func (t *TableInfo) Equal(other *TableInfo) bool {
    return compare.Maps(t.Settings, other.Settings)
}

func MapsWithEqual

func MapsWithEqual[K comparable, V any](a, b map[K]V, equalFunc func(V, V) bool) bool

MapsWithEqual compares two maps using a custom equality function for values. Returns true if both maps have the same keys and all corresponding values are equal according to the equality function.

Example:

func (d *Dictionary) Equal(other *Dictionary) bool {
    return compare.MapsWithEqual(d.Columns, other.Columns,
        func(a, b *Column) bool { return a.Equal(b) })
}

func NilCheck

func NilCheck[T any](a, b *T) (equal bool, needsMoreChecks bool)

NilCheck performs a nil check on two pointers and returns whether they are equal and whether more comparison checks are needed.

Returns (equal, needsMoreChecks) where:

  • equal: true if both are nil, false if only one is nil
  • needsMoreChecks: true if both pointers are non-nil and further comparison is needed

Example:

func (e *Expression) Equal(other *Expression) bool {
    if eq, needsMoreChecks := compare.NilCheck(e, other); !needsMoreChecks {
        return eq
    }
    // Continue with field comparisons...
}

func Pointers

func Pointers[T comparable](a, b *T) bool

Pointers compares two pointer values for equality. Returns true if both are nil, or both are non-nil with equal values.

Example:

func (t *TypeParameter) Equal(other *TypeParameter) bool {
    return compare.Pointers(t.Number, other.Number) &&
           compare.Pointers(t.String, other.String)
}

func PointersWithEqual

func PointersWithEqual[T any](a, b *T, equalFunc func(*T, *T) bool) bool

PointersWithEqual compares two pointers using a custom equality function. Returns true if both are nil, or both are non-nil and the equality function returns true.

Example:

func (t *TableEngine) Equal(other *TableEngine) bool {
    return compare.PointersWithEqual(t.Engine, other.Engine,
        func(a, b *EngineSpec) bool { return a.Equal(b) })
}

func Slices

func Slices[T any](a, b []T, equalFunc func(T, T) bool) bool

Slices compares two slices for equality using an equality function for elements. Returns true if both slices have the same length and all corresponding elements are equal.

Example:

func (t *Tuple) Equal(other *Tuple) bool {
    return compare.Slices(t.Elements, other.Elements,
        func(a, b TupleElement) bool { return a.Equal(&b) })
}

func SlicesUnordered

func SlicesUnordered[T any](a, b []T, equalFunc func(T, T) bool) bool

SlicesUnordered compares two slices for equality regardless of order. Returns true if both slices contain the same elements (by the equality function).

Example:

func (s *Settings) Equal(other *Settings) bool {
    return compare.SlicesUnordered(s.Items, other.Items,
        func(a, b Setting) bool { return a.Name == b.Name && a.Value == b.Value })
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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