@@ -10,8 +10,10 @@ import (
10
10
"os"
11
11
"path/filepath"
12
12
"reflect"
13
+ "regexp"
13
14
"sort"
14
15
"strings"
16
+ "unicode"
15
17
)
16
18
17
19
const (
@@ -27,18 +29,16 @@ This Document documents the types introduced by the %s Operator.
27
29
28
30
var (
29
31
links = map [string ]string {
30
- "metav1.ObjectMeta" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#objectmeta-v1-meta" ,
31
- "metav1.ListMeta" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#listmeta-v1-meta" ,
32
- "metav1.LabelSelector" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#labelselector-v1-meta" ,
33
- "corev1.SecretKeySelector" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#secretkeyselector-v1-core" ,
34
- "corev1.Toleration" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#toleration-v1-core" ,
35
- "corev1.VolumeSource" : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#volume-v1-core" ,
36
- "plugins.Secret" : "../secret.md" ,
37
- "Secret" : "secret.md" ,
38
- "plugins.TLS" : "../tls.md" ,
32
+ "plugins.Secret" : "../secret.md" ,
33
+ "Secret" : "secret.md" ,
34
+ "plugins.TLS" : "../tls.md" ,
39
35
}
40
36
41
- selfLinks = map [string ]string {}
37
+ kubernetes_link_templates = map [string ]string {
38
+ "corev1." : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#%s-v1-core" ,
39
+ "metav1." : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#%s-v1-meta" ,
40
+ "rbacv1." : "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#%s-v1-rbac-authorization-k8s-io" ,
41
+ }
42
42
43
43
unincludedKeyWords = []string {
44
44
"deepcopy.go" ,
@@ -55,7 +55,7 @@ type DocumentsLocation struct {
55
55
56
56
// Inspired by coreos/prometheus-operator: https://github.com/coreos/prometheus-operator
57
57
func main () {
58
- var pluginsLocations = []DocumentsLocation {
58
+ pluginsLocations : = []DocumentsLocation {
59
59
{
60
60
path : fluentbitPluginPath ,
61
61
name : "fluentbit" ,
@@ -67,7 +67,7 @@ func main() {
67
67
}
68
68
plugins (pluginsLocations )
69
69
70
- var crdsLocations = []DocumentsLocation {
70
+ crdsLocations : = []DocumentsLocation {
71
71
{
72
72
path : fluentbitCrdsPath ,
73
73
name : "fluentbit" ,
@@ -105,7 +105,7 @@ func plugins(docsLocations []DocumentsLocation) {
105
105
for _ , src := range srcs {
106
106
var buffer bytes.Buffer
107
107
108
- types := ParseDocumentationFrom (src , true )
108
+ types := ParseDocumentationFrom (src , dl . name , true )
109
109
110
110
for _ , t := range types {
111
111
strukt := t [0 ]
@@ -164,7 +164,7 @@ func crds(docsLocations []DocumentsLocation) {
164
164
var buffer bytes.Buffer
165
165
var types []KubeTypes
166
166
for _ , src := range srcs {
167
- types = append (types , ParseDocumentationFrom (src , false )... )
167
+ types = append (types , ParseDocumentationFrom (src , dl . name , false )... )
168
168
}
169
169
170
170
sort .Slice (types , func (i , j int ) bool {
@@ -228,7 +228,7 @@ type Pair struct {
228
228
// KubeTypes is an array to represent all available types in a parsed file. [0] is for the type itself
229
229
type KubeTypes []Pair
230
230
231
- func ParseDocumentationFrom (src string , shouldSort bool ) []KubeTypes {
231
+ func ParseDocumentationFrom (src string , dl_name string , shouldSort bool ) []KubeTypes {
232
232
var docForTypes []KubeTypes
233
233
234
234
pkg := astFrom (src )
@@ -254,7 +254,7 @@ func ParseDocumentationFrom(src string, shouldSort bool) []KubeTypes {
254
254
ks = append (ks , Pair {kubType .Name , fmtRawDoc (kubType .Doc ), "" })
255
255
256
256
for _ , field := range structType .Fields .List {
257
- typeString := fieldType (field .Type )
257
+ typeString := fieldType (field .Type , dl_name )
258
258
if n := fieldName (field ); n != "-" {
259
259
fieldDoc := fmtRawDoc (field .Doc .Text ())
260
260
ks = append (ks , Pair {n , fieldDoc , typeString })
@@ -314,23 +314,63 @@ func fmtRawDoc(rawDoc string) string {
314
314
return postDoc
315
315
}
316
316
317
- func toLink (typeName string ) string {
317
+ func tryKubernetesLink (typeName string ) (string , bool ) {
318
+ for prefix , link_template := range kubernetes_link_templates {
319
+ if strings .HasPrefix (typeName , prefix ) {
320
+ typeName = strings .ToLower (strings .TrimPrefix (typeName , prefix ))
321
+ return fmt .Sprintf (link_template , typeName ), true
322
+ }
323
+ }
324
+ return "" , false
325
+ }
326
+
327
+ var (
328
+ matchFirstCap = regexp .MustCompile ("(.)([A-Z][a-z]+)" )
329
+ matchAllCap = regexp .MustCompile ("([a-z0-9])([A-Z])" )
330
+ )
331
+
332
+ func ToSnakeCase (str string ) string {
333
+ snake := matchFirstCap .ReplaceAllString (str , "${1}_${2}" )
334
+ snake = matchAllCap .ReplaceAllString (snake , "${1}_${2}" )
335
+ return strings .ToLower (snake )
336
+ }
337
+
338
+ func toLink (typeName string , documentLocationName string ) string {
339
+ primitiveTypes := map [string ]bool {
340
+ "string" : true ,
341
+ "bool" : true ,
342
+ "int32" : true ,
343
+ "int64" : true ,
344
+ "map[string]string" : true ,
345
+ }
346
+
347
+ if _ , ok := primitiveTypes [typeName ]; ok {
348
+ return typeName
349
+ }
350
+
351
+ link , hasLink := links [typeName ]
352
+ if hasLink {
353
+ return wrapInLink (typeName , link )
354
+ }
355
+
318
356
if strings .Contains (typeName , "input." ) ||
319
357
strings .Contains (typeName , "output." ) ||
320
358
strings .Contains (typeName , "filter." ) ||
321
- strings .Contains (typeName , "parser." ) {
359
+ strings .Contains (typeName , "parser." ) ||
360
+ strings .Contains (typeName , "custom." ) {
322
361
// Eg. *output.Elasticsearch => ../plugins/output/elasticsearch.md
323
- link := fmt .Sprintf ("plugins/%s.md" , strings .ReplaceAll (strings .ToLower (typeName ), "." , "/" ))
362
+ pluginType , pluginName , _ := strings .Cut (typeName , "." )
363
+ link := fmt .Sprintf ("plugins/%s/%s/%s.md" , documentLocationName , pluginType , ToSnakeCase (pluginName ))
324
364
return wrapInLink (typeName , link )
325
365
}
326
366
327
- selfLink , hasSelfLink := selfLinks [ typeName ]
328
- if hasSelfLink {
329
- return wrapInLink (typeName , selfLink )
367
+ k8sLink , hasK8sLink := tryKubernetesLink ( typeName )
368
+ if hasK8sLink {
369
+ return wrapInLink (typeName , k8sLink )
330
370
}
331
371
332
- link , hasLink := links [ typeName ]
333
- if hasLink {
372
+ if ! strings . Contains ( typeName , "." ) && len ( typeName ) > 0 && unicode . IsUpper ([] rune ( typeName )[ 0 ]) {
373
+ link := fmt . Sprintf ( "#%s" , strings . ToLower ( typeName ))
334
374
return wrapInLink (typeName , link )
335
375
}
336
376
@@ -362,22 +402,22 @@ func fieldName(field *ast.Field) string {
362
402
return jsonTag
363
403
}
364
404
365
- func fieldType (typ ast.Expr ) string {
405
+ func fieldType (typ ast.Expr , dl_name string ) string {
366
406
switch typ .(type ) {
367
407
case * ast.Ident :
368
- return toLink (typ .(* ast.Ident ).Name )
408
+ return toLink (typ .(* ast.Ident ).Name , dl_name )
369
409
case * ast.StarExpr :
370
- return "*" + fieldType (typ .(* ast.StarExpr ).X )
410
+ return "*" + fieldType (typ .(* ast.StarExpr ).X , dl_name )
371
411
case * ast.SelectorExpr :
372
412
e := typ .(* ast.SelectorExpr )
373
413
pkg := e .X .(* ast.Ident )
374
414
t := e .Sel
375
- return toLink (pkg .Name + "." + t .Name )
415
+ return toLink (pkg .Name + "." + t .Name , dl_name )
376
416
case * ast.ArrayType :
377
- return "[]" + toLink ( fieldType (typ .(* ast.ArrayType ).Elt ) )
417
+ return "[]" + fieldType (typ .(* ast.ArrayType ).Elt , dl_name )
378
418
case * ast.MapType :
379
419
mapType := typ .(* ast.MapType )
380
- return "map[" + toLink (fieldType (mapType .Key ) ) + "]" + toLink (fieldType (mapType .Value ) )
420
+ return "map[" + toLink (fieldType (mapType .Key , dl_name ), dl_name ) + "]" + toLink (fieldType (mapType .Value , dl_name ), dl_name )
381
421
default :
382
422
return ""
383
423
}
0 commit comments