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

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

Issue 2699943004: LogDog: Implement StreamName.Split. (Closed)
Patch Set: Created 3 years, 10 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 | logdog/common/types/streamname_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 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 "encoding/json" 8 "encoding/json"
9 "errors" 9 "errors"
10 "fmt" 10 "fmt"
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 return nil 226 return nil
227 } 227 }
228 return strings.Split(string(s), string(StreamNameSep)) 228 return strings.Split(string(s), string(StreamNameSep))
229 } 229 }
230 230
231 // SegmentCount returns the total number of segments in the StreamName. 231 // SegmentCount returns the total number of segments in the StreamName.
232 func (s StreamName) SegmentCount() int { 232 func (s StreamName) SegmentCount() int {
233 return segmentCount(string(s)) 233 return segmentCount(string(s))
234 } 234 }
235 235
236 // Split splits a StreamName on its last path element, into a base (everything
nodir 2017/02/18 05:01:51 "base" is somewhat confusing here. Term base is us
dnj 2017/02/18 06:09:31 Change to "prefix".
237 // before that element) and the last element.
238 //
239 // Split assumes that s is a valid stream name.
240 //
241 // If there is only one element in the stream name, base will be empty.
242 //
243 // For example:
244 //
245 // - Split("foo/bar/baz") ("foo/bar", "baz")
246 // - Split("foo") ("", "foo")
247 // - Split("") ("", "")
248 func (s StreamName) Split() (base StreamName, last StreamName) {
nodir 2017/02/18 05:01:51 (base, last StreamName)
dnj 2017/02/18 06:09:31 Done.
249 lidx := strings.LastIndex(string(s), StreamNameSepStr)
250 if lidx < 0 {
251 last = s
252 return
253 }
254 base, last = s[:lidx], s[lidx+len(StreamNameSepStr):]
255 return
256 }
257
236 // UnmarshalJSON implements json.Unmarshaler. 258 // UnmarshalJSON implements json.Unmarshaler.
237 func (s *StreamName) UnmarshalJSON(data []byte) error { 259 func (s *StreamName) UnmarshalJSON(data []byte) error {
238 v := "" 260 v := ""
239 if err := json.Unmarshal(data, &v); err != nil { 261 if err := json.Unmarshal(data, &v); err != nil {
240 return err 262 return err
241 } 263 }
242 if err := StreamName(v).Validate(); err != nil { 264 if err := StreamName(v).Validate(); err != nil {
243 return err 265 return err
244 } 266 }
245 *s = StreamName(v) 267 *s = StreamName(v)
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 459
438 return s 460 return s
439 } 461 }
440 462
441 func segmentCount(s string) int { 463 func segmentCount(s string) int {
442 if len(s) == 0 { 464 if len(s) == 0 {
443 return 0 465 return 0
444 } 466 }
445 return strings.Count(s, string(StreamNameSep)) + 1 467 return strings.Count(s, string(StreamNameSep)) + 1
446 } 468 }
OLDNEW
« no previous file with comments | « no previous file | logdog/common/types/streamname_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698