github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/intent.go (about)

     1  package protoc
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  )
     7  
     8  // intent represents an action for an attribute name or "value" optionally
     9  // prefixed by a '+' or '-'.  If the prefix is missing, the intent is not
    10  // negative.
    11  type intent struct {
    12  	Value string
    13  	Want  bool
    14  }
    15  
    16  func parseIntent(value string) *intent {
    17  	value = strings.TrimSpace(value)
    18  	negative := strings.HasPrefix(value, "-")
    19  	positive := strings.HasPrefix(value, "+")
    20  	if negative || positive {
    21  		value = value[1:]
    22  	}
    23  	return &intent{Value: value, Want: !negative}
    24  }
    25  
    26  // ForIntent reduces a dict to a list by the given intent.
    27  func ForIntent(in map[string]bool, want bool) []string {
    28  	vals := make([]string, 0)
    29  	for val, intent := range in {
    30  		if intent != want {
    31  			continue
    32  		}
    33  		vals = append(vals, val)
    34  	}
    35  	sort.Strings(vals)
    36  	return vals
    37  }