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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoparse: enforce unique JSON names #523

Merged
merged 2 commits into from Aug 17, 2022
Merged

Conversation

jhump
Copy link
Owner

@jhump jhump commented Aug 16, 2022

Fixes #358

This is done in a case-insensitive way, just as protoc does it. However, I wish I knew why the protobuf team chose to make it case-insensitive. Case-sensitivity issues should generally be warnings or linter errors, not compile errors. 馃し

@jhump
Copy link
Owner Author

jhump commented Aug 16, 2022

@amckinney, mind taking a look at this?

Copy link

@amckinney amckinney left a comment

Choose a reason for hiding this comment

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

Looks good! A couple small details, but the test results look great.

if name != existing.orig {
otherName = fmt.Sprintf(" %q", existing.orig)
}
conflictErr := errorWithPos(fldNode.Start(), "%s: %s JSON name %q conflicts with %s JSON name%s of field %s, defined at %v",

Choose a reason for hiding this comment

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

Suggested change
conflictErr := errorWithPos(fldNode.Start(), "%s: %s JSON name %q conflicts with %s JSON name%s of field %s, defined at %v",
conflictErr := errorWithPos(fldNode.Start(), "%s: %s JSON name %q conflicts with %s JSON name %s of field %s, defined at %v",

Choose a reason for hiding this comment

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

I would have thought we'd need a space here, but is there always a space in the value we're including here? By the error messages included in your test, everything looks correct, so I'm a little surprised.

This would be the existing.source.GetName() value.

Copy link
Owner Author

Choose a reason for hiding this comment

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

We only print a name here when the two names that conflict differ (which can happen if they vary only by case). So if the previously defined JSON name is the same as the one causing the conflict, we omit it entirely. See how the name var is actually constructed above: if it's not empty, it includes a leading space.

Choose a reason for hiding this comment

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

Ah ha! Thanks for the clarification - makes sense.

Copy link
Owner Author

Choose a reason for hiding this comment

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

FWIW, this is actually for the origValue value. The existing.source.GetName() is the next %s in the format string.

desc/protoparse/linker.go Outdated Show resolved Hide resolved
@jhump jhump merged commit 3057e78 into master Aug 17, 2022
@jhump jhump deleted the jh/check-json-name-conflicts branch August 17, 2022 01:23
pkwarren added a commit to bufbuild/protocompile that referenced this pull request Sep 19, 2022
Port jhump/protoreflect#523 from protoparse over
to protocompile.
pkwarren added a commit to bufbuild/protocompile that referenced this pull request Sep 20, 2022
Port jhump/protoreflect#523 and
jhump/protoreflect#523  from protoparse
over to protocompile.

Co-authored-by: Joshua Humphries <jhumphries@buf.build>
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.

protoparse: does not check for unique json_name in proto3 files
2 participants