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

Refactor equals #3958

Merged
merged 1 commit into from
Apr 4, 2019
Merged

Refactor equals #3958

merged 1 commit into from
Apr 4, 2019

Conversation

aledbf
Copy link
Member

@aledbf aledbf commented Apr 2, 2019

What this PR does / why we need it:

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #3956

Special notes for your reviewer:

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 2, 2019
@aledbf
Copy link
Member Author

aledbf commented Apr 2, 2019

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 2, 2019
@aledbf aledbf removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 2, 2019
@aledbf
Copy link
Member Author

aledbf commented Apr 3, 2019 via email

}

sameResult := StringElementsMatch(testCase.listB, testCase.listA)
if result != testCase.expected {
Copy link
Member

Choose a reason for hiding this comment

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

/s/result/sameResult

@ElvinEfendi
Copy link
Member

ElvinEfendi commented Apr 3, 2019

can we not make this general enough so that we don't repeat the same logic for different types? maybe something like following in sets package?

func Compare(s1, s2 []interface, equalFunc func(e1 interface, e2 interface) bool) bool {

with this you can still have type specific helper functions, but based on this common function. that way we would not be repeating the same logic in multiple functions.

@aledbf
Copy link
Member Author

aledbf commented Apr 3, 2019

can we not make this general enough so that we don't repeat the same logic for different types? maybe something like following in sets package?

I started doing just that but it implies using reflection



func Compare(listA, listB interface{}, equalFunc func(e1, e2 interface{}) bool) bool {
	a := reflect.ValueOf(listA)
	log.Println(reflect.TypeOf(a).Kind())
	if reflect.TypeOf(a).Kind() != reflect.Slice &&
		reflect.TypeOf(a).Kind() != reflect.Struct &&
		reflect.TypeOf(a).Kind() != reflect.Array {
		return false
	}

	b := reflect.ValueOf(listB)
	if reflect.TypeOf(b).Kind() != reflect.Slice &&
		reflect.TypeOf(b).Kind() != reflect.Array &&
		reflect.TypeOf(b).Kind() != reflect.Struct {
		return false
	}

	if a.Len() != b.Len() {
		return false
	}

	visited := make([]bool, b.Len())

	for i := 0; i < a.Len(); i++ {
		element := a.Index(i).Interface()

		found := false
		for j := 0; j < b.Len(); j++ {
			if visited[j] {
				continue
			}

			if equalFunc(element, b.Index(j).Interface()) {
				visited[j] = true
				found = true
				break
			}
		}

		if !found {
			return false
		}
	}

	return true
}

If you are ok with that I can change the code

Edit: not sure if some of the equalFunc would require type casting or not

@ElvinEfendi
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 3, 2019
@aledbf aledbf added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 3, 2019
@aledbf
Copy link
Member Author

aledbf commented Apr 3, 2019

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 4, 2019
@aledbf aledbf force-pushed the fix-equals branch 2 times, most recently from 6dde93f to 51de018 Compare April 4, 2019 01:05
internal/sets/match.go Outdated Show resolved Hide resolved
@aledbf aledbf removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 4, 2019
@ElvinEfendi
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 4, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aledbf, ElvinEfendi

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit dcb1a04 into kubernetes:master Apr 4, 2019
@aledbf aledbf deleted the fix-equals branch April 6, 2019 13:52

func isIterable(obj reflect.Value) bool {
switch reflect.TypeOf(obj).Kind() {
case reflect.Struct, reflect.Slice, reflect.Array:
Copy link

Choose a reason for hiding this comment

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

@aledbf why the type struct is iterative?

Copy link
Member Author

Choose a reason for hiding this comment

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

@dongqi1990 fixed by #3980

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

some questions about the Equal function
4 participants