Skip to content

Commit 0a14ba5

Browse files
committed
Seperate documentation lookup from find field type
1 parent a82ebbb commit 0a14ba5

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

cel/fieldpaths.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ func (f *fieldPath) Documentation() *common.Doc {
2222
return common.NewFieldDoc(f.path, f.celType.String(), f.description)
2323
}
2424

25+
type documentationProvider interface {
26+
// FindStructFieldDescription returns documentation for a field if available.
27+
// Returns false if the field could not be found.
28+
FindStructFieldDescription(typeName, fieldName string) (string, bool)
29+
}
30+
2531
type backtrack struct {
2632
// provider used to resolve types.
2733
provider types.Provider
34+
// provider for resolving documentation. nil if unsupported
35+
docProvider documentationProvider
2836
// paths of fields that have been visited along the path.
2937
path []string
3038
// types of fields that have been visited along the path. used to avoid cycles.
@@ -79,10 +87,14 @@ func (b *backtrack) expandFieldPaths(celType *Type, paths []*fieldPath) []*field
7987
continue
8088
}
8189
b.push(field, celType)
90+
description := ""
91+
if b.docProvider != nil {
92+
description, _ = b.docProvider.FindStructFieldDescription(celType.String(), field)
93+
}
8294
path := &fieldPath{
8395
celType: fieldType.Type,
8496
path: formatPath(b.path),
85-
description: fieldType.Description,
97+
description: description,
8698
isLeaf: false,
8799
}
88100
paths = append(paths, path)
@@ -137,9 +149,10 @@ func (b *backtrack) expandFieldPaths(celType *Type, paths []*fieldPath) []*field
137149
// fieldPathsForType expands the reachable fields from the given root identifier.
138150
func fieldPathsForType(provider types.Provider, identifier string, celType *Type) []*fieldPath {
139151
b := &backtrack{
140-
provider: provider,
141-
path: []string{identifier},
142-
types: []*Type{celType},
152+
provider: provider,
153+
docProvider: provider.(documentationProvider),
154+
path: []string{identifier},
155+
types: []*Type{celType},
143156
}
144157
paths := []*fieldPath{
145158
{

common/types/provider.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ type FieldType struct {
8181

8282
// GetFrom retrieves the field value on the input object, if set.
8383
GetFrom ref.FieldGetter
84-
85-
// Description provides a description of the field. Empty if no description available.
86-
Description string
8784
}
8885

8986
// Registry provides type information for a set of registered types.
@@ -209,13 +206,26 @@ func (p *Registry) FindStructFieldType(structType, fieldName string) (*FieldType
209206
return nil, false
210207
}
211208
return &FieldType{
212-
Type: fieldDescToCELType(field),
213-
IsSet: field.IsSet,
214-
GetFrom: field.GetFrom,
215-
Description: field.Documentation(),
209+
Type: fieldDescToCELType(field),
210+
IsSet: field.IsSet,
211+
GetFrom: field.GetFrom,
216212
}, true
217213
}
218214

215+
// FindStructFieldDescription returns documentation for a field if available.
216+
// Returns false if the field could not be found.
217+
func (p *Registry) FindStructFieldDescription(structType, fieldName string) (string, bool) {
218+
msgType, found := p.pbdb.DescribeType(structType)
219+
if !found {
220+
return "", false
221+
}
222+
field, found := msgType.FieldByName(fieldName)
223+
if !found {
224+
return "", false
225+
}
226+
return field.Documentation(), true
227+
}
228+
219229
// FindIdent takes a qualified identifier name and returns a ref.Val if one exists.
220230
func (p *Registry) FindIdent(identName string) (ref.Val, bool) {
221231
if t, found := p.revTypeMap[identName]; found {

0 commit comments

Comments
 (0)