-
Notifications
You must be signed in to change notification settings - Fork 262
Optionally include reachable fieldpaths in prompt #1285
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
Open
jnthntatum
wants to merge
6
commits into
google:master
Choose a base branch
from
jnthntatum:field-path-docs-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,410
−27
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
196bd25
Optionally include reachable fieldpaths in prompt
jnthntatum 0f8bc57
go mod tidy
jnthntatum 1868cc6
go mod vendor
jnthntatum b37382f
Add bazel rule for preserving proto source info
jnthntatum c01a12f
Respond to feedback:
jnthntatum bb93fe2
Seperate documentation lookup from find field type
jnthntatum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
|
|
||
| bzl_library( | ||
| name = "proto_source_info", | ||
| srcs = ["proto_source_info.bzl"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Build rule for preserving source information in proto descriptor sets.""" | ||
|
|
||
| load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo") | ||
|
|
||
| def _source_info_proto_descriptor_set(ctx): | ||
| """Returns a proto descriptor set with source information preserved.""" | ||
| srcs = depset([s for dep in ctx.attr.proto_libs for s in dep[ProtoInfo].direct_sources]) | ||
| deps = depset(transitive = [dep[ProtoInfo].transitive_descriptor_sets for dep in ctx.attr.proto_libs]) | ||
|
|
||
| src_files = srcs.to_list() | ||
| dep_files = deps.to_list() | ||
|
|
||
| args = ctx.actions.args() | ||
| args.add("--descriptor_set_out=" + ctx.outputs.out.path) | ||
| args.add("--include_imports") | ||
| args.add("--include_source_info=true") | ||
| args.add("--proto_path=.") | ||
| args.add("--proto_path=" + ctx.configuration.genfiles_dir.path) | ||
| args.add("--descriptor_set_in=" + ":".join([d.path for d in dep_files])) | ||
| args.add_all(src_files) | ||
|
|
||
| ctx.actions.run( | ||
| executable = ctx.executable._protoc, | ||
| inputs = src_files + dep_files, | ||
| outputs = [ctx.outputs.out], | ||
| arguments = [args], | ||
| mnemonic = "SourceInfoProtoDescriptorSet", | ||
| progress_message = "Generating proto descriptor set with source information for %{label}", | ||
| ) | ||
|
|
||
| source_info_proto_descriptor_set = rule( | ||
| doc = """ | ||
| Rule for generating a proto descriptor set for the transitive dependencies of proto libraries | ||
| with source information preserved. | ||
|
|
||
| This can dramatically increase the size of the descriptor set, so only use it | ||
| when necessary (e.g. for formatting documentation about a CEL environment). | ||
|
|
||
| Source info is only preserved for input files for each proto_library label in | ||
| protolibs. Transitive dependencies are included with source info stripped. | ||
| """, | ||
| attrs = { | ||
| "proto_libs": attr.label_list(providers = [[ProtoInfo]]), | ||
| "_protoc": attr.label( | ||
| default = "@com_google_protobuf//:protoc", | ||
| executable = True, | ||
| cfg = "exec", | ||
| ), | ||
| }, | ||
| outputs = { | ||
| "out": "%{name}-transitive-descriptor-set-source-info.proto.bin", | ||
| }, | ||
| implementation = _source_info_proto_descriptor_set, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package cel | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/google/cel-go/common" | ||
| "github.com/google/cel-go/common/types" | ||
| ) | ||
|
|
||
| // fieldPath represents a selection path to a field from a variable in a CEL environment. | ||
| type fieldPath struct { | ||
| celType *Type | ||
| // path represents the selection path to the field. | ||
| path string | ||
| description string | ||
| isLeaf bool | ||
| } | ||
|
|
||
| // Documentation implements the Documentor interface. | ||
| func (f *fieldPath) Documentation() *common.Doc { | ||
| return common.NewFieldDoc(f.path, f.celType.String(), f.description) | ||
| } | ||
|
|
||
| type documentationProvider interface { | ||
| // FindStructFieldDescription returns documentation for a field if available. | ||
| // Returns false if the field could not be found. | ||
| FindStructFieldDescription(typeName, fieldName string) (string, bool) | ||
| } | ||
|
|
||
| type backtrack struct { | ||
| // provider used to resolve types. | ||
| provider types.Provider | ||
| // paths of fields that have been visited along the path. | ||
| path []string | ||
| // types of fields that have been visited along the path. used to avoid cycles. | ||
| types []*Type | ||
| } | ||
|
|
||
| func (b *backtrack) push(pathStep string, celType *Type) { | ||
| b.path = append(b.path, pathStep) | ||
| b.types = append(b.types, celType) | ||
| } | ||
|
|
||
| func (b *backtrack) pop() { | ||
| b.path = b.path[:len(b.path)-1] | ||
| b.types = b.types[:len(b.types)-1] | ||
| } | ||
|
|
||
| func formatPath(path []string) string { | ||
| var buffer strings.Builder | ||
| for i, p := range path { | ||
| if i == 0 { | ||
| buffer.WriteString(p) | ||
| continue | ||
| } | ||
| if strings.HasPrefix(p, "[") { | ||
| buffer.WriteString(p) | ||
| continue | ||
| } | ||
| buffer.WriteString(".") | ||
| buffer.WriteString(p) | ||
| } | ||
| return buffer.String() | ||
| } | ||
|
|
||
| func (b *backtrack) expandFieldPaths(celType *Type, paths []*fieldPath) []*fieldPath { | ||
| if slices.ContainsFunc(b.types[:len(b.types)-1], func(t *Type) bool { return t.String() == celType.String() }) { | ||
| // Cycle detected, so stop expanding. | ||
| paths[len(paths)-1].isLeaf = false | ||
| return paths | ||
| } | ||
| switch celType.Kind() { | ||
| case types.StructKind: | ||
| fields, ok := b.provider.FindStructFieldNames(celType.String()) | ||
| if !ok { | ||
| // Caller added this type to the path, so it must be a leaf. | ||
| paths[len(paths)-1].isLeaf = true | ||
| return paths | ||
| } | ||
| for _, field := range fields { | ||
| fieldType, ok := b.provider.FindStructFieldType(celType.String(), field) | ||
| if !ok { | ||
| // Field not found, either hidden or an error. | ||
| continue | ||
| } | ||
| b.push(field, celType) | ||
| description := "" | ||
| if docProvider, ok := b.provider.(documentationProvider); ok { | ||
| description, _ = docProvider.FindStructFieldDescription(celType.String(), field) | ||
| } | ||
| path := &fieldPath{ | ||
| celType: fieldType.Type, | ||
| path: formatPath(b.path), | ||
| description: description, | ||
| isLeaf: false, | ||
| } | ||
| paths = append(paths, path) | ||
| paths = b.expandFieldPaths(fieldType.Type, paths) | ||
| b.pop() | ||
| } | ||
| return paths | ||
| case types.MapKind: | ||
| if len(celType.Parameters()) != 2 { | ||
| // dynamic map, so treat as a leaf. | ||
| paths[len(paths)-1].isLeaf = true | ||
| return paths | ||
| } | ||
| mapKeyType := celType.Parameters()[0] | ||
| mapValueType := celType.Parameters()[1] | ||
| // Add a placeholder for the map key kind (the zero value). | ||
| keyIdentifier := "" | ||
| switch mapKeyType.Kind() { | ||
| case types.StringKind: | ||
| keyIdentifier = "[\"\"]" | ||
| case types.IntKind: | ||
| keyIdentifier = "[0]" | ||
| case types.UintKind: | ||
| keyIdentifier = "[0u]" | ||
| case types.BoolKind: | ||
| keyIdentifier = "[false]" | ||
| default: | ||
| // Caller added this type to the path, so it must be a leaf. | ||
| paths[len(paths)-1].isLeaf = true | ||
| return paths | ||
| } | ||
| b.push(keyIdentifier, mapValueType) | ||
| defer b.pop() | ||
| return b.expandFieldPaths(mapValueType, paths) | ||
| case types.ListKind: | ||
| if len(celType.Parameters()) != 1 { | ||
| // dynamic list, so treat as a leaf. | ||
| paths[len(paths)-1].isLeaf = true | ||
| return paths | ||
| } | ||
| listElemType := celType.Parameters()[0] | ||
| b.push("[0]", listElemType) | ||
| defer b.pop() | ||
| return b.expandFieldPaths(listElemType, paths) | ||
| default: | ||
| paths[len(paths)-1].isLeaf = true | ||
| } | ||
|
|
||
| return paths | ||
| } | ||
|
|
||
| // fieldPathsForType expands the reachable fields from the given root identifier. | ||
| func fieldPathsForType(provider types.Provider, identifier string, celType *Type) []*fieldPath { | ||
| b := &backtrack{ | ||
| provider: provider, | ||
| path: []string{identifier}, | ||
| types: []*Type{celType}, | ||
| } | ||
| paths := []*fieldPath{ | ||
| { | ||
| celType: celType, | ||
| path: identifier, | ||
| isLeaf: false, | ||
| }, | ||
| } | ||
|
|
||
| return b.expandFieldPaths(celType, paths) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.