From d3cde0720bccadccc5ed60b8a70eb3dd3afb1d91 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Thu, 23 Jun 2016 17:54:49 +0200 Subject: [PATCH] schema/types: don't panic validating incomplete isolators Even though Isolator instances should be constructed via unmarshaling, library users may still construct one directly with a nil value. This commit fixes the validator to handle this error gracefully instead of panicing. --- schema/types/isolator.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/schema/types/isolator.go b/schema/types/isolator.go index ab8eee1f..fcd1801e 100644 --- a/schema/types/isolator.go +++ b/schema/types/isolator.go @@ -23,7 +23,12 @@ import ( var ( isolatorMap map[ACIdentifier]IsolatorValueConstructor + // ErrIncompatibleIsolator is returned whenever an Isolators set contains + // conflicting IsolatorValue instances ErrIncompatibleIsolator = errors.New("isolators set contains incompatible types") + // ErrInvalidIsolator is returned upon validation failures due to improper + // or partially constructed Isolator instances (eg. from incomplete direct construction) + ErrInvalidIsolator = errors.New("invalid isolator") ) func init() { @@ -49,15 +54,19 @@ type Isolators []Isolator func (isolators Isolators) assertValid() error { typesMap := make(map[ACIdentifier]bool) for _, i := range isolators { - if err := i.Value().AssertValid(); err != nil { + v := i.Value() + if v == nil { + return ErrInvalidIsolator + } + if err := v.AssertValid(); err != nil { return err } if _, ok := typesMap[i.Name]; ok { - if !i.Value().multipleAllowed() { + if !v.multipleAllowed() { return fmt.Errorf(`isolators set contains too many instances of type %s"`, i.Name) } } - for _, c := range i.Value().Conflicts() { + for _, c := range v.Conflicts() { if _, found := typesMap[c]; found { return ErrIncompatibleIsolator }