github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/selection/selection.go (about) 1 package selection 2 3 import ( 4 "errors" 5 ) 6 7 // EnsureValid verifies that a Selection is valid. 8 func (s *Selection) EnsureValid() error { 9 // A nil selection is not valid. 10 if s == nil { 11 return errors.New("nil selection") 12 } 13 14 // Count the number of selection mechanisms present. 15 var mechanismsPresent uint 16 if s.All { 17 mechanismsPresent++ 18 } 19 if len(s.Specifications) > 0 { 20 mechanismsPresent++ 21 } 22 if s.LabelSelector != "" { 23 mechanismsPresent++ 24 } 25 26 // Enforce that exactly one selection mechanism is present. 27 if mechanismsPresent > 1 { 28 return errors.New("multiple selection mechanisms present") 29 } else if mechanismsPresent < 1 { 30 return errors.New("no selection mechanisms present") 31 } 32 33 // Enforce that specifications are non-empty. 34 for _, specification := range s.Specifications { 35 if specification == "" { 36 return errors.New("empty specification") 37 } 38 } 39 40 // We avoid validating the label selector, if present, because it doesn't 41 // pose a risk to parse unvalidated and it would only be possible to 42 // validate by parsing, so we'll catch any format errors later. 43 44 // Success. 45 return nil 46 }