github.com/yanndegat/hiera@v0.6.8/session/serverctx.go (about) 1 package session 2 3 import ( 4 "github.com/lyraproj/dgo/dgo" 5 "github.com/lyraproj/hierasdk/hiera" 6 "github.com/yanndegat/hiera/api" 7 ) 8 9 type serverCtx struct { 10 hiera.ProviderContext 11 invocation api.Invocation 12 } 13 14 func (c *serverCtx) Interpolate(value dgo.Value) dgo.Value { 15 return c.invocation.Interpolate(value, true) 16 } 17 18 func (c *serverCtx) Explain(messageProducer func() string) { 19 c.invocation.ReportText(messageProducer) 20 } 21 22 func (c *serverCtx) Cache(key string, value dgo.Value) dgo.Value { 23 cache := c.invocation.TopProviderCache() 24 if old, loaded := cache.LoadOrStore(key, value); loaded { 25 // Replace old value 26 cache.Store(key, value) 27 return old.(dgo.Value) 28 } 29 return nil 30 } 31 32 func (c *serverCtx) CacheAll(hash dgo.Map) { 33 cache := c.invocation.TopProviderCache() 34 hash.EachEntry(func(e dgo.MapEntry) { 35 cache.Store(e.Key().String(), e.Value()) 36 }) 37 } 38 39 func (c *serverCtx) CachedValue(key string) (dgo.Value, bool) { 40 if v, ok := c.invocation.TopProviderCache().Load(key); ok { 41 return v.(dgo.Value), true 42 } 43 return nil, false 44 } 45 46 func (c *serverCtx) CachedEntries(consumer func(key string, value dgo.Value)) { 47 c.invocation.TopProviderCache().Range(func(k, v interface{}) bool { 48 consumer(k.(string), v.(dgo.Value)) 49 return true 50 }) 51 } 52 53 func (c *serverCtx) Invocation() api.Invocation { 54 return c.invocation 55 } 56 57 func (c *serverCtx) ForData() api.ServerContext { 58 return &serverCtx{ProviderContext: c.ProviderContext, invocation: c.invocation.ForData()} 59 } 60 61 func (c *serverCtx) ForLookupOptions() api.ServerContext { 62 return &serverCtx{ProviderContext: c.ProviderContext, invocation: c.invocation.ForLookupOptions()} 63 }