Skip to content

Commit

Permalink
Lookup doc strings with hierarchical fieldpath names when generating …
Browse files Browse the repository at this point in the history
…fields

Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
  • Loading branch information
ulucinar committed Sep 7, 2022
1 parent 7b4ef8b commit d0af53c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/registry/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ func getRootPath(n *html.Node) string { // nolint: gocyclo
if ulNode == nil {
return ""
}
for pNode = ulNode; pNode != nil && (pNode.Data != "p" || !checkBlockParagraph(pNode)); pNode = pNode.PrevSibling {
for pNode = ulNode.PrevSibling; pNode != nil && (pNode.Data != "p" || !checkBlockParagraph(pNode)); pNode = pNode.PrevSibling {
// intentionally left empty
}
if pNode == nil {
return ""
}
for codeNode = pNode.FirstChild; codeNode != nil && codeNode.Data != "code"; codeNode = codeNode.NextSibling {
// intentionally left empty
}
if codeNode == nil || codeNode.FirstChild == nil {
return ""
Expand Down
38 changes: 36 additions & 2 deletions pkg/types/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ type Field struct {
SelectorName string
}

func getDocString(cfg *config.Resource, f *Field, tfPath []string) string {
hName := f.Name.Snake
if len(tfPath) > 0 {
hName = fieldPath(append(tfPath, hName))
}
docString := ""
if cfg.MetaResource != nil {
lm := 0
match := ""
for k := range cfg.MetaResource.ArgumentDocs {
parts := strings.Split(k, ".")
if parts[len(parts)-1] == f.Name.Snake {
lm = len(f.Name.Snake)
match = k
}
}
if lm == 0 {
for k := range cfg.MetaResource.ArgumentDocs {
if strings.HasSuffix(hName, k) {
if len(k) > lm {
lm = len(k)
match = k
}
}
}
}
if lm > 0 {
docString = strings.TrimSpace(cfg.MetaResource.ArgumentDocs[match])
}
}
return docString
}

// NewField returns a constructed Field object.
func NewField(g *Builder, cfg *config.Resource, r *resource, sch *schema.Schema, snakeFieldName string, tfPath, xpPath, names []string, asBlocksMode bool) (*Field, error) {
f := &Field{
Expand All @@ -41,8 +74,9 @@ func NewField(g *Builder, cfg *config.Resource, r *resource, sch *schema.Schema,
// Use registry descriptions for fields if exists
// Otherwise, use schema as source
var commentText string
if cfg.MetaResource != nil {
commentText = cfg.MetaResource.ArgumentDocs[f.Name.Snake] + "\n"
docString := getDocString(cfg, f, tfPath)
if len(docString) > 0 {
commentText = docString + "\n"
}
commentText += f.Schema.Description

Expand Down

0 comments on commit d0af53c

Please sign in to comment.