Chromium Code Reviews| Index: go/src/infra/monorail/validation.go |
| diff --git a/go/src/infra/monorail/validation.go b/go/src/infra/monorail/validation.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..645599f0972b0589eff662b4c41e916f808edebc |
| --- /dev/null |
| +++ b/go/src/infra/monorail/validation.go |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package monorail |
| + |
| +import "fmt" |
| + |
| +// Validate checks the message for errors. |
| +func (i *IssueRef) Validate() error { |
|
seanmccullough1
2016/06/03 18:45:33
I see. This could be annoying to do without adding
nodir
2016/06/03 18:56:12
yeah, we use this approach a lot in luci-go.
|
| + if i == nil { |
| + return fmt.Errorf("is nil") |
| + } |
| + if i.ProjectId == "" { |
| + return fmt.Errorf("no projectId") |
| + } |
| + if i.IssueId == 0 { |
| + return fmt.Errorf("no issueId") |
| + } |
| + return nil |
| +} |
| + |
| +// Validate checks the message for errors. |
| +func (a *AtomPerson) Validate() error { |
| + if a == nil { |
| + return fmt.Errorf("is nil") |
| + } |
| + if a.Name == "" { |
| + return fmt.Errorf("no name") |
| + } |
| + return nil |
| +} |
| + |
| +// Validate checks the message for errors. |
| +func (i *Issue) Validate() error { |
| + if i == nil { |
| + return fmt.Errorf("is nil") |
| + } |
| + if i.Status == "" { |
| + return fmt.Errorf("no status") |
| + } |
| + for _, ref := range i.BlockedOn { |
| + if err := ref.Validate(); err != nil { |
| + return fmt.Errorf("blockedOn: %s", err) |
| + } |
| + } |
| + |
| + seen := map[string]struct{}{} |
| + for _, cc := range i.Cc { |
| + if err := cc.Validate(); err != nil { |
| + return fmt.Errorf("cc: %s", err) |
| + } |
| + // Monorail does not like duplicates in CC list. |
| + if _, saw := seen[cc.Name]; saw { |
| + return fmt.Errorf("cc: duplicate %s", cc.Name) |
| + } |
| + seen[cc.Name] = struct{}{} |
| + } |
| + |
| + if i.Owner != nil { |
| + if err := i.Owner.Validate(); err != nil { |
| + return err |
| + } |
| + } |
| + |
| + return nil |
| +} |
| + |
| +// Validate checks the message for errors. |
| +func (i *InsertIssueRequest) Validate() error { |
| + if i.ProjectId == "" { |
| + return fmt.Errorf("no projectId") |
| + } |
| + if err := i.Issue.Validate(); err != nil { |
| + return fmt.Errorf("issue: %s", err) |
| + } |
| + if i.Issue.Id != 0 { |
| + return fmt.Errorf("issue: must not have id") |
| + } |
| + return nil |
| +} |