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

option for ignoring unexported fields #89

Open
aexvir opened this issue Jul 13, 2022 · 3 comments
Open

option for ignoring unexported fields #89

aexvir opened this issue Jul 13, 2022 · 3 comments

Comments

@aexvir
Copy link

aexvir commented Jul 13, 2022

👋🏻 hey there
I'm using this library as it has been the most consistent way to compare structs that I've found so far, thanks!

one feature that I'm missing is the option to ignore unexported fields from being compared and marked as differences, I know I can strip them by marshal/unmarshal but if that functionality would be provided by the library itself that would be amazing 🙂

I'm ok with implementing it myself and submitting a pr!
I would like to know

  • is this something you're ok with adding?
  • is there some limitation that I could hit while adding this feature?
  • any hints where I should start from?

I haven't checked the codebase yet, that's why I'm asking this 😅

@aexvir
Copy link
Author

aexvir commented Jul 13, 2022

well... I just realized I can use the Filter function 😅 🙈

diff.Filter(
    func(path []string, parent reflect.Type, field reflect.StructField) bool {
        return field.IsExported()
    },
)

hmm... I guess that's the way to handle it, right?
I'd be ok with either documenting it as example or adding an explicit option for this
imo that would make it more obvious

wdyt?

@purehyperbole
Copy link
Member

Hey @aexvir , thanks for raising the issue!

So there are a couple of ways to handle this:

  1. use diff.Filter as you suggest
  2. use diff:"-" tag on your structs

So 2 would be the best way to go if you have control over how those structs are defined:

type MyStruct struct {
    DontDiff string `diff:"-"`  // this will be ignored
    Diff     string             // will be diffed
}

I hope that helps!

@aexvir
Copy link
Author

aexvir commented Jul 15, 2022

while I was aware of the diff:"-" struct tag I don't want to keep track of this and make sure to add it every time we add an unexported field; the filter works alright though, so I'm happy with that

I still think that maybe documenting this would be nice, as it didn't struck me as something obvious on the first place
but up to you 🙂 feel free to close this issue if there is nothing else to discuss here

and thanks for the help!

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

No branches or pull requests

2 participants