Skip to content

Commit 291b784

Browse files
authored
Split long doc strings over multiple lines (#249)
The OpenAPI spec concatenates some multi-line doc comments in the original Rust code into a single long line. We write these long lines verbatim into the Go source we generate, and some are sufficiently long that viewing the full comment requires [scrolling sideways on pkg.go.dev](https://pkg.go.dev/github.com/oxidecomputer/oxide.go@v0.1.0-beta9/oxide#InternetGatewayCreate). Add a helper function to split long doc strings into lines of roughly 100 characters and apply it to the `Description` of our generated paths and types. Closes #248
1 parent 6bce2f6 commit 291b784

File tree

6 files changed

+616
-239
lines changed

6 files changed

+616
-239
lines changed

internal/generate/paths.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func buildMethod(f *os.File, spec *openapi3.T, method string, path string, o *op
175175
sanitisedDescription := strings.ReplaceAll(o.Description, "\n", "\n// ")
176176

177177
config := methodTemplate{
178-
Description: sanitisedDescription,
178+
Description: splitDocString(sanitisedDescription),
179179
HTTPMethod: method,
180180
FunctionName: methodName,
181181
WrappedFunction: ogmethodName,
@@ -278,6 +278,33 @@ func cleanPath(path string) string {
278278
return strings.Replace(path, "}", "}}", -1)
279279
}
280280

281+
// splitDocString inserts newlines into doc comments at approximately 100 character intervals.
282+
func splitDocString(s string) string {
283+
var (
284+
out strings.Builder
285+
written int
286+
)
287+
288+
for _, subStr := range strings.SplitAfter(s, " ") {
289+
if written > 100 {
290+
// Remove trailing space if inserting a newline.
291+
out.WriteString(strings.TrimSuffix(subStr, " "))
292+
out.WriteString("\n// ")
293+
written = 0
294+
295+
continue
296+
}
297+
298+
ct, _ := out.WriteString(subStr)
299+
written += ct
300+
301+
if strings.Contains(subStr, "\n") {
302+
written = 0
303+
}
304+
}
305+
return out.String()
306+
}
307+
281308
func writeTpl(f *os.File, config methodTemplate) error {
282309
var t *template.Template
283310
var err error

internal/generate/test_utils/types_output

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ type DiskCreate struct {
1111
DiskSource DiskSource `json:"disk_source,omitempty" yaml:"disk_source,omitempty"`
1212
}
1313

14-
// DiskIdentifier is parameters for the [`Disk`](omicron_common::api::external::Disk) to be attached or detached to an instance
14+
// DiskIdentifier is parameters for the [`Disk`](omicron_common::api::external::Disk) to be attached or
15+
// detached to an instance
1516
type DiskIdentifier struct {
1617
Name Name `json:"name,omitempty" yaml:"name,omitempty"`
1718
}

internal/generate/test_utils/types_output_expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ type DiskCreate struct {
1111
DiskSource DiskSource `json:"disk_source,omitempty" yaml:"disk_source,omitempty"`
1212
}
1313

14-
// DiskIdentifier is parameters for the [`Disk`](omicron_common::api::external::Disk) to be attached or detached to an instance
14+
// DiskIdentifier is parameters for the [`Disk`](omicron_common::api::external::Disk) to be attached or
15+
// detached to an instance
1516
type DiskIdentifier struct {
1617
Name Name `json:"name,omitempty" yaml:"name,omitempty"`
1718
}

internal/generate/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,14 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
348348
continue
349349
}
350350

351-
fmt.Fprintf(f, "%s\n", tt.Description)
351+
fmt.Fprintf(f, "%s\n", splitDocString(tt.Description))
352352
fmt.Fprintf(f, "type %s %s", tt.Name, tt.Type)
353353
if tt.Fields != nil {
354354
fmt.Fprint(f, " {\n")
355355
for _, ft := range tt.Fields {
356356
if ft.Description != "" {
357357
// TODO: Double check about the "//"
358-
fmt.Fprintf(f, "\t%s\n", ft.Description)
358+
fmt.Fprintf(f, "\t%s\n", splitDocString(ft.Description))
359359
}
360360
fmt.Fprintf(f, "\t%s %s %s\n", ft.Name, ft.Type, ft.SerializationInfo)
361361
}
@@ -398,7 +398,7 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
398398
continue
399399
}
400400

401-
fmt.Fprintf(f, "%s\n", et.Description)
401+
fmt.Fprintf(f, "%s\n", splitDocString(et.Description))
402402
fmt.Fprintf(f, "%s %s %s\n\n", et.ValueType, et.Name, et.Value)
403403
}
404404
}

oxide/paths.go

Lines changed: 35 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)