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 errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 "sync" | 10 "sync" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 continue | 66 continue |
67 } | 67 } |
68 ret = append(ret, e) | 68 ret = append(ret, e) |
69 } | 69 } |
70 if len(ret) == 0 { | 70 if len(ret) == 0 { |
71 return nil | 71 return nil |
72 } | 72 } |
73 return ret | 73 return ret |
74 } | 74 } |
75 | 75 |
76 // LazyMultiError is a lazily-constructed MultiError. You specify the target | 76 // LazyMultiError is a lazily-constructed MultiError. |
77 // MultiError size up front (as Size), and then you call Assign for each error | 77 // |
78 // encountered, and it's potential index. The MultiError will only be allocated | 78 // LazyMultiError is like MultiError, except that you know the ultimate size up |
79 // if one of the Assign'd errors is non-nil. Similarly, Get will retrieve either | 79 // front, and then you call Assign for each error encountered, and it's |
80 // the allocated MultiError, or nil if no error was encountered. | 80 // potential index. The underlying MultiError will only be allocated if one of |
81 // the Assign'd errors is non-nil. Similarly, Get will retrieve either the | |
82 // allocated MultiError, or nil if no error was encountered. | |
83 // Build one with NewLazy. | |
81 type LazyMultiError struct { | 84 type LazyMultiError struct { |
M-A Ruel
2015/08/12 19:13:50
Why doesn't LazyMultiError have method Error() so
dnj (Google)
2015/08/14 16:40:00
If your goal is "only constructable", then LazyMul
iannucci
2015/08/14 17:00:26
Done
| |
82 sync.Mutex | 85 sync.Mutex |
83 | 86 |
84 » Size int | 87 » size int |
tandrii_google
2015/08/14 08:50:36
wait, this doesn't make what CL description says.
iannucci
2015/08/14 17:00:26
done
| |
85 me MultiError | 88 me MultiError |
86 } | 89 } |
87 | 90 |
91 // NewLazyMultiError makes a new LazyMultiError of the provided size. | |
92 func NewLazyMultiError(size int) *LazyMultiError { | |
93 return &LazyMultiError{size: size} | |
94 } | |
95 | |
88 // Assign semantically assigns the error to the given index in the MultiError. | 96 // Assign semantically assigns the error to the given index in the MultiError. |
89 // If the error is nil, no action is taken. Otherwise the MultiError is | 97 // If the error is nil, no action is taken. Otherwise the MultiError is |
90 // allocated to its full size (if not already), and the error assigned into it. | 98 // allocated to its full size (if not already), and the error assigned into it. |
91 // | 99 // |
92 // It returns true iff err != nil. | 100 // It returns true iff err != nil. |
93 func (e *LazyMultiError) Assign(i int, err error) bool { | 101 func (e *LazyMultiError) Assign(i int, err error) bool { |
94 if err == nil { | 102 if err == nil { |
95 return false | 103 return false |
96 } | 104 } |
97 e.Lock() | 105 e.Lock() |
98 defer e.Unlock() | 106 defer e.Unlock() |
99 if e.me == nil { | 107 if e.me == nil { |
100 » » e.me = make(MultiError, e.Size) | 108 » » e.me = make(MultiError, e.size) |
101 } | 109 } |
102 e.me[i] = err | 110 e.me[i] = err |
103 return true | 111 return true |
104 } | 112 } |
105 | 113 |
106 // GetOne returns the error at the given index, or nil, if no non-nil error | 114 // GetOne returns the error at the given index, or nil, if no non-nil error |
107 // was Assign'd. | 115 // was Assign'd. |
108 func (e *LazyMultiError) GetOne(i int) error { | 116 func (e *LazyMultiError) GetOne(i int) error { |
109 e.Lock() | 117 e.Lock() |
110 defer e.Unlock() | 118 defer e.Unlock() |
(...skipping 20 matching lines...) Expand all Loading... | |
131 // we know that err already conforms to the error interface (or the caller's | 139 // we know that err already conforms to the error interface (or the caller's |
132 // method wouldn't compile), so check to see if the error's unde rlying type | 140 // method wouldn't compile), so check to see if the error's unde rlying type |
133 // looks like one of the special error types we implement. | 141 // looks like one of the special error types we implement. |
134 v := reflect.ValueOf(err) | 142 v := reflect.ValueOf(err) |
135 if v.Type().ConvertibleTo(multiErrorType) { | 143 if v.Type().ConvertibleTo(multiErrorType) { |
136 err = v.Convert(multiErrorType).Interface().(error) | 144 err = v.Convert(multiErrorType).Interface().(error) |
137 } | 145 } |
138 } | 146 } |
139 return err | 147 return err |
140 } | 148 } |
OLD | NEW |