github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/injector/container_ro.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 NewStaticContainer(parentRegistry DependencyRegistry, contentRegistry ScanDependencyRegistry) StaticContainer { 13 sc := StaticContainer{ parentRegistry: parentRegistry } 14 if contentRegistry != nil { 15 sc.localRegistry = map[string]interface{}{} 16 contentRegistry.ScanDependencies(func(id string, v interface{}) bool { 17 sc.localRegistry[id] = v 18 return false 19 }) 20 } 21 return sc 22 } 23 24 type StaticContainer struct { 25 parentRegistry DependencyRegistry 26 localRegistry map[string]interface{} 27 } 28 29 func (m StaticContainer) FindDependency(id string) (interface{}, bool) { 30 if v, ok := m.localRegistry[id]; ok { 31 return v, true 32 } 33 if m.parentRegistry != nil { 34 return m.parentRegistry.FindDependency(id) 35 } 36 return nil, false 37 } 38 39 func (m StaticContainer) ScanDependencies(fn func(id string, v interface{}) bool) (found bool) { 40 if fn == nil { 41 panic(throw.IllegalValue()) 42 } 43 44 for key, value := range m.localRegistry { 45 if fn(key, value) { 46 return true 47 } 48 } 49 50 if sp, ok := m.parentRegistry.(ScanDependencyRegistry); ok { 51 return sp.ScanDependencies(fn) 52 } 53 return false 54 } 55 56 func (m StaticContainer) AsRegistry() DependencyRegistry { 57 if m.parentRegistry == nil && len(m.localRegistry) == 0 { 58 return nil 59 } 60 return m 61 }