Skip to content

Commit

Permalink
Update TruncateDescription
Browse files Browse the repository at this point in the history
- Implement word boundary handling
- Add ellipsis

Signed-off-by: Dale Haiducek <[email protected]>
  • Loading branch information
dhaiducek committed Jul 15, 2024
1 parent 7483ddc commit 9fca0b1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
22 changes: 17 additions & 5 deletions pkg/crd/desc_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TruncateDescription(schema *apiext.JSONSchemaProps, maxLen int) {
// descVisitor recursively visits all fields in the schema and truncates the
// description of the fields to specified maxLen.
type descVisitor struct {
// maxLen is the maximum allowed length for decription of a field
// maxLen is the maximum allowed length for description of a field
maxLen int
}

Expand All @@ -60,19 +60,31 @@ func (v descVisitor) Visit(schema *apiext.JSONSchemaProps) SchemaVisitor {
// exceeds maxLen because it tries to chop off the desc at the closest sentence
// boundary to avoid incomplete sentences.
func truncateString(desc string, maxLen int) string {
if len(desc) <= maxLen {
return desc
}

desc = desc[0:maxLen]

// Trying to chop off at closest sentence boundary.
if n := strings.LastIndexFunc(desc, isSentenceTerminal); n > 0 {
return desc[0 : n+1]
}
// TODO(droot): Improve the logic to chop off at closest word boundary
// or add elipses (...) to indicate that it's chopped incase no closest
// sentence found within maxLen.
return desc

// Trying to chop off at closest word boundary (i.e. whitespace).
if n := strings.LastIndexFunc(desc, isWhiteSpace); n > 0 {
return desc[0 : n] + "..."
}

return desc[0:maxLen] + "..."
}

// helper function to determine if given rune is a sentence terminal or not.
func isSentenceTerminal(r rune) bool {
return unicode.Is(unicode.STerm, r)
}

// helper function to determine if given rune is whitespace or not.
func isWhiteSpace(r rune) bool {
return unicode.Is(unicode.White_Space, r)
}
2 changes: 1 addition & 1 deletion pkg/crd/desc_visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("TruncateDescription", func() {
}
crd.TruncateDescription(schema, len(schema.Description)-2)
Expect(schema).To(Equal(&apiext.JSONSchemaProps{
Description: `This is top level description of the root obje`,
Description: `This is top level description of the root...`,
}))
})
})
2 changes: 1 addition & 1 deletion pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type Generator struct {
// This value can only be specified for CustomResourceDefinitions that were created with
// `apiextensions.k8s.io/v1beta1`.
//
// The field can be set for compatiblity reasons, although strongly discouraged, resource
// The field can be set for compatibility reasons, although strongly discouraged, resource
// authors should move to a structural OpenAPI schema instead.
//
// See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning
Expand Down

0 comments on commit 9fca0b1

Please sign in to comment.