feat(gen): Make Identifier function configurable#1560
Merged
ti-mo merged 1 commit intocilium:mainfrom Sep 24, 2024
Merged
Conversation
a519023 to
eb8aaf3
Compare
ti-mo
reviewed
Sep 11, 2024
Contributor
ti-mo
left a comment
There was a problem hiding this comment.
I don't know where we stand regarding using bpf2go as a library, or if this fits the vision for the project. Deferring to @mejedi and @lmb for that.
@wdullaer Could you provide some examples of unusual output of internal.Identifier? Maybe we could improve the default instead, which could benefit all users.
eb8aaf3 to
48f2ffb
Compare
Contributor
Author
|
We use ebpf programs written in rust, which means the identifiers follow rust convention. Specifically map names are all caps and field names have underscores. We're using this function: // Identifier turns a C style type or field name into an exportable Go equivalent.
func Identifier(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return s
}
capNext := true
prevIsCap := false
return strings.Map(func(v rune) rune {
vIsCap := unicode.IsUpper(v)
if capNext {
if unicode.IsLower(v) {
v = unicode.ToUpper(v)
}
vIsCap = true
} else if prevIsCap && unicode.IsUpper(v) {
v = unicode.ToLower(v)
}
prevIsCap = vIsCap
if unicode.IsLetter(v) {
capNext = false
return v
} else if unicode.IsNumber(v) {
capNext = true
return v
} else {
capNext = v == '_' || unicode.IsSpace(v) || v == '-' || v == '.'
return -1
}
}, s)
}I would be careful with changing the default here, since this might cause churn in clients. |
mejedi
reviewed
Sep 13, 2024
The standard Identifier function used by the generator results in very unidiomatic go identifier names when used with my ebpf object. With this change, the Identifier function becomes configurable, allowing me to pass in something that produces better outputs in my context. If no function is passed in, we keep using the default, this makes the change entirely backwards compatible. Furthermore, a test has been added to ensure the configuration is correctly passed through. Signed-off-by: Wouter Dullaert <[email protected]>
48f2ffb to
74774ce
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The standard Identifier function used by the generator results in very unidiomatic go identifier names when used with my ebpf object. With this change, the Identifier function becomes configurable, allowing me to pass in something that produces better outputs in my context. If no function is passed in, we keep using the default, this makes the change entirely backwards compatible.
Furthermore, a test has been added to ensure the configuration is correctly passed through.