-
Notifications
You must be signed in to change notification settings - Fork 55
feat: hints on slices #263
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
base: main
Are you sure you want to change the base?
Changes from all commits
91126a2
8e16979
5a14e13
35173f8
92ffeb7
3b29f36
aec7d72
93131d9
56d7085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "slices" | ||
| "strings" | ||
| "time" | ||
| "unicode" | ||
|
|
||
| "golang.org/x/crypto/openpgp/packet" | ||
| "gopkg.in/yaml.v3" | ||
|
|
@@ -146,6 +147,7 @@ func (ym yamlMode) MarshalYAML() (any, error) { | |
| var _ yaml.Marshaler = yamlMode(0) | ||
|
|
||
| type yamlSlice struct { | ||
| Hint string `yaml:"hint,omitempty"` | ||
| Essential []string `yaml:"essential,omitempty"` | ||
| Contents map[string]*yamlPath `yaml:"contents,omitempty"` | ||
| Mutate string `yaml:"mutate,omitempty"` | ||
|
|
@@ -407,11 +409,18 @@ func parsePackage(baseDir, pkgName, pkgPath string, data []byte) (*Package, erro | |
| for sliceName, yamlSlice := range yamlPkg.Slices { | ||
| match := apacheutil.SnameExp.FindStringSubmatch(sliceName) | ||
| if match == nil { | ||
| return nil, fmt.Errorf("invalid slice name %q in %s (start with a-z, len >= 3, only a-z / 0-9 / -)", sliceName, pkgPath) | ||
| return nil, fmt.Errorf("invalid slice name in %s (start with a-z, len >= 3, only a-z / 0-9 / -): %q", pkgPath, sliceName) | ||
| } | ||
| hintNotPrintable := strings.ContainsFunc(yamlSlice.Hint, func(r rune) bool { | ||
| return !unicode.IsPrint(r) | ||
| }) | ||
| if len(yamlSlice.Hint) > 40 || hintNotPrintable { | ||
| return nil, fmt.Errorf("invalid slice hint for %q in %s (len <= 40, only printable characters and standard space): %q", sliceName, pkgPath, yamlSlice.Hint) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Note to reviewers]: The way to list the rules here is inconsistent with the other error message for slice names. I tried to express it the same way but it led to something long and hard to read. So I propose to compromise a bit on consistency to gain in clarity.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a balance here to strike. We want to be precise but we do not want to create an overly complex error message that will confuse the user. If you look at what we do for slice names, the message is not 100% coherent with the regex but it is enough for the user to know how to write a good slice name. We can do a similar thing here, I think "printable characters" is too technical as it refers to a concrete subset of unicode. Also, my assumption is that no user is going to use non-printable characters accidentally. So I think we can simplify the message to what you had before,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This, alone, looks misleading to me. We are not only allowing spaces. What about |
||
| } | ||
| slice := &Slice{ | ||
| Package: pkgName, | ||
| Name: sliceName, | ||
| Hint: yamlSlice.Hint, | ||
| Scripts: SliceScripts{ | ||
| Mutate: yamlSlice.Mutate, | ||
| }, | ||
|
|
@@ -631,6 +640,7 @@ func pathInfoToYAML(pi *PathInfo) (*yamlPath, error) { | |
| // sliceToYAML converts a Slice object to a yamlSlice object. | ||
| func sliceToYAML(s *Slice) (*yamlSlice, error) { | ||
| slice := &yamlSlice{ | ||
| Hint: s.Hint, | ||
| Contents: make(map[string]*yamlPath, len(s.Contents)), | ||
| Mutate: s.Scripts.Mutate, | ||
| V3Essential: make(map[string]*yamlEssential, len(s.Essential)), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Note to reviewers]: This test might be a bit redundant but I wanted to test a non-standard space other than a line break.