Skip to content

Commit e12affe

Browse files
committed
feat: added support for generics to doc generation
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 079cb85 commit e12affe

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

codegen/internal/generator/funcmaps/funcmaps.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func FuncMap() template.FuncMap {
5858
"returns": PrintReturns,
5959
"sourceLink": sourceLink,
6060
"titleize": titleize,
61+
"slugize": slugize,
6162
}
6263
}
6364

@@ -345,3 +346,20 @@ func printDebug(in any) string {
345346
func printDate() string {
346347
return time.Now().Format(time.DateOnly)
347348
}
349+
350+
// slugize converts a name into a markdown ref inside a document.
351+
func slugize(in string) string {
352+
return strings.ToLower(
353+
strings.Map(func(r rune) rune {
354+
switch r {
355+
case '.', '_', ' ', '\t', ':':
356+
return '-'
357+
case '[', ']', ',':
358+
return -1
359+
default:
360+
return r
361+
}
362+
},
363+
in,
364+
))
365+
}

codegen/internal/generator/templates/doc_page.md.gotmpl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,30 @@ keywords:
3131
_All links point to <{{ $godoc }}>_
3232

3333
This domain exposes {{ .RefCount }} functionalities.
34+
{{- if .HasGenerics }}
35+
Generic assertions are marked with a {{ print "{{" }}% icon icon="star" color=orange %{{ print "}}" }}
36+
{{- end }}
3437

38+
```tree
3539
{{- with .Package }}
3640
{{- $enableGenerics := .EnableGenerics }}
3741
{{- range .Functions }}{{/* functions in internal/assertions annotated with "domain:" */}}
3842
{{- if or (not .IsGeneric) ($enableGenerics) }}
3943
{{- if and (not .IsHelper) (not .IsConstructor) }}
44+
- [{{ .GenericName }}](#{{ slugize .GenericName }}) | {{ if .IsGeneric}}star | orange {{ else }}angles-right{{ end }}
45+
{{- end }}
46+
{{- end }}
47+
{{- end }}
48+
{{- end }}
49+
```
4050

41-
### {{ .Name }}
51+
{{- with .Package }}
52+
{{- $enableGenerics := .EnableGenerics }}
53+
{{- range .Functions }}{{/* functions in internal/assertions annotated with "domain:" */}}
54+
{{- if or (not .IsGeneric) ($enableGenerics) }}
55+
{{- if and (not .IsHelper) (not .IsConstructor) }}
56+
57+
### {{ .GenericName }}{{ if .IsGeneric }} {{ print "{{" }}% icon icon="star" color=orange %{{ print "}}" }}{{ end }}{#{{ slugize .GenericName }}}
4258
{{- if .IsDeprecated }}
4359

4460
{{ print "{{" }}% expand title="{{ .Name }} [DEPRECATED]" %{{ print "}}" }}
@@ -55,14 +71,14 @@ This domain exposes {{ .RefCount }} functionalities.
5571
{{ print "{{" }}% tab title="{{ .Package }}" style="secondary" %{{ print "}}" }}
5672
| Signature | Usage |
5773
|--|--|
58-
| [`{{.Package }}.{{ .Name }}({{ params .AllParams }}) {{ returns .Returns }}`]({{ $godoc }}/{{ .Package }}#{{ .Name }}) | package-level function |
74+
| [`{{.Package }}.{{ .GenericName }}({{ params .AllParams }}) {{ returns .Returns }}`]({{ $godoc }}/{{ .Package }}#{{ .Name }}) | package-level function |
5975
{{- if .EnableFormat }}
60-
| [`{{ .Package }}.{{ .Name }}f(t T, {{ params .Params }}, msg string, args ...any) {{ returns .Returns }}`]({{ $godoc }}/{{ .Package }}#{{ .Name }}f) | formatted variant |
76+
| [`{{ .Package }}.{{ .GenericName "f" }}(t T, {{ params .Params }}, msg string, args ...any) {{ returns .Returns }}`]({{ $godoc }}/{{ .Package }}#{{ .Name }}f) | formatted variant |
6177
{{- end }}
62-
{{- if .EnableForward }}
78+
{{- if and .EnableForward (not .IsGeneric) }}
6379
| [`{{ .Package}}.(*{{ .Receiver }}).{{ .Name }}({{ params .Params }}) {{ returns .Returns }}`]({{ $godoc }}/{{ .Package }}#{{ .Receiver }}.{{ .Name }}) | method variant |
6480
{{- end }}
65-
{{- if and .EnableForward .EnableFormat }}
81+
{{- if and .EnableForward .EnableFormat (not .IsGeneric) }}
6682
| [`{{ .Package }}.(*{{ .Receiver }}).{{ .Name }}f({{ params .Params }}, msg string, args ..any)`]({{ $godoc }}/{{ .Package }}#{{ .Receiver }}.{{ .Name }}f) | method formatted variant |
6783
{{- end }}
6884
{{ print "{{" }}% /tab %{{ print "}}" }}

codegen/internal/model/documentation.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ type Document struct {
8484
Weight int
8585
}
8686

87+
func (d Document) HasGenerics() bool {
88+
if d.Package == nil {
89+
return false
90+
}
91+
92+
for _, fn := range d.Package.Functions {
93+
if fn.IsGeneric {
94+
return true
95+
}
96+
}
97+
98+
return false
99+
}
100+
87101
type ExtraPackages []*AssertionPackage
88102

89103
func (pkgs ExtraPackages) LookupFunction(name string) []FunctionWithContext {

internal/assertions/boolean.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ func True(t T, value bool, msgAndArgs ...any) bool {
2929
//
3030
// # Usage
3131
//
32-
// type B bool
33-
// var b B = true
32+
// type B bool
33+
// var b B = true
3434
//
35-
// assertions.True(t, b)
35+
// assertions.True(t, b)
3636
//
3737
// # Examples
3838
//
3939
// success: 1 == 1
4040
// failure: 1 == 0
4141
func TrueT[B Boolean](t T, value B, msgAndArgs ...any) bool {
42+
// Domain: boolean
4243
if !bool(value) {
4344
if h, ok := t.(H); ok {
4445
h.Helper()

0 commit comments

Comments
 (0)