Skip to content

Commit 9687154

Browse files
Generate function name, and description based on input and return values (#957)
* Generate function name, and description based on input and return values
1 parent 94dbb45 commit 9687154

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

test-crawler/collector/collector.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package collector
22

33
import (
4+
"bytes"
5+
"fmt"
46
"os"
57
"path/filepath"
68
"strings"
9+
"unicode"
710

811
a "testsuites/annotations"
912
)
@@ -33,7 +36,7 @@ type FunctionAnnotation struct {
3336
Name string `yaml:"name"`
3437
InputParams string `yaml:"inputParams"`
3538
ReturnValues string `yaml:"returnValues"`
36-
Description string
39+
Description string `yaml:"description"`
3740
Public bool
3841
}
3942

@@ -126,3 +129,33 @@ func ListGoFilesInFolder(root string, ignore []string) (system string, files []s
126129

127130
return "", nil, nil
128131
}
132+
133+
// GenerateMethodName generates name adding underscore and usecase index
134+
// ex: name: ListenProofRequest -> LISTEN_PROOF_REQUEST_001
135+
func GenerateMethodName(funcName string) string {
136+
usecase := "001" // in the future it will be autoincremented depending how many return cases we have.
137+
buf := &bytes.Buffer{}
138+
for i, rune := range funcName {
139+
if unicode.IsUpper(rune) && i > 0 {
140+
buf.WriteRune('_')
141+
}
142+
buf.WriteRune(rune)
143+
}
144+
145+
return fmt.Sprintf("%s_%s", strings.ToUpper(buf.String()), usecase)
146+
}
147+
148+
// GenerateMethodDescription generates description based on inputParams, returnParams
149+
func GenerateMethodDescription(f FunctionAnnotation) string {
150+
var dsc string
151+
if f.Description == "" {
152+
dsc = "Function description not set."
153+
if (f.InputParams != "" && f.InputParams != "()") && f.ReturnValues != "" {
154+
dsc = fmt.Sprintf(`Given a %s, returns %s`, f.InputParams, f.ReturnValues)
155+
} else if (f.InputParams == "" || f.InputParams == "()") && f.ReturnValues != "" {
156+
dsc = fmt.Sprintf(`Returns %s`, f.ReturnValues)
157+
}
158+
}
159+
160+
return dsc
161+
}

test-crawler/crawler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ func TestCrawlSingleFileForFunctions(t *testing.T) {
2121
t.Errorf("got %q, expected %q methods", len(fnsAnn), 2)
2222
}
2323

24-
assert.Equal(t, "HelloEvent", fnsAnn[0].Name)
24+
assert.Equal(t, "HELLO_EVENT_001", fnsAnn[0].Name)
2525
assert.Equal(t, "()", fnsAnn[0].InputParams) // input param
2626
assert.Equal(t, "string", fnsAnn[0].ReturnValues) // return param
2727

28-
assert.Equal(t, "HelloEventWithParameter", fnsAnn[1].Name)
28+
assert.Equal(t, "HELLO_EVENT_WITH_PARAMETER_001", fnsAnn[1].Name)
2929
assert.Equal(t, "(param string)", fnsAnn[1].InputParams)
3030
assert.Equal(t, "(string, error)", fnsAnn[1].ReturnValues)
3131

32-
assert.Equal(t, "FunctionWithoutParameters", fnsAnn[2].Name)
32+
assert.Equal(t, "FUNCTION_WITHOUT_PARAMETERS_001", fnsAnn[2].Name)
3333
assert.Equal(t, "()", fnsAnn[2].InputParams)
3434
assert.Equal(t, "", fnsAnn[2].ReturnValues)
3535

36-
assert.Equal(t, "FunctionWithPointerReturnValue", fnsAnn[3].Name)
36+
assert.Equal(t, "FUNCTION_WITH_POINTER_RETURN_VALUE_001", fnsAnn[3].Name)
3737
assert.Equal(t, "()", fnsAnn[3].InputParams)
3838
assert.Equal(t, "*Event", fnsAnn[3].ReturnValues)
3939
}

test-crawler/extractor/extractor.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,18 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
279279
returnValueType == string(TYPE_IDENTIFIER) {
280280
returnValues = content[child.Child(4).StartByte():child.Child(4).EndByte()]
281281
}
282+
funcAnn := c.FunctionAnnotation{
283+
Name: funcName,
284+
Public: isPublic(funcName),
285+
InputParams: params,
286+
ReturnValues: returnValues,
287+
}
288+
funcAnn.Name = c.GenerateMethodName(funcAnn.Name)
289+
funcAnn.Description = c.GenerateMethodDescription(funcAnn)
290+
282291
funcAnnoPair = append(funcAnnoPair, FunctionAnnotationNode{
283-
Node: child,
284-
Function: c.FunctionAnnotation{
285-
Name: funcName,
286-
Public: isPublic(funcName),
287-
InputParams: params,
288-
ReturnValues: returnValues,
289-
},
292+
Node: child,
293+
Function: funcAnn,
290294
})
291295
}
292296
prevNode = child

0 commit comments

Comments
 (0)