Skip to content

Commit

Permalink
generator-go-sdk: use operation descriptions to output method comments
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Aug 18, 2024
1 parent 91b5eb0 commit 15d19aa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
22 changes: 22 additions & 0 deletions tools/generator-go-sdk/internal/generator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,25 @@ func wordifyString(input string) string {

return strings.TrimPrefix(output, " ")
}

// wrapOnWordBoundary attempts to wrap a string on whitespace to the specified maximum length, optionally
// prefixing each line with the provided prefix (useful for comments).
func wrapOnWordBoundary(in string, maxLength int, prefix string) string {
words := strings.Fields(in)
out := make([]string, 0)
currentLine := prefix

for _, word := range words {
if len(currentLine)+len(word) > maxLength {
out = append(out, currentLine)
currentLine = prefix
}
currentLine = fmt.Sprintf("%s %s", currentLine, word)
}

if currentLine != prefix {
out = append(out, currentLine)
}

return strings.Join(out, "\n")
}
36 changes: 30 additions & 6 deletions tools/generator-go-sdk/internal/generator/templater_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,20 @@ func (c methodsPandoraTemplater) immediateOperationTemplate(data GeneratorData)
return nil, fmt.Errorf("building options struct: %+v", err)
}

comment := c.operationName
if c.operation.Description != "" {
comment += fmt.Sprintf(": %s", c.operation.Description)
} else {
comment += " ..."
}
comment = wrapOnWordBoundary(comment, 120, "//")

templated := fmt.Sprintf(`
%[7]s
%[8]s
%[9]s
// %[2]s ...
%[10]s
func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse, err error) {
opts := %[4]s
Expand All @@ -194,7 +202,7 @@ func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse,
return
}
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *marshalerCode, *unmarshalerCode, *responseStruct, *optionsStruct, requestOptionStruct)
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *marshalerCode, *unmarshalerCode, *responseStruct, *optionsStruct, requestOptionStruct, comment)
return &templated, nil
}

Expand Down Expand Up @@ -226,12 +234,20 @@ func (c methodsPandoraTemplater) longRunningOperationTemplate(data GeneratorData
return nil, fmt.Errorf("building options struct: %+v", err)
}

comment := c.operationName
if c.operation.Description != "" {
comment += fmt.Sprintf(": %s", c.operation.Description)
} else {
comment += " ..."
}
comment = wrapOnWordBoundary(comment, 120, "//")

templated := fmt.Sprintf(`
%[9]s
%[10]s
%[11]s
// %[3]s ...
// %[12]s
func (c %[1]s) %[3]s(ctx context.Context %[4]s) (result %[3]sOperationResponse, err error) {
opts := %[5]s
Expand Down Expand Up @@ -275,7 +291,7 @@ func (c %[1]s) %[3]sThenPoll(ctx context.Context %[4]s) error {
return nil
}
`, data.serviceClientName, data.baseClientPackage, c.operationName, *methodArguments, *requestOptions, *marshalerCode, *unmarshalerCode, argumentsCode, *responseStruct, *optionsStruct, requestOptionStruct)
`, data.serviceClientName, data.baseClientPackage, c.operationName, *methodArguments, *requestOptions, *marshalerCode, *unmarshalerCode, argumentsCode, *responseStruct, *optionsStruct, requestOptionStruct, comment)
return &templated, nil
}

Expand Down Expand Up @@ -313,12 +329,20 @@ func (c methodsPandoraTemplater) listOperationTemplate(data GeneratorData) (*str
predicateName = fmt.Sprintf("%s%s", *typeName, predicateName)
}

comment := c.operationName
if c.operation.Description != "" {
comment += fmt.Sprintf(": %s", c.operation.Description)
} else {
comment += " ..."
}
comment = wrapOnWordBoundary(comment, 120, "//")

templated := fmt.Sprintf(`
%[6]s
%[7]s
%[8]s
// %[2]s ...
// %[9]s
func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse, err error) {
opts := %[4]s
Expand All @@ -341,7 +365,7 @@ func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse,
return
}
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *unmarshalerCode, *responseStruct, *optionsStruct, requestOptionStruct)
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *unmarshalerCode, *responseStruct, *optionsStruct, requestOptionStruct, comment)

// Only output predicate functions for models and not for base types like string, int etc.
if c.operation.ResponseObject.Type == models.ReferenceSDKObjectDefinitionType || c.operation.ResponseObject.Type == models.ListSDKObjectDefinitionType {
Expand Down

0 comments on commit 15d19aa

Please sign in to comment.