github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/injector/container_multi.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package injector
     7  
     8  import (
     9  	"github.com/insolar/vanilla/throw"
    10  )
    11  
    12  func NewMultiMapRegistry(maps []map[string]interface{}) MultiMapRegistry {
    13  	return MultiMapRegistry{maps}
    14  }
    15  
    16  var _ DependencyRegistry = MultiMapRegistry{}
    17  var _ ScanDependencyRegistry = MultiMapRegistry{}
    18  
    19  type MultiMapRegistry struct {
    20  	maps []map[string]interface{}
    21  }
    22  
    23  func (v MultiMapRegistry) ScanDependencies(fn func(id string, v interface{}) bool) bool {
    24  	if fn == nil {
    25  		panic(throw.IllegalValue())
    26  	}
    27  	for _, om := range v.maps {
    28  		for id, v := range om {
    29  			if fn(id, v) {
    30  				return true
    31  			}
    32  		}
    33  	}
    34  	return false
    35  }
    36  
    37  func (v MultiMapRegistry) FindDependency(id string) (interface{}, bool) {
    38  	for _, om := range v.maps {
    39  		if v, ok := om[id]; ok {
    40  			return v, true
    41  		}
    42  	}
    43  	return nil, false
    44  }