Skip to content

Commit

Permalink
Reduce regex usage in annotation parser
Browse files Browse the repository at this point in the history
  • Loading branch information
treuherz committed Jun 18, 2024
1 parent e823900 commit 41431d8
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions util/buildflags/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,44 @@ func ParseExports(inp []string) ([]*controllerapi.ExportEntry, error) {

func ParseAnnotations(inp []string) (map[exptypes.AnnotationKey]string, error) {
// TODO: use buildkit's annotation parser once it supports setting custom prefix and ":" separator
annotationRegexp := regexp.MustCompile(`^((?:[a-z-]+(?:\[[A-Za-z0-9_/-]+\])?)(?:,[a-z-]+(?:\[[A-Za-z0-9_/-]+\])?)*:)?(\S+)$`)

// type followed by optional platform specifier in square brackets
annotationTypeRegexp := regexp.MustCompile(`^([a-z-]+)(?:\[([A-Za-z0-9_/-]+)\])?$`)

annotations := make(map[exptypes.AnnotationKey]string)
for _, inp := range inp {
k, v, ok := strings.Cut(inp, "=")
if !ok {
return nil, errors.Errorf("invalid annotation %q, expected key=value", inp)
}

groups := annotationRegexp.FindStringSubmatch(k)
if groups == nil {
return nil, errors.Errorf("invalid annotation format, expected <type>:<key>=<value>, got %q", inp)
}

types, key := groups[1], groups[2]
types, key, ok := strings.Cut(k, ":")
if !ok {
// no types specified, swap Cut outputs
key = types

if types == "" {
ak := exptypes.AnnotationKey{Key: key}
annotations[ak] = v
continue
}

typesSplit := strings.Split(strings.TrimSuffix(types, ":"), ",")
typesSplit := strings.Split(types, ",")
for _, typeAndPlatform := range typesSplit {
groups := annotationTypeRegexp.FindStringSubmatch(typeAndPlatform)
if groups == nil {
return nil, errors.Errorf(
"invalid annotation type %q, expected type and optional platform in square brackets",
typeAndPlatform)
}

typ, platform := groups[1], groups[2]

switch typ {
case "":
case exptypes.AnnotationIndex, exptypes.AnnotationIndexDescriptor, exptypes.AnnotationManifest, exptypes.AnnotationManifestDescriptor:
case exptypes.AnnotationIndex,
exptypes.AnnotationIndexDescriptor,
exptypes.AnnotationManifest,
exptypes.AnnotationManifestDescriptor:
default:
return nil, errors.Errorf("unknown annotation type %q", typ)
}
Expand Down

0 comments on commit 41431d8

Please sign in to comment.