getDoc returns the doc string associated with types.Object parent is the name of the containing scope ("" for global scope)
(parent string, o types.Object)
| 109 | // getDoc returns the doc string associated with types.Object |
| 110 | // parent is the name of the containing scope ("" for global scope) |
| 111 | func (p *Package) getDoc(parent string, o types.Object) string { |
| 112 | n := o.Name() |
| 113 | switch tp := o.(type) { |
| 114 | case *types.Const: |
| 115 | // Check for untyped consts |
| 116 | for _, c := range p.doc.Consts { |
| 117 | for _, cn := range c.Names { |
| 118 | if n == cn { |
| 119 | return c.Doc |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | // Check for typed consts |
| 124 | scopeName := p.pkg.Scope().Lookup(n) |
| 125 | if scopeName == nil { |
| 126 | return "" |
| 127 | } |
| 128 | constType := scopeName.Type() |
| 129 | if constType == nil { |
| 130 | return "" |
| 131 | } |
| 132 | for _, t := range p.doc.Types { |
| 133 | if p.pkg.Path()+"."+t.Name == constType.String() { |
| 134 | for _, c := range t.Consts { |
| 135 | for _, cn := range c.Names { |
| 136 | if n == cn { |
| 137 | return c.Doc |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | case *types.Var: |
| 145 | if tp.IsField() && parent != "" { |
| 146 | // Find the package-scoped struct |
| 147 | for _, typ := range p.doc.Types { |
| 148 | _ = typ |
| 149 | if typ.Name != parent { |
| 150 | continue |
| 151 | } |
| 152 | // Name matches package-scoped struct. |
| 153 | // Make sure it is a struct type. |
| 154 | for _, spec := range typ.Decl.Specs { |
| 155 | typSpec, ok := spec.(*ast.TypeSpec) |
| 156 | if !ok { |
| 157 | continue |
| 158 | } |
| 159 | structSpec, ok := typSpec.Type.(*ast.StructType) |
| 160 | if !ok { |
| 161 | continue |
| 162 | } |
| 163 | // We have the package-scoped struct matching the parent name. |
| 164 | // Find the matching field. |
| 165 | for _, field := range structSpec.Fields.List { |
| 166 | for _, fieldName := range field.Names { |
| 167 | if fieldName.Name == tp.Name() { |
| 168 | return field.Doc.Text() |
no test coverage detected