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

Compare public elements of struct #1309

Merged

Conversation

mchlp
Copy link
Contributor

@mchlp mchlp commented Nov 28, 2022

Summary

Adds a method to check for equality of two structs of the same type by checking ONLY the exported (public) fields.

Changes

  • Added ObjectsExportedFieldsAreEqual method in assert/assertions.go that contains core code for the functionality (loops through fields of the struct using the reflect library and only compares fields that are exported
  • Added EqualExportedValues method in assert/assertions.go that calls the ObjectsExportedFieldsAreEqual method and contains assertion related code that logs the appropriate message on failure
  • Added corresponding unit test TestObjectsExportedFieldsAreEqual in assert/assertions_test.go

Motivation

There are use cases where users want to compare only the public fields of two struct objects (when they only care about the public interface of the object and the internal private representation does not matter). This added method provides users the ability to do this with the testify library.

Example Usage:

type S struct {
	Exported int
        notExported int
}

ObjectsExportedFieldsAreEqual(S{1, 2}, S{1, 2}) => true
ObjectsExportedFieldsAreEqual(S{1, 2}, S{1, 3}) => true
ObjectsExportedFieldsAreEqual(S{1, 2}, S{2, 3}) => false

The method also works with nested structures, where ObjectsExportedFieldsAreEqual is recursively called for nested fields that are of type struct, or ObjectsAreEqualValues are called on non-struct type fields.

type Nested struct {
	Exported    interface{}
	notExported interface{}
}

type S struct {
	Exported1    interface{}
	Exported2    Nested
	notExported1 interface{}
	notExported2 Nested
}

ObjectsExportedFieldsAreEqual(S{1, Nested{2, 3}, 4, Nested{5, 6}}, S{1, Nested{2, "a"}, 4, Nested{5, 6}}) => true
ObjectsExportedFieldsAreEqual(S{1, Nested{2, 3}, 4, Nested{5, 6}}, S{"a", Nested{2, 3}, 4, Nested{5, 6}}) => false

Related issues

Closes #1033

@mchlp
Copy link
Contributor Author

mchlp commented Dec 2, 2022

@boyan-soubachov Would be great if you could take a look at this and merge if it looks good!

// ObjectsExportedFieldsAreEqual determines if the exported (public) fields of two structs are considered equal.
// If the two objects are not of the same type, or if either of them are not a struct, they are not considered equal.
//
// This function does no assertion of any kind.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Given that this is the assert package, would it not make sense if we did assert?

Right now the functionality of this is more of a helper function, should we not just change it to an assert function that asserts whether object_a's exported fields == object_b's exported fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have another function that is added further down in the file called EqualExportedValues that actually does the assertion by calling this ObjectsExportedFieldsAreEqual helper function.

We thought that it would make sense to split up the logic for actually doing the comparison (and put it into a helper function) from the actual assertion function. This seems to be the approach that is used for equality comparisons with a ObjectsAreEqual helper function that is then called by the Equal function that actually does the assertion.

@groeney
Copy link

groeney commented Mar 13, 2023

Ping is this blocked - if so what needs to be figured out? Is there any workaround to testing equality of protobuf structs (only the fields we actually care about)?

Copy link
Collaborator

@boyan-soubachov boyan-soubachov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution :)

@HaraldNordgren
Copy link
Contributor

HaraldNordgren commented Apr 19, 2023

@mchlp This is awesome!

However, after trying it out now, I notice that unexported fields are still compared in some cases. This is with a Protobuf date.Date pointer nested inside a struct:

	var myDate = date.Date{
		Year:  2023,
		Month: 3,
		Day:   15,
	}

	suite.assertListMealsResponse(ctx, user, &myDate, &my_protobuf_definitions.ListMealsResponse{
		Date: &myDate,
		...

which fails like this:

        	            	...
        	            	@@ -22,3 +5985,3 @@
        	            	   },
        	            	-  sizeCache: (int32) 7,
        	            	+  sizeCache: (int32) 0,
        	            	   unknownFields: ([]uint8) <nil>,
        	Test:       	TestIntegration/TestAPIListMeals

I also noticed that the diff will contain all unexported fields. Meaning that if you are comparing nested Protobuf messages, the terminal will be very noisy. This is what I see (truncated):

        	            	...
        	            	+         sizecacheOffset: (impl.offset) 0x8,
        	            	+         unknownOffset: (impl.offset) 0x10,
        	            	+         unknownPtrKind: (bool) false,
        	            	+         extensionOffset: (impl.offset) 0xffffffffffffffff,
        	            	+         needsInitCheck: (bool) false,
        	            	+         isMessageSet: (bool) false,
        	            	+         numRequiredFields: (uint8) 0
        	            	+        }
        	            	+       }),
        	            	+       typ: (impl.validationType) 1,
        	            	+       keyType: (impl.validationType) 0,
        	            	+       valType: (impl.validationType) 0,
        	            	+       requiredBit: (uint64) 0
        	            	+      },
        	            	+      num: (protowire.Number) 2,
        	            	+      offset: (impl.offset) 0x30,
        	            	+      wiretag: (uint64) 18,
        	            	+      tagsize: (int) 1,
        	            	+      isPointer: (bool) true,
        	            	+      isRequired: (bool) false
        	            	+     })
        	            	+    },
        	            	+    sizecacheOffset: (impl.offset) 0x8,
        	            	+    unknownOffset: (impl.offset) 0x10,
        	            	+    unknownPtrKind: (bool) false,
        	            	+    extensionOffset: (impl.offset) 0xffffffffffffffff,
        	            	+    needsInitCheck: (bool) false,
        	            	+    isMessageSet: (bool) false,
        	            	+    numRequiredFields: (uint8) 0
        	            	+   }
        	            	+  })
        	            	  },
        	            	@@ -26,3 +5989,3 @@
        	            	   Month: (int32) 3,
        	            	-  Day: (int32) 14
        	            	+  Day: (int32) 15
        	            	  }),
        	Test:       	TestIntegration/TestAPIListMeals

@HaraldNordgren
Copy link
Contributor

HaraldNordgren commented Apr 19, 2023

Here is a proposed fix by me: #1379

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

Successfully merging this pull request may close these issues.

Impossible to just compare public elements of a struct.
4 participants