當前位置: 首頁>>代碼示例>>Golang>>正文


Golang reflectwalk.Walk函數代碼示例

本文整理匯總了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
}
開發者ID:chrishoffman,項目名稱:vault,代碼行數:29,代碼來源:copystructure.go

示例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
}
開發者ID:packetloop,項目名稱:terraform,代碼行數:26,代碼來源:raw_config.go

示例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
}
開發者ID:mhlias,項目名稱:terraform,代碼行數:33,代碼來源:raw_config.go

示例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
}
開發者ID:influxdata,項目名稱:kapacitor,代碼行數:28,代碼來源:override.go

示例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
}
開發者ID:influxdata,項目名稱:kapacitor,代碼行數:11,代碼來源:override.go

示例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
}
開發者ID:influxdata,項目名稱:kapacitor,代碼行數:10,代碼來源:override.go

示例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)
		}
	}
}
開發者ID:GeorgeErickson,項目名稱:terraform-1,代碼行數:52,代碼來源:interpolate_walk_test.go

示例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
}
開發者ID:rchicoli,項目名稱:consul-template,代碼行數:17,代碼來源:hashstructure.go

示例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
}
開發者ID:Banno,項目名稱:terraform-provider-mesoskafka,代碼行數:20,代碼來源:closer.go

示例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
}
開發者ID:c12simple,項目名稱:packer,代碼行數:17,代碼來源:render.go

示例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
}
開發者ID:mhlias,項目名稱:terraform,代碼行數:16,代碼來源:raw_config.go

示例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)
			}
		})
	}
}
開發者ID:hooklift,項目名稱:terraform,代碼行數:47,代碼來源:resource_test.go

示例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
}
開發者ID:faradayio,項目名稱:vault-1,代碼行數:18,代碼來源:copystructure.go

示例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
}
開發者ID:c12simple,項目名稱:packer,代碼行數:20,代碼來源:render.go

示例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
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:27,代碼來源:raw_config.go


注:本文中的github.com/mitchellh/reflectwalk.Walk函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。