From bdc856d639fcb332daa11c7876e9cf3d336bdea1 Mon Sep 17 00:00:00 2001 From: Charith Ellawala <52399125+charith-elastic@users.noreply.github.com> Date: Tue, 18 Feb 2020 08:51:50 +0000 Subject: [PATCH] Sort references (#2) * Sort references * Move sorting to method call * Add build status --- README.md | 3 +++ templates/asciidoctor/type.tpl | 2 +- types/types.go | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6617a7..eb4e7b2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +![](https://github.com/elastic/crd-ref-docs/workflows/Build/badge.svg) + + CRD Reference Documentation Generator ====================================== diff --git a/templates/asciidoctor/type.tpl b/templates/asciidoctor/type.tpl index ed95d17..24592b5 100644 --- a/templates/asciidoctor/type.tpl +++ b/templates/asciidoctor/type.tpl @@ -10,7 +10,7 @@ {{ if $type.References -}} .Appears In: **** -{{- range $type.References }} +{{- range $type.SortedReferences }} - {{ asciidocRenderTypeLink . }} {{- end }} **** diff --git a/types/types.go b/types/types.go index 5dbbb77..ef335f5 100644 --- a/types/types.go +++ b/types/types.go @@ -174,6 +174,26 @@ func (t *Type) IsAlias() bool { return t.Kind == AliasKind } +func (t *Type) SortedReferences() []*Type { + if t == nil || len(t.References) == 0 { + return nil + } + + sort.Slice(t.References, func(i, j int) bool { + if t.References[i].Name < t.References[j].Name { + return true + } + + if t.References[i].Name == t.References[j].Name { + return t.References[i].Package < t.References[j].Package + } + + return false + }) + + return t.References +} + // Field describes a field in a struct. type Field struct { Name string