Skip to content

Commit 51dd72f

Browse files
committed
Seperate documentation lookup from find field type
1 parent a82ebbb commit 51dd72f

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

cel/fieldpaths.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ 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
@@ -79,10 +85,14 @@ func (b *backtrack) expandFieldPaths(celType *Type, paths []*fieldPath) []*field
7985
continue
8086
}
8187
b.push(field, celType)
88+
description := ""
89+
if docProvider, ok := b.provider.(documentationProvider); ok {
90+
description, _ = docProvider.FindStructFieldDescription(celType.String(), field)
91+
}
8292
path := &fieldPath{
8393
celType: fieldType.Type,
8494
path: formatPath(b.path),
85-
description: fieldType.Description,
95+
description: description,
8696
isLeaf: false,
8797
}
8898
paths = append(paths, path)

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)