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

Add protosourcepath package #3224

Merged
merged 12 commits into from
Aug 15, 2024
Merged

Add protosourcepath package #3224

merged 12 commits into from
Aug 15, 2024

Conversation

doriable
Copy link
Member

This PR adds a new package, protosourcepath. The API is
a single function, GetAssociatedSourcePaths that takes a protoreflect.SourcePath
and the option to excludeChildAssociatedPaths, and returns a list of associated paths, []protoreflect.SourcePath:

func GetAssociatedSourcePaths(
	sourcePath protoreflect.SourcePath,
	excludeChildAssociatedPaths bool,
) ([]protoreflect.SourcePath, error)
  • We expect there always to be at least one associated path.
  • excludeChildAssociatedPaths will only include complete/top-level declarations as associated paths

Semantics

  • All associated paths are based on:
    • Parent paths
    • Child paths
  • We do not consider all child paths as associated, only some
  • Some “parent paths” may actually be the associated child paths of the parent path
  • For example, a path to a field definition, [4, 0, 2, 0] would have the following associated paths:
    • [4, 0, 2, 0] the field definition
    • [4, 0] the message definition (parent path)
    • [4, 0, 1] the message name (parent associated child path)
    • [4, 0, 2, 0, 1] the field name (child path)
    • [4, 0, 2, 0, 3] the field number (child path)
    • [4, 0, 2, 0, 5] the field type (child path)
    • [4, 0, 2, 0, 6] the field type name (child path)
    • [4, 0, 2, 0, 4] the field label (child path)
  • If excludeChildAssociatedPaths == true , then child paths and parent-associated child paths would not be included.
  • So for the example above, we could get the following associated paths:
    • [4, 0, 2, 0] the field definition
    • [4, 0] the message definition (parent path)

Implementation

  • GetAssociatedSourcePaths iterates through path elements and checks them through a DFA.
  • Each state will take the current token, the full source path, and the index being parsed, and return
    the next state and associated source paths:
type state func(
	token int32,
	sourcePath protoreflect.SourcePath,
	index int, // needed to handle nested structures
	excludeChildAssociatedPaths bool,
) (state, []protoreflect.SourcePath, error)

Copy link
Contributor

github-actions bot commented Aug 10, 2024

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedAug 13, 2024, 6:57 PM

@bufdev bufdev changed the base branch from main to bufplugin August 15, 2024 17:55
Copy link
Member

@bufdev bufdev left a comment

Choose a reason for hiding this comment

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

Still needs work but we've logged what that is, merging into bufplugin so I can use this.

@bufdev bufdev merged commit f4ec09d into bufplugin Aug 15, 2024
12 checks passed
@bufdev bufdev deleted the associated-source-paths branch August 15, 2024 17:55
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.

2 participants