@@ -10,14 +10,14 @@ import (
10
10
)
11
11
12
12
type AutometricsLinkCommentGenerator interface {
13
- GenerateAutometricsComment (funcName string ) []string
13
+ GenerateAutometricsComment (funcName , moduleName string ) []string
14
14
}
15
15
16
16
// TransformFile takes a file path and generates the documentation
17
17
// for the `//autometrics:doc` functions.
18
18
//
19
- // It also replaces the file in place
20
- func TransformFile (path string , generator AutometricsLinkCommentGenerator ) error {
19
+ // It also replaces the file in place.
20
+ func TransformFile (path , moduleName string , generator AutometricsLinkCommentGenerator ) error {
21
21
cwd , err := os .Getwd ()
22
22
if err != nil {
23
23
return fmt .Errorf ("error getting a working directory: %w" , err )
@@ -27,6 +27,7 @@ func TransformFile(path string, generator AutometricsLinkCommentGenerator) error
27
27
if err != nil {
28
28
return fmt .Errorf ("error reading file information from %s: %w" , path , err )
29
29
}
30
+
30
31
permissions := info .Mode ()
31
32
32
33
sourceBytes , err := os .ReadFile (path )
@@ -40,7 +41,8 @@ func TransformFile(path string, generator AutometricsLinkCommentGenerator) error
40
41
}
41
42
42
43
sourceCode := string (sourceBytes )
43
- transformedSource , err := GenerateDocumentation (sourceCode , generator )
44
+
45
+ transformedSource , err := GenerateDocumentation (sourceCode , moduleName , generator )
44
46
if err != nil {
45
47
return fmt .Errorf ("error generating documentation: %w" , err )
46
48
}
@@ -57,7 +59,7 @@ func TransformFile(path string, generator AutometricsLinkCommentGenerator) error
57
59
// the documentation for the `//autometrics:doc` functions.
58
60
//
59
61
// It returns the new source code with augmented documentation.
60
- func GenerateDocumentation (sourceCode string , generator AutometricsLinkCommentGenerator ) (string , error ) {
62
+ func GenerateDocumentation (sourceCode , moduleName string , generator AutometricsLinkCommentGenerator ) (string , error ) {
61
63
fileTree , err := decorator .Parse (sourceCode )
62
64
if err != nil {
63
65
return "" , fmt .Errorf ("error parsing source code: %w" , err )
@@ -83,7 +85,7 @@ func GenerateDocumentation(sourceCode string, generator AutometricsLinkCommentGe
83
85
// Insert new autometrics comment
84
86
listIndex := hasAutometricsDocDirective (docComments )
85
87
if listIndex >= 0 {
86
- autometricsComment := generateAutometricsComment (x .Name .Name , generator )
88
+ autometricsComment := generateAutometricsComment (x .Name .Name , moduleName , generator )
87
89
x .Decorations ().Start .Replace (insertComments (docComments , listIndex , autometricsComment )... )
88
90
}
89
91
}
@@ -131,17 +133,18 @@ func autometricsDocEndDirective(commentGroup []string) int {
131
133
return - 1
132
134
}
133
135
134
- func generateAutometricsComment (funcName string , generator AutometricsLinkCommentGenerator ) []string {
136
+ func generateAutometricsComment (funcName , moduleName string , generator AutometricsLinkCommentGenerator ) []string {
135
137
var ret []string
136
138
ret = append (ret , "//" )
137
139
ret = append (ret , "// autometrics:doc-start DO NOT EDIT" )
138
140
ret = append (ret , "//" )
139
141
ret = append (ret , "// # Autometrics" )
140
142
ret = append (ret , "//" )
141
- ret = append (ret , generator .GenerateAutometricsComment (funcName )... )
143
+ ret = append (ret , generator .GenerateAutometricsComment (funcName , moduleName )... )
142
144
ret = append (ret , "//" )
143
145
ret = append (ret , "// autometrics:doc-end DO NOT EDIT" )
144
146
ret = append (ret , "//" )
147
+
145
148
return ret
146
149
}
147
150
@@ -161,3 +164,28 @@ func insertComments(inputArray []string, index int, values []string) []string {
161
164
162
165
return inputArray
163
166
}
167
+
168
+ // errorReturnValueName returns the name of the error return value if it exists.
169
+ func errorReturnValueName (funcNode * dst.FuncDecl ) (string , error ) {
170
+ returnValues := funcNode .Type .Results
171
+ if returnValues == nil || returnValues .List == nil {
172
+ return "" , nil
173
+ }
174
+
175
+ for _ , field := range returnValues .List {
176
+ fieldType := field .Type
177
+ if spec , ok := fieldType .(* dst.Ident ); ok {
178
+ if spec .Name == "error" {
179
+ // Assuming that the `error` type has 0 or 1 name before it.
180
+ if field .Names == nil {
181
+ return "" , nil
182
+ } else if len (field .Names ) > 1 {
183
+ return "" , fmt .Errorf ("expecting a single named `error` return value, got %d instead." , len (field .Names ))
184
+ }
185
+ return field .Names [0 ].Name , nil
186
+ }
187
+ }
188
+ }
189
+
190
+ return "" , nil
191
+ }
0 commit comments