linkForType returns an anchor to the type if it can be generated. returns empty string if it is not a local type or unrecognized external type.
(t *types.Type, c generatorConfig, typePkgMap map[*types.Type]*apiPackage)
| 430 | // linkForType returns an anchor to the type if it can be generated. returns |
| 431 | // empty string if it is not a local type or unrecognized external type. |
| 432 | func linkForType(t *types.Type, c generatorConfig, typePkgMap map[*types.Type]*apiPackage) (string, error) { |
| 433 | t = tryDereference(t) // dereference kind=Pointer |
| 434 | |
| 435 | if isLocalType(t, typePkgMap) { |
| 436 | return "#" + anchorIDForLocalType(t, typePkgMap), nil |
| 437 | } |
| 438 | |
| 439 | var arrIndex = func(a []string, i int) string { |
| 440 | return a[(len(a)+i)%len(a)] |
| 441 | } |
| 442 | |
| 443 | // types like k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta, |
| 444 | // k8s.io/api/core/v1.Container, k8s.io/api/autoscaling/v1.CrossVersionObjectReference, |
| 445 | // github.com/knative/build/pkg/apis/build/v1alpha1.BuildSpec |
| 446 | if t.Kind == types.Struct || t.Kind == types.Pointer || t.Kind == types.Interface || t.Kind == types.Alias { |
| 447 | id := typeIdentifier(t) // gives {{ImportPath.Identifier}} for type |
| 448 | segments := strings.Split(t.Name.Package, "/") // to parse [meta, v1] from "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 449 | |
| 450 | for _, v := range c.ExternalPackages { |
| 451 | r, err := regexp.Compile(v.TypeMatchPrefix) |
| 452 | if err != nil { |
| 453 | return "", errors.Wrapf(err, "pattern %q failed to compile", v.TypeMatchPrefix) |
| 454 | } |
| 455 | if r.MatchString(id) { |
| 456 | tpl, err := texttemplate.New("").Funcs(map[string]interface{}{ |
| 457 | "lower": strings.ToLower, |
| 458 | "arrIndex": arrIndex, |
| 459 | }).Parse(v.DocsURLTemplate) |
| 460 | if err != nil { |
| 461 | return "", errors.Wrap(err, "docs URL template failed to parse") |
| 462 | } |
| 463 | |
| 464 | var b bytes.Buffer |
| 465 | if err := tpl. |
| 466 | Execute(&b, map[string]interface{}{ |
| 467 | "TypeIdentifier": t.Name.Name, |
| 468 | "PackagePath": t.Name.Package, |
| 469 | "PackageSegments": segments, |
| 470 | }); err != nil { |
| 471 | return "", errors.Wrap(err, "docs url template execution error") |
| 472 | } |
| 473 | return b.String(), nil |
| 474 | } |
| 475 | } |
| 476 | klog.Warningf("not found external link source for type %v", t.Name) |
| 477 | } |
| 478 | return "", nil |
| 479 | } |
| 480 | |
| 481 | // tryDereference returns the underlying type when t is a pointer, map, or slice. |
| 482 | func tryDereference(t *types.Type) *types.Type { |
no test coverage detected
searching dependent graphs…