本文整理匯總了Golang中github.com/mitchellh/reflectwalk.Walk函數的典型用法代碼示例。如果您正苦於以下問題:Golang Walk函數的具體用法?Golang Walk怎麽用?Golang Walk使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Walk函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Copy
func (c Config) Copy(v interface{}) (interface{}, error) {
if c.Lock && reflect.ValueOf(v).Kind() != reflect.Ptr {
return nil, errPointerRequired
}
w := new(walker)
if c.Lock {
w.useLocks = true
}
if c.Copiers == nil {
c.Copiers = Copiers
}
err := reflectwalk.Walk(v, w)
if err != nil {
return nil, err
}
// Get the result. If the result is nil, then we want to turn it
// into a typed nil if we can.
result := w.Result
if result == nil {
val := reflect.ValueOf(v)
result = reflect.Indirect(reflect.New(val.Type())).Interface()
}
return result, nil
}
示例2: init
func (r *RawConfig) init() error {
r.config = r.Raw
r.Interpolations = nil
r.Variables = nil
fn := func(i Interpolation) (string, error) {
r.Interpolations = append(r.Interpolations, i)
for k, v := range i.Variables() {
if r.Variables == nil {
r.Variables = make(map[string]InterpolatedVariable)
}
r.Variables[k] = v
}
return "", nil
}
walker := &interpolationWalker{F: fn}
if err := reflectwalk.Walk(r.Raw, walker); err != nil {
return err
}
return nil
}
示例3: init
func (r *RawConfig) init() error {
r.lock.Lock()
defer r.lock.Unlock()
r.config = r.Raw
r.Interpolations = nil
r.Variables = nil
fn := func(node ast.Node) (interface{}, error) {
r.Interpolations = append(r.Interpolations, node)
vars, err := DetectVariables(node)
if err != nil {
return "", err
}
for _, v := range vars {
if r.Variables == nil {
r.Variables = make(map[string]InterpolatedVariable)
}
r.Variables[v.FullKey()] = v
}
return "", nil
}
walker := &interpolationWalker{F: fn}
if err := reflectwalk.Walk(r.Raw, walker); err != nil {
return err
}
return nil
}
示例4: applyOverride
// applyOverride applies the given override to the specified object.
func applyOverride(object interface{}, o Override) (Element, error) {
if err := o.Validate(); err != nil {
return Element{}, errors.Wrap(err, "invalid override")
}
walker := newOverrideWalker(o)
// walk the copy and apply the updates
if err := reflectwalk.Walk(object, walker); err != nil {
return Element{}, err
}
unused := walker.unused()
if len(unused) > 0 {
return Element{}, fmt.Errorf("unknown options %v in section %s", unused, o.Section)
}
// Return the modified copy
element := walker.elementObject()
if element.value == nil && !o.Delete {
return Element{}, fmt.Errorf("unknown section %s", o.Section)
}
// Validate new value
if v, ok := element.value.(Validator); ok {
if err := v.Validate(); err != nil {
return Element{}, errors.Wrap(err, "failed validation")
}
}
return element, nil
}
示例5: Redacted
// Redacted returns the options for the element in a map.
// Any fields with the `override:",redact"` tag set will be replaced
// with a boolean value indicating whether a non-zero value was set.
func (e Element) Redacted() (map[string]interface{}, []string, error) {
walker := newRedactWalker()
// walk the section and collect redacted options
if err := reflectwalk.Walk(e.value, walker); err != nil {
return nil, nil, errors.Wrap(err, "failed to redact section")
}
return walker.optionsMap(), walker.redactedList(), nil
}
示例6: ElementKeys
// ElementKeys returns a map of section name to element key for each section.
func ElementKeys(config interface{}) (map[string]string, error) {
// walk the config and read all sections
walker := newSectionWalker()
if err := reflectwalk.Walk(config, walker); err != nil {
return nil, errors.Wrap(err, "failed to read sections from configuration object")
}
return walker.elementKeysMap(), nil
}
示例7: TestInterpolationWalker_replace
func TestInterpolationWalker_replace(t *testing.T) {
cases := []struct {
Input interface{}
Output interface{}
}{
{
Input: map[string]interface{}{
"foo": "$${var.foo}",
},
Output: map[string]interface{}{
"foo": "$${var.foo}",
},
},
{
Input: map[string]interface{}{
"foo": "hello, ${var.foo}",
},
Output: map[string]interface{}{
"foo": "hello, bar",
},
},
{
Input: map[string]interface{}{
"foo": map[string]interface{}{
"${var.foo}": "bar",
},
},
Output: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "bar",
},
},
},
}
for i, tc := range cases {
fn := func(i Interpolation) (string, error) {
return "bar", nil
}
w := &interpolationWalker{F: fn, Replace: true}
if err := reflectwalk.Walk(tc.Input, w); err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(tc.Input, tc.Output) {
t.Fatalf("%d: bad:\n\n%#v", i, tc.Input)
}
}
}
示例8: HashStructure
// HashStructure takes an interface and hashes all the values within
// the structure. Only _values_ are hashed: keys of objects are not.
//
// For the HashCallback, see the built-in HashCallbacks below.
func HashStructure(s interface{}, cb HashCallback) (interface{}, error) {
s, err := copystructure.Copy(s)
if err != nil {
return nil, err
}
walker := &hashWalker{Callback: cb}
if err := reflectwalk.Walk(s, walker); err != nil {
return nil, err
}
return s, nil
}
示例9: Close
// Close will close all shadow values within the given structure.
//
// This uses reflection to walk the structure, find all shadow elements,
// and close them. Currently this will only find struct fields that are
// shadow values, and not slice elements, etc.
func Close(v interface{}) error {
// We require a pointer so we can address the internal fields
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return fmt.Errorf("value must be a pointer")
}
// Walk and close
var w closeWalker
if err := reflectwalk.Walk(v, &w); err != nil {
return err
}
return w.Err
}
示例10: ValidateInterface
// ValidateInterface renders any value and returns the resulting value.
func ValidateInterface(v interface{}, ctx *Context) error {
f := func(v string) (string, error) {
return v, Validate(v, ctx)
}
walker := &renderWalker{
F: f,
Replace: false,
}
err := reflectwalk.Walk(v, walker)
if err != nil {
return err
}
return nil
}
示例11: interpolate
func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
config, err := copystructure.Copy(r.Raw)
if err != nil {
return err
}
r.config = config.(map[string]interface{})
w := &interpolationWalker{F: fn, Replace: true}
err = reflectwalk.Walk(r.config, w)
if err != nil {
return err
}
r.unknownKeys = w.unknownKeys
return nil
}
示例12: TestUnknownCheckWalker
func TestUnknownCheckWalker(t *testing.T) {
cases := []struct {
Name string
Input interface{}
Result bool
}{
{
"primitive",
42,
false,
},
{
"primitive computed",
unknownValue(),
true,
},
{
"list",
[]interface{}{"foo", unknownValue()},
true,
},
{
"nested list",
[]interface{}{
"foo",
[]interface{}{unknownValue()},
},
true,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
var w unknownCheckWalker
if err := reflectwalk.Walk(tc.Input, &w); err != nil {
t.Fatalf("err: %s", err)
}
if w.Unknown != tc.Result {
t.Fatalf("bad: %v", w.Unknown)
}
})
}
}
示例13: Copy
// Copy returns a deep copy of v.
func Copy(v interface{}) (interface{}, error) {
w := new(walker)
err := reflectwalk.Walk(v, w)
if err != nil {
return nil, err
}
// Get the result. If the result is nil, then we want to turn it
// into a typed nil if we can.
result := w.Result
if result == nil {
val := reflect.ValueOf(v)
result = reflect.Indirect(reflect.New(val.Type())).Interface()
}
return result, nil
}
示例14: RenderInterface
// RenderInterface renders any value and returns the resulting value.
func RenderInterface(v interface{}, ctx *Context) (interface{}, error) {
f := func(v string) (string, error) {
return Render(v, ctx)
}
walker := &renderWalker{
F: f,
Replace: true,
}
err := reflectwalk.Walk(v, walker)
if err != nil {
return nil, err
}
if walker.Top != nil {
v = walker.Top
}
return v, nil
}
示例15: Interpolate
// Interpolate uses the given mapping of variable values and uses
// those as the values to replace any variables in this raw
// configuration.
//
// Any prior calls to Interpolate are replaced with this one.
//
// If a variable key is missing, this will panic.
func (r *RawConfig) Interpolate(vs map[string]string) error {
config, err := copystructure.Copy(r.Raw)
if err != nil {
return err
}
r.config = config.(map[string]interface{})
fn := func(i Interpolation) (string, error) {
return i.Interpolate(vs)
}
w := &interpolationWalker{F: fn, Replace: true}
err = reflectwalk.Walk(r.config, w)
if err != nil {
return err
}
r.unknownKeys = w.unknownKeys
return nil
}