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

Unified Diff: common/errors/lazymultierror.go

Issue 1284323002: Make LazyMultiError only constructable via function (Closed) Base URL: https://github.com/luci/luci-go.git@master
Patch Set: Make interface, move to new file Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | common/errors/lazymultierror_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/errors/lazymultierror.go
diff --git a/common/errors/lazymultierror.go b/common/errors/lazymultierror.go
new file mode 100644
index 0000000000000000000000000000000000000000..f4fbec3881503ce51ac8a9cac05afdaefcf62bd5
--- /dev/null
+++ b/common/errors/lazymultierror.go
@@ -0,0 +1,80 @@
+// Copyright 2015 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 errors
+
+import (
+ "sync"
+)
+
+// LazyMultiError is a lazily-constructed MultiError.
+//
+// LazyMultiError is like MultiError, except that you know the ultimate size up
+// front, and then you call Assign for each error encountered, and it's
+// potential index. The underlying MultiError will only be allocated if one of
+// the Assign'd errors is non-nil. Similarly, Get will retrieve either the
+// allocated MultiError, or nil if no error was encountered.
+// Build one with NewLazy.
tandrii(chromium) 2015/08/14 17:14:30 nit: NewLazyMultiError
+type LazyMultiError interface {
+ // Assign semantically assigns the error to the given index in the MultiError.
+ // If the error is nil, no action is taken. Otherwise the MultiError is
+ // allocated to its full size (if not already), and the error assigned into
+ // it.
+ //
+ // Returns true iff err != nil (i.e. "was it assigned?"), so you can use this
+ // like:
+ // if !lme.Assign(i, err) {
+ // // stuff requiring err == nil
+ // }
+ Assign(int, error) bool
+
+ // GetOne returns the error at the given index (which may be nil)
+ GetOne(int) error
+
+ // Get returns the MultiError, or nil, if no non-nil error was Assign'd.
+ Get() error
+}
+
+type lazyMultiError struct {
+ sync.Mutex
+
+ size int
+ me MultiError
+}
+
+// NewLazyMultiError makes a new LazyMultiError of the provided size.
+func NewLazyMultiError(size int) *lazyMultiError {
+ return &lazyMultiError{size: size}
+}
+
+func (e *lazyMultiError) Assign(i int, err error) bool {
+ if err == nil {
+ return false
+ }
+ e.Lock()
+ defer e.Unlock()
+ if e.me == nil {
+ e.me = make(MultiError, e.size)
+ }
+ e.me[i] = err
+ return true
+}
+
+func (e *lazyMultiError) GetOne(i int) error {
+ e.Lock()
+ defer e.Unlock()
+ if e.me == nil {
+ return nil
+ }
+ return e.me[i]
+}
+
+func (e *lazyMultiError) Get() error {
+ e.Lock()
+ defer e.Unlock()
+ if e.me == nil {
+ return nil
+ }
+ return e.me
+}
« no previous file with comments | « no previous file | common/errors/lazymultierror_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698