Skip to content

Commit 5e93af4

Browse files
committed
anonymous structs shall be embedded
1 parent 3fd2299 commit 5e93af4

File tree

5 files changed

+81
-31
lines changed

5 files changed

+81
-31
lines changed

asciidoc/linking_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,53 @@ func signatureMarkup(ctx *TemplateContext, doc *SignatureDoc) string {
244244
}
245245
return b.String()
246246
}
247+
248+
func TestFieldSummaryAnonymousStructNoLink(t *testing.T) {
249+
ctx := testContextWithMode(TypeLinksInternal)
250+
251+
container := &goparser.GoStruct{Name: "Container", File: ctx.File}
252+
ctx.Struct = container
253+
ctx.Package.Structs = []*goparser.GoStruct{container}
254+
255+
// Anonymous struct type
256+
structType := &goparser.GoType{File: ctx.File, Type: "struct", Kind: goparser.TypeKindStruct}
257+
258+
field := &goparser.GoField{
259+
Struct: container,
260+
File: ctx.File,
261+
Name: "Config",
262+
Type: "struct",
263+
Decl: "Config struct { Value string }",
264+
TypeInfo: structType,
265+
}
266+
267+
got := ctx.fieldSummary(field)
268+
expected := "Config\tstruct"
269+
assert.Equal(t, expected, got)
270+
assert.NotContains(t, got, "<<", "Anonymous struct should not have internal link")
271+
}
272+
273+
func TestFieldSummaryAnonymousInterfaceNoLink(t *testing.T) {
274+
ctx := testContextWithMode(TypeLinksInternal)
275+
276+
container := &goparser.GoStruct{Name: "Container", File: ctx.File}
277+
ctx.Struct = container
278+
ctx.Package.Structs = []*goparser.GoStruct{container}
279+
280+
// Anonymous interface type
281+
interfaceType := &goparser.GoType{File: ctx.File, Type: "interface{}", Kind: goparser.TypeKindInterface}
282+
283+
field := &goparser.GoField{
284+
Struct: container,
285+
File: ctx.File,
286+
Name: "Data",
287+
Type: "interface{}",
288+
Decl: "Data interface{}",
289+
TypeInfo: interfaceType,
290+
}
291+
292+
got := ctx.fieldSummary(field)
293+
expected := "Data\tinterface{}"
294+
assert.Equal(t, expected, got)
295+
assert.NotContains(t, got, "<<", "Anonymous interface should not have internal link")
296+
}

asciidoc/links.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ func (t *TemplateContext) renderType(gt *goparser.GoType, scope map[string]struc
341341
}
342342
case goparser.TypeKindIdent, goparser.TypeKindSelector:
343343
return t.linkIdentifier(gt.Type, gt.File, scope)
344+
case goparser.TypeKindStruct, goparser.TypeKindInterface:
345+
// Anonymous structs and interfaces are native Go constructs and should not be linked
346+
return gt.Type
344347
default:
345348
if len(gt.Inner) == 0 {
346349
return t.linkIdentifier(gt.Type, gt.File, scope)

asciidoc/testdata/producer_basic.golden

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616

17+
1718
=== Imports
1819
[source, go]
1920
----
@@ -70,8 +71,6 @@ Name is the service name.
7071

7172

7273

73-
74-
7574
[[example-com-docs-sample-Config]]
7675

7776
=== Config
@@ -91,8 +90,6 @@ Config stores formatter configuration for services.
9190

9291
Formatter applies formatting.
9392

94-
95-
9693
==== Receivers
9794

9895
===== FormatName

defaults/struct.gtpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
{{- if $hasUndocumented }}
6262
|===
6363

64-
{{- end}}
64+
{{end}}
6565
{{- range .Struct.Fields}}
6666
{{- if not .AnonymousStruct}}
6767
{{- if or .Exported $.Config.Private }}
@@ -73,5 +73,5 @@
7373
{{- end}}
7474
{{- end}}
7575
{{- end}}
76-
{{range .Struct.Fields}}{{if or .Exported $.Config.Private }}{{if .AnonymousStruct}}{{render $ .AnonymousStruct}}{{end}}{{end}}{{end}}
76+
{{- /* Anonymous structs are rendered inline in the parent struct, not as separate sections */ -}}
7777
{{if hasReceivers . .Struct.Name}}{{renderReceivers . .Struct.Name}}{{end}}

main.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,34 @@ var templateCustomTypeDefintion string
6969
var templateCustomTypeDefinitions string
7070

7171
type args struct {
72-
Out string `arg:"-o" help:"The out filepath to write the generated document, default module path, file docs.adoc" placeholder:"PATH"`
73-
StdOut bool ` help:"If output the generated asciidoc to stdout instead of file"`
74-
Debug bool `arg:"--debug" help:"Outputs debug statements to stdout during processing"`
75-
Module string `arg:"-m" help:"an optional folder or file path to module, otherwise current directory" placeholder:"PATH"`
76-
Internal bool `arg:"-i" help:"If internal go code shall be rendered as well"`
77-
Private bool `arg:"-p" help:"If files beneath directories starting with an underscore shall be included"`
78-
NonExported bool ` help:"Renders Non exported as well as the exported. Default only Exported is rendered."`
79-
Test bool `arg:"-t" help:"If test code should be included"`
80-
NoIndex bool `arg:"-n" help:"If no index header shall be generated"`
81-
NoToc bool ` help:"Removes the table of contents if index document"`
82-
IndexConfig string `arg:"-c" help:"JSON document to override the IndexConfig" placeholder:"JSON"`
83-
Overrides []string `arg:"-r,separate" help:"name=template filepath to override default templates"`
84-
Paths []string `arg:"positional" help:"Directory or files to be included in scan (if none, current path is used)" placeholder:"PATH"`
85-
ListTemplates bool `arg:"--list-template" help:"Lists all default templates in the binary"`
86-
OutputTemplate string `arg:"--out-template" help:"outputs a template to stdout"`
87-
PackageDoc []string `arg:"-d,separate" help:"set relative package search filepaths for package documentation" placeholder:"FILEPATH"`
88-
TemplateDir string ` help:"Loads template files *.gtpl from a directory, use --list to get valid names of templates"`
89-
TypeLinks string `arg:"--type-links" help:"Controls type reference linking: disabled, internal, or external (default disabled)"`
90-
Concatenation string `arg:"--concatenation" help:"Controls doc comment concatenation: none or full (default none)" default:"none"`
91-
Highlighter string `arg:"--highlighter" help:"Source code highlighter to use; available: highlightjs, goasciidoc (custom highlightjs)" default:"highlightjs"`
92-
Render []string `arg:"--render,separate" help:"Controls what examples to render for structs: struct-json, struct-yaml (can specify multiple)"`
93-
BuildTag []string `arg:"--build-tag,separate" help:"Build tags to include when parsing (can specify multiple, e.g., --build-tag=integration --build-tag=dev)" placeholder:"TAG"`
94-
AllBuildTags bool `arg:"--all-build-tags" help:"Auto-discover and include all build tags found in source files"`
95-
IgnoreMarkdownHeadings bool `arg:"--ignore-markdown-headings" help:"Replace markdown headings (#, ##, etc.) in comments with their text content"`
72+
Out string `arg:"-o" help:"The out filepath to write the generated document, default module path, file docs.adoc" placeholder:"PATH"`
73+
StdOut bool ` help:"If output the generated asciidoc to stdout instead of file"`
74+
Debug bool `arg:"--debug" help:"Outputs debug statements to stdout during processing"`
75+
Module string `arg:"-m" help:"an optional folder or file path to module, otherwise current directory" placeholder:"PATH"`
76+
Internal bool `arg:"-i" help:"If internal go code shall be rendered as well"`
77+
Private bool `arg:"-p" help:"If files beneath directories starting with an underscore shall be included"`
78+
NonExported bool ` help:"Renders Non exported as well as the exported. Default only Exported is rendered."`
79+
Test bool `arg:"-t" help:"If test code should be included"`
80+
NoIndex bool `arg:"-n" help:"If no index header shall be generated"`
81+
NoToc bool ` help:"Removes the table of contents if index document"`
82+
IndexConfig string `arg:"-c" help:"JSON document to override the IndexConfig" placeholder:"JSON"`
83+
Overrides []string `arg:"-r,separate" help:"name=template filepath to override default templates"`
84+
Paths []string `arg:"positional" help:"Directory or files to be included in scan (if none, current path is used)" placeholder:"PATH"`
85+
ListTemplates bool `arg:"--list-template" help:"Lists all default templates in the binary"`
86+
OutputTemplate string `arg:"--out-template" help:"outputs a template to stdout"`
87+
PackageDoc []string `arg:"-d,separate" help:"set relative package search filepaths for package documentation" placeholder:"FILEPATH"`
88+
TemplateDir string ` help:"Loads template files *.gtpl from a directory, use --list to get valid names of templates"`
89+
TypeLinks string `arg:"--type-links" help:"Controls type reference linking: disabled, internal, or external (default disabled)"`
90+
Concatenation string `arg:"--concatenation" help:"Controls doc comment concatenation: none or full (default none)" default:"none"`
91+
Highlighter string `arg:"--highlighter" help:"Source code highlighter to use; available: highlightjs, goasciidoc (custom highlightjs)" default:"highlightjs"`
92+
Render []string `arg:"--render,separate" help:"Controls what examples to render for structs: struct-json, struct-yaml (can specify multiple)"`
93+
BuildTag []string `arg:"--build-tag,separate" help:"Build tags to include when parsing (can specify multiple, e.g., --build-tag=integration --build-tag=dev)" placeholder:"TAG"`
94+
AllBuildTags bool `arg:"--all-build-tags" help:"Auto-discover and include all build tags found in source files"`
95+
IgnoreMarkdownHeadings bool `arg:"--ignore-markdown-headings" help:"Replace markdown headings (#, ##, etc.) in comments with their text content"`
9696
}
9797

9898
func (args) Version() string {
99-
return "goasciidoc v0.5.3"
99+
return "goasciidoc v0.5.5"
100100
}
101101

102102
func main() {

0 commit comments

Comments
 (0)