Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: logdog/common/types/streamaddr.go

Issue 2899953002: [logdog/common/types] add Valid method to StreamAddr. (Closed)
Patch Set: add IsZero Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The LUCI Authors. All rights reserved. 1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package types 5 package types
6 6
7 import ( 7 import (
8 "flag" 8 "flag"
9 "net/url" 9 "net/url"
10 "strings" 10 "strings"
(...skipping 21 matching lines...) Expand all
32 // Set implements flag.Value 32 // Set implements flag.Value
33 func (s *StreamAddr) Set(v string) error { 33 func (s *StreamAddr) Set(v string) error {
34 a, err := ParseURL(v) 34 a, err := ParseURL(v)
35 if err != nil { 35 if err != nil {
36 return err 36 return err
37 } 37 }
38 *s = *a 38 *s = *a
39 return nil 39 return nil
40 } 40 }
41 41
42 // Validate returns an error if the supplied StreamAddr is not valid.
43 func (s *StreamAddr) Validate() error {
44 if s.Host == "" {
45 return errors.New("cannot have empty Host")
46 }
47 if err := s.Project.Validate(); err != nil {
48 return err
49 }
50 if err := s.Path.Validate(); err != nil {
51 return err
52 }
53 return nil
54 }
55
56 // IsZero returns true iff all fields are empty.
57 func (s *StreamAddr) IsZero() bool {
58 return s.Host == "" && s.Path == "" && s.Project == ""
59 }
60
42 // String returns a string representation of this address. 61 // String returns a string representation of this address.
43 func (s *StreamAddr) String() string { return s.URL().String() } 62 func (s *StreamAddr) String() string { return s.URL().String() }
44 63
45 // URL returns a LogDog URL that represents this Stream. 64 // URL returns a LogDog URL that represents this Stream.
46 func (s *StreamAddr) URL() *url.URL { 65 func (s *StreamAddr) URL() *url.URL {
47 return &url.URL{ 66 return &url.URL{
48 Scheme: logDogURLScheme, 67 Scheme: logDogURLScheme,
49 Host: s.Host, 68 Host: s.Host,
50 Path: strings.Join([]string{"", string(s.Project), string(s.Pa th)}, "/"), 69 Path: strings.Join([]string{"", string(s.Project), string(s.Pa th)}, "/"),
51 } 70 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 } 106 }
88 107
89 if err := addr.Path.Validate(); err != nil { 108 if err := addr.Path.Validate(); err != nil {
90 return nil, errors.Annotate(err).Reason("invalid stream path: %( path)q"). 109 return nil, errors.Annotate(err).Reason("invalid stream path: %( path)q").
91 D("path", addr.Path). 110 D("path", addr.Path).
92 Err() 111 Err()
93 } 112 }
94 113
95 return &addr, nil 114 return &addr, nil
96 } 115 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698