Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (Only valid for those who consume Kubebuilder as a lib) - Allow usage of custom marker names #3993

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions pkg/machinery/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
)

const prefix = "+kubebuilder:scaffold:"
const kbPrefix = "+kubebuilder:scaffold:"

var commentsByExt = map[string]string{
".go": "//",
Expand All @@ -33,16 +33,28 @@ var commentsByExt = map[string]string{

// Marker represents a machine-readable comment that will be used for scaffolding purposes
type Marker struct {
prefix string
comment string
value string
}

// NewMarkerFor creates a new marker customized for the specific file
// Supported file extensions: .go, .yaml, .yml
// NewMarkerFor creates a new marker customized for the specific file. The created marker
// is prefixed with `+kubebuilder:scaffold:` the default prefix for kubebuilder.
// Supported file extensions: .go, .yaml, .yml.
func NewMarkerFor(path string, value string) Marker {
return NewMarkerWithPrefixFor(kbPrefix, path, value)
}

// NewMarkerWithPrefixFor creates a new custom prefixed marker customized for the specific file
// Supported file extensions: .go, .yaml, .yml
func NewMarkerWithPrefixFor(prefix string, path string, value string) Marker {
ext := filepath.Ext(path)
if comment, found := commentsByExt[ext]; found {
return Marker{comment, value}
return Marker{
prefix: markerPrefix(prefix),
comment: comment,
value: value,
}
}

extensions := make([]string, 0, len(commentsByExt))
Expand All @@ -54,13 +66,13 @@ func NewMarkerFor(path string, value string) Marker {

// String implements Stringer
func (m Marker) String() string {
return m.comment + " " + prefix + m.value
return m.comment + " " + m.prefix + m.value
}

// EqualsLine compares a marker with a string representation to check if they are the same marker
func (m Marker) EqualsLine(line string) bool {
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), m.comment))
return line == prefix+m.value
return line == m.prefix+m.value
}

// CodeFragments represents a set of code fragments
Expand All @@ -69,3 +81,17 @@ type CodeFragments []string

// CodeFragmentsMap binds Markers and CodeFragments together
type CodeFragmentsMap map[Marker]CodeFragments

func markerPrefix(prefix string) string {
trimmed := strings.TrimSpace(prefix)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the trim revert the change:

Bug fix to ensure consistent spacing in marker annotations (#3904).

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camilamacedo86 Please, correct my when I am wrong, but what I understand from the issue is, that it requests a constant string representation of the marker. This is still achieved by adding a space between the comment and the prefix:

func (m Marker) String() string {
	return m.comment + " " + m.prefix + m.value
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right 👍

var builder strings.Builder
if !strings.HasPrefix(trimmed, "+") {
builder.WriteString("+")
}
builder.WriteString(trimmed)
if !strings.HasSuffix(trimmed, ":") {
builder.WriteString(":")
}

return builder.String()
}
43 changes: 41 additions & 2 deletions pkg/machinery/marker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,47 @@ var _ = Describe("Marker", func() {
Context("String", func() {
DescribeTable("should return the right string representation",
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
Entry("for go files", Marker{comment: "//", value: "test"}, "// +kubebuilder:scaffold:test"),
Entry("for yaml files", Marker{comment: "#", value: "test"}, "# +kubebuilder:scaffold:test"),
Entry("for go files", Marker{prefix: kbPrefix, comment: "//", value: "test"}, "// +kubebuilder:scaffold:test"),
Entry("for yaml files", Marker{prefix: kbPrefix, comment: "#", value: "test"}, "# +kubebuilder:scaffold:test"),
)
})
})

var _ = Describe("NewMarkerFor", func() {
Context("String", func() {
DescribeTable("should return the right string representation",
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
Entry("for yaml files", NewMarkerFor("test.yaml", "test"), "# +kubebuilder:scaffold:test"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more one test with // right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will add tests for //

)
})
})

var _ = Describe("NewMarkerWithPrefixFor", func() {
Context("String", func() {
DescribeTable("should return the right string representation",
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },

Entry("for yaml files",
NewMarkerWithPrefixFor("custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
Entry("for yaml files",
NewMarkerWithPrefixFor("+custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
Entry("for yaml files",
NewMarkerWithPrefixFor("custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
Entry("for yaml files",
NewMarkerWithPrefixFor("+custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
Entry("for yaml files",
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.yaml", "test"), "# +custom:scaffold:test"),

Entry("for go files",
NewMarkerWithPrefixFor("custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
Entry("for go files",
NewMarkerWithPrefixFor("+custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
Entry("for go files",
NewMarkerWithPrefixFor("custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
Entry("for go files",
NewMarkerWithPrefixFor("+custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
Entry("for go files",
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.go", "test"), "// +custom:scaffold:test"),
)
})
})
Loading