litter icon indicating copy to clipboard operation
litter copied to clipboard

Don't add pointer comments, or at least let the user disable them

Open purpleidea opened this issue 9 months ago • 9 comments

When printing with Sdump, we sometimes see these pointer comments:

// p0

and so on... This makes it more difficult to diff. Here's a workaround, but it would be nice to have an option to disable them:

pattern := regexp.MustCompile(`\ \/\/\ p[0-9]+$`) // the p0, p1 comments...
clean := func(s string) string {
	lines := []string{}
	for _, line := range strings.Split(s, "\n") {
		s := pattern.ReplaceAllLiteralString(line, "")
		lines = append(lines, s)
	}
	return strings.Join(lines, "\n")
}

lo1 := clean(lo.Sdump(exp))
lo2 := clean(lo.Sdump(ast))
if lo1 == lo2 { // simple diff
	return
}

I use this to diff two AST's in tests in https://github.com/purpleidea/mgmt/

Thanks!

purpleidea avatar Feb 26 '25 01:02 purpleidea

You can disable them with the DisablePointerReplacement option. However, you will then get duplicate data — not sure if that's a problem for you?

atombender avatar Feb 26 '25 10:02 atombender

As you can see, I am using the DisablePointerReplacement option, and the comments still appear.

https://github.com/purpleidea/mgmt/commit/d7ecc72b4144bf869948db47b8fa4c17d07eeee0#diff-ae0ef0944b296aac81d71d5217e3acc998acd6fd6ca73aee7706ffe24192f8ccR2235

purpleidea avatar Feb 26 '25 15:02 purpleidea

I guess we still include the comment even when there's no circular relationship. I'll look into disabling the comments.

atombender avatar Feb 26 '25 16:02 atombender

The challenge here is what to provide instead when there is a cyclic reference. For example, if you have a cyclic reference today, you get:

&Itself{ // p0
  Value: p0,
}

Without this comment, the p0 would not be defined and we can't provide a replacement. Suggestions?

atombender avatar Feb 26 '25 20:02 atombender

My use case is that your library is fantastic for tests and running a diff between expected and actual AST's for example.

So in my situation there are no cyclic references. In this situation there should be no comments at all. This way the diff will work correctly.

purpleidea avatar Feb 26 '25 20:02 purpleidea

I understand. It's just that we're targeting all kinds of use cases. Unfortunately, we cannot know ahead of time if a pointer is going to be recursive, so we can't make an exception for that case. I suppose we could dump out something like /* recursive pointer */.

atombender avatar Feb 26 '25 20:02 atombender

I suspect a flag to disable this line: https://github.com/sanity-io/litter/blob/4fde30cab354d1dc18c21d2a1b9ac5c75600ec19/dump.go#L90 would probably be sufficient.

But I didn't test this yet.

purpleidea avatar Feb 26 '25 20:02 purpleidea

Yes, but then you still end up with the pointer replacements. In a recursive structure, it will say:

&Itself{
  Value: p0,
}

…and p0 isn't "defined" anywhere.

atombender avatar Feb 26 '25 20:02 atombender

I agree, this flag is useful for the most common scenario where there aren't recursive references.

purpleidea avatar Feb 26 '25 20:02 purpleidea