| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package types | 5 package types |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" |
| 8 "errors" | 9 "errors" |
| 9 "fmt" | 10 "fmt" |
| 10 "strings" | 11 "strings" |
| 11 "unicode/utf8" | 12 "unicode/utf8" |
| 12 ) | 13 ) |
| 13 | 14 |
| 14 const ( | 15 const ( |
| 15 // StreamNameSep is the separator rune for stream name tokens. | 16 // StreamNameSep is the separator rune for stream name tokens. |
| 16 StreamNameSep = '/' | 17 StreamNameSep = '/' |
| 17 nameSepStr = "/" | 18 nameSepStr = "/" |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 203 } |
| 203 | 204 |
| 204 // SegmentCount returns the total number of segments in the StreamName. | 205 // SegmentCount returns the total number of segments in the StreamName. |
| 205 func (s StreamName) SegmentCount() int { | 206 func (s StreamName) SegmentCount() int { |
| 206 if len(s) == 0 { | 207 if len(s) == 0 { |
| 207 return 0 | 208 return 0 |
| 208 } | 209 } |
| 209 return strings.Count(string(s), string(StreamNameSep)) + 1 | 210 return strings.Count(string(s), string(StreamNameSep)) + 1 |
| 210 } | 211 } |
| 211 | 212 |
| 213 // UnmarshalJSON implements json.Unmarshaler. |
| 214 func (s *StreamName) UnmarshalJSON(data []byte) error { |
| 215 v := "" |
| 216 if err := json.Unmarshal(data, &v); err != nil { |
| 217 return err |
| 218 } |
| 219 if err := StreamName(v).Validate(); err != nil { |
| 220 return err |
| 221 } |
| 222 *s = StreamName(v) |
| 223 return nil |
| 224 } |
| 225 |
| 226 // MarshalJSON implements json.Marshaler. |
| 227 func (s StreamName) MarshalJSON() ([]byte, error) { |
| 228 v := string(s) |
| 229 return json.Marshal(&v) |
| 230 } |
| 231 |
| 212 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) | 232 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) |
| 213 // separator. | 233 // separator. |
| 214 type StreamPath string | 234 type StreamPath string |
| 215 | 235 |
| 216 // Split splits a StreamPath into its prefix and name components. | 236 // Split splits a StreamPath into its prefix and name components. |
| 217 // | 237 // |
| 218 // If there is no divider present (e.g., foo/bar/baz), the result will parse | 238 // If there is no divider present (e.g., foo/bar/baz), the result will parse |
| 219 // as the stream prefix with an empty name component. | 239 // as the stream prefix with an empty name component. |
| 220 func (p StreamPath) Split() (StreamName, StreamName) { | 240 func (p StreamPath) Split() (StreamName, StreamName) { |
| 221 segments := StreamName(p).Segments() | 241 segments := StreamName(p).Segments() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 232 func (p StreamPath) Validate() error { | 252 func (p StreamPath) Validate() error { |
| 233 prefix, tail := p.Split() | 253 prefix, tail := p.Split() |
| 234 if err := prefix.Validate(); err != nil { | 254 if err := prefix.Validate(); err != nil { |
| 235 return err | 255 return err |
| 236 } | 256 } |
| 237 if err := tail.Validate(); err != nil { | 257 if err := tail.Validate(); err != nil { |
| 238 return err | 258 return err |
| 239 } | 259 } |
| 240 return nil | 260 return nil |
| 241 } | 261 } |
| 262 |
| 263 // UnmarshalJSON implements json.Unmarshaler. |
| 264 func (p *StreamPath) UnmarshalJSON(data []byte) error { |
| 265 v := "" |
| 266 if err := json.Unmarshal(data, &v); err != nil { |
| 267 return err |
| 268 } |
| 269 if err := StreamPath(v).Validate(); err != nil { |
| 270 return err |
| 271 } |
| 272 *p = StreamPath(v) |
| 273 return nil |
| 274 } |
| 275 |
| 276 // MarshalJSON implements json.Marshaler. |
| 277 func (p StreamPath) MarshalJSON() ([]byte, error) { |
| 278 v := string(p) |
| 279 return json.Marshal(&v) |
| 280 } |
| OLD | NEW |