github.com/lyraproj/hiera@v1.0.0-rc4/examples/customfunc_test.go (about) 1 package examples_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/lyraproj/dgo/dgo" 8 "github.com/lyraproj/dgo/vf" 9 "github.com/lyraproj/hiera/api" 10 "github.com/lyraproj/hiera/hiera" 11 "github.com/lyraproj/hiera/provider" 12 sdk "github.com/lyraproj/hierasdk/hiera" 13 ) 14 15 // customLK is a custom lookup key function that just returns from the options that is passed to it 16 func customLK(hc sdk.ProviderContext, key string) dgo.Value { 17 return hc.Option(key) 18 } 19 20 // TestCustomLK shows how to provide an in-process lookup function to Hiera using the api.HieraFunctions 21 // configuration option. 22 func TestCustomLK(t *testing.T) { 23 // Provide custom functions in a dgo.Map so that they can be declared in the hiera configuration file. The function 24 // signature must conform to the declared type (data_dig, data_hash, or lookup_key). 25 // 26 // The exact function signatures are defined in the hierasdk module as hiera.DataDig, hiera.DataHash, and 27 // hiera.LookupKey. 28 customFunctions := vf.Map(`customLK`, customLK) 29 30 configOptions := vf.Map( 31 api.HieraRoot, `testdata`, 32 api.HieraConfigFileName, `custom.yaml`, 33 api.HieraFunctions, customFunctions) 34 35 hiera.DoWithParent(context.Background(), provider.ConfigLookupKey, configOptions, func(hs api.Session) { 36 result := hiera.Lookup(hs.Invocation(nil, nil), `a`, nil, nil) 37 if result == nil || `option a` != result.String() { 38 t.Fatalf("unexpected result %v", result) 39 } 40 }) 41 }