Skip to content

uuid is unreadable since it is compared as array #57

Open

Description

@primus11

Currently comparing two "github.com/google/uuid" is not really readable since it is checked as array and looks like this:

UUID.array[0]: 93 != 96 UUID.array[1]: 181 != 145 UUID.array[2]: 180 != 187 UUID.array[3]: 80 != 70 UUID.array[4]: 220 != 109 UUID.array[5]: 73 != 90 UUID.array[6]: 73 != 79 UUID.array[7]: 212 != 138 UUID.array[8]: 181 != 149 UUID.array[9]: 244 != 206

or we can get following test:

func TestUUID(t *testing.T) {
	uuid1, _ := uuid.Parse("ac0bd5ef-6f92-4b03-953d-a2c4b9828c8a")
	uuid2, _ := uuid.Parse("de09e3ce-227f-49a1-b54d-9fe13af54375")

	diff := deep.Equal(uuid1, uuid1)
	if len(diff) > 0 {
		t.Error("should be equal:", diff)
	}

	diff = deep.Equal(uuid1, uuid2)
	if diff == nil {
		t.Fatal("no diff")
	}
	if len(diff) != 10 {
		t.Error("uuid expects array of len() == 10:", diff)
	}
	if diff[0] != "array[0]: 172 != 222" {
		t.Error("wrong diff:", diff[0])
	}
	if diff[1] != "array[1]: 11 != 9" {
		t.Error("wrong diff:", diff[1])
	}
	if diff[2] != "array[2]: 213 != 227" {
		t.Error("wrong diff:", diff[2])
	}
	if diff[3] != "array[3]: 239 != 206" {
		t.Error("wrong diff:", diff[3])
	}
	if diff[4] != "array[4]: 111 != 34" {
		t.Error("wrong diff:", diff[4])
	}
	if diff[5] != "array[5]: 146 != 127" {
		t.Error("wrong diff:", diff[5])
	}
	if diff[6] != "array[6]: 75 != 73" {
		t.Error("wrong diff:", diff[6])
	}
	if diff[7] != "array[7]: 3 != 161" {
		t.Error("wrong diff:", diff[7])
	}
	if diff[8] != "array[8]: 149 != 181" {
		t.Error("wrong diff:", diff[8])
	}
	if diff[9] != "array[9]: 61 != 77" {
		t.Error("wrong diff:", diff[9])
	}
}

Would it be maybe interesting to implement something like following or is it preferred to not mix other types?

	if aType == reflect.TypeOf(uuid.UUID{}) {
		aUUID := a.Interface().(uuid.UUID)
		bUUID := b.Interface().(uuid.UUID)

		if aUUID.String() != bUUID.String() {
			c.saveDiff(aUUID.String(), bUUID.String())
		}
		return
	}

	switch aKind {

Maybe even more interesting for me would be to have CustomCompare like below which would allow this lib to be kept "pure".

I am also using something like getUUIDByName("my_cool_uuid_name") in my tests and this would allow to match uuids back to "my_cool_uuid_name" for even better readability.

	// NilMapsAreEmpty causes a nil map to be equal to an empty map.
	NilMapsAreEmpty = false

	// CustomCompare allows to implement custom behaviour for specific type
	// and should return if type was handled and what is difference between a and b
	CustomCompare func(abType reflect.Type, a, b reflect.Value) (
		bool, *string, *string) = nil
...

	if CustomCompare != nil {
		handled, aDiff, bDiff := CustomCompare(aType, a, b)
		if handled {
			if aDiff != nil || bDiff != nil {
				c.saveDiff(getNilOrValue(aDiff), getNilOrValue(bDiff))
			}
			return
		}
	}

Activity

  1. daniel-nichter commented on Feb 18, 2023

    @daniel-nichter
    Member

    Something like CustomCompare might work. Can you do a PR to see how it looks and feels? (I'll try to think of a more precise or representative name for the func because "custom" is very vague, but naming things is difficult.)

  2. primus11 commented on Feb 27, 2023

    @primus11
    Author

    Will do but won't be very fast because I am on completely different project atm.

  3. daniel-nichter commented on May 2, 2025

    @daniel-nichter
    Member

    #64 would address this, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions