github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/forwarding/session.go (about) 1 package forwarding 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/mutagen-io/mutagen/pkg/identifier" 8 "github.com/mutagen-io/mutagen/pkg/selection" 9 "github.com/mutagen-io/mutagen/pkg/url" 10 ) 11 12 // EnsureValid ensures that Session's invariants are respected. 13 func (s *Session) EnsureValid() error { 14 // A nil session is not valid. 15 if s == nil { 16 return errors.New("nil session") 17 } 18 19 // Ensure that the session identifier is valid. 20 if !identifier.IsValid(s.Identifier) { 21 return errors.New("invalid session identifier") 22 } 23 24 // Ensure that the session version is supported. 25 if !s.Version.Supported() { 26 return errors.New("unknown or unsupported session version") 27 } 28 29 // Ensure that the creation time is present. 30 if s.CreationTime == nil { 31 return errors.New("missing creation time") 32 } 33 34 // Ensure that the source URL is valid and is a forwarding URL. 35 if err := s.Source.EnsureValid(); err != nil { 36 return fmt.Errorf("invalid source URL: %w", err) 37 } else if s.Source.Kind != url.Kind_Forwarding { 38 return errors.New("source URL is not a forwarding URL") 39 } 40 41 // Ensure that the destination URL is valid and is a forwarding URL. 42 if err := s.Destination.EnsureValid(); err != nil { 43 return fmt.Errorf("invalid destination URL: %w", err) 44 } else if s.Destination.Kind != url.Kind_Forwarding { 45 return errors.New("destination URL is not a forwarding URL") 46 } 47 48 // Ensure that the configuration is valid. 49 if err := s.Configuration.EnsureValid(false); err != nil { 50 return fmt.Errorf("invalid configuration: %w", err) 51 } 52 53 // Ensure that the source-specific configuration is valid. 54 if err := s.ConfigurationSource.EnsureValid(true); err != nil { 55 return fmt.Errorf("invalid source-specific configuration: %w", err) 56 } 57 58 // Ensure that the destination-specific configuration is valid. 59 if err := s.ConfigurationDestination.EnsureValid(true); err != nil { 60 return fmt.Errorf("invalid destination-specific configuration: %w", err) 61 } 62 63 // Validate the session name. 64 if err := selection.EnsureNameValid(s.Name); err != nil { 65 return fmt.Errorf("invalid session name: %w", err) 66 } 67 68 // Ensure that labels are valid. 69 for k, v := range s.Labels { 70 if err := selection.EnsureLabelKeyValid(k); err != nil { 71 return fmt.Errorf("invalid label key: %w", err) 72 } else if err = selection.EnsureLabelValueValid(v); err != nil { 73 return fmt.Errorf("invalid label value: %w", err) 74 } 75 } 76 77 // Success. 78 return nil 79 }