Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uuid is unreadable since it is compared as array #57

Open
primus11 opened this issue Jan 20, 2023 · 2 comments
Open

uuid is unreadable since it is compared as array #57

primus11 opened this issue Jan 20, 2023 · 2 comments

Comments

@primus11
Copy link

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
		}
	}
@daniel-nichter
Copy link
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.)

@primus11
Copy link
Author

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

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

No branches or pull requests

2 participants