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


Golang copystructure.Copy函數代碼示例

本文整理匯總了Golang中github.com/mitchellh/copystructure.Copy函數的典型用法代碼示例。如果您正苦於以下問題:Golang Copy函數的具體用法?Golang Copy怎麽用?Golang Copy使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Copy函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: upgradeToV2

func (old *instanceStateV1) upgradeToV2() (*InstanceState, error) {
	if old == nil {
		return nil, nil
	}

	attributes, err := copystructure.Copy(old.Attributes)
	if err != nil {
		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
	}
	ephemeral, err := old.Ephemeral.upgradeToV2()
	if err != nil {
		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
	}
	meta, err := copystructure.Copy(old.Meta)
	if err != nil {
		return nil, fmt.Errorf("Error upgrading InstanceState V1: %v", err)
	}

	return &InstanceState{
		ID:         old.ID,
		Attributes: attributes.(map[string]string),
		Ephemeral:  *ephemeral,
		Meta:       meta.(map[string]string),
	}, nil
}
開發者ID:RezaDKhan,項目名稱:terraform,代碼行數:25,代碼來源:state_upgrade_v1_to_v2.go

示例2: LogRequest

func (b *Backend) LogRequest(auth *logical.Auth, req *logical.Request) error {
	if !b.logRaw {
		// Copy the structures
		cp, err := copystructure.Copy(auth)
		if err != nil {
			return err
		}
		auth = cp.(*logical.Auth)

		cp, err = copystructure.Copy(req)
		if err != nil {
			return err
		}
		req = cp.(*logical.Request)

		// Hash any sensitive information
		if err := audit.Hash(auth); err != nil {
			return err
		}
		if err := audit.Hash(req); err != nil {
			return err
		}
	}

	// Encode the entry as JSON
	var buf bytes.Buffer
	var format audit.FormatJSON
	if err := format.FormatRequest(&buf, auth, req); err != nil {
		return err
	}

	// Write out to syslog
	_, err := b.logger.Write(buf.Bytes())
	return err
}
開發者ID:worldspawn,項目名稱:vault,代碼行數:35,代碼來源:backend.go

示例3: LogResponse

func (b *Backend) LogResponse(auth *logical.Auth, req *logical.Request,
	resp *logical.Response, err error) error {
	if !b.logRaw {
		// Before we copy the structure we must nil out some data
		// otherwise we will cause reflection to panic and die
		if req.Connection != nil && req.Connection.ConnState != nil {
			origReq := req
			origState := req.Connection.ConnState
			req.Connection.ConnState = nil
			defer func() {
				origReq.Connection.ConnState = origState
			}()
		}

		// Copy the structure
		cp, err := copystructure.Copy(auth)
		if err != nil {
			return err
		}
		auth = cp.(*logical.Auth)

		cp, err = copystructure.Copy(req)
		if err != nil {
			return err
		}
		req = cp.(*logical.Request)

		cp, err = copystructure.Copy(resp)
		if err != nil {
			return err
		}
		resp = cp.(*logical.Response)

		// Hash any sensitive information
		if err := audit.Hash(auth); err != nil {
			return err
		}
		if err := audit.Hash(req); err != nil {
			return err
		}
		if err := audit.Hash(resp); err != nil {
			return err
		}
	}

	// Encode the entry as JSON
	var buf bytes.Buffer
	var format audit.FormatJSON
	if err := format.FormatResponse(&buf, auth, req, resp, err); err != nil {
		return err
	}

	// Write otu to syslog
	_, err = b.logger.Write(buf.Bytes())
	return err
}
開發者ID:kgutwin,項目名稱:vault,代碼行數:56,代碼來源:backend.go

示例4: LogResponse

func (b *Backend) LogResponse(
	auth *logical.Auth,
	req *logical.Request,
	resp *logical.Response,
	err error) error {
	if err := b.open(); err != nil {
		return err
	}
	if !b.logRaw {
		// Before we copy the structure we must nil out some data
		// otherwise we will cause reflection to panic and die
		if req.Connection != nil && req.Connection.ConnState != nil {
			origReq := req
			origState := req.Connection.ConnState
			req.Connection.ConnState = nil
			defer func() {
				origReq.Connection.ConnState = origState
			}()
		}

		// Copy the structure
		cp, err := copystructure.Copy(auth)
		if err != nil {
			return err
		}
		auth = cp.(*logical.Auth)

		cp, err = copystructure.Copy(req)
		if err != nil {
			return err
		}
		req = cp.(*logical.Request)

		cp, err = copystructure.Copy(resp)
		if err != nil {
			return err
		}
		resp = cp.(*logical.Response)

		// Hash any sensitive information
		if err := audit.Hash(b.salt, auth); err != nil {
			return err
		}
		if err := audit.Hash(b.salt, req); err != nil {
			return err
		}
		if err := audit.Hash(b.salt, resp); err != nil {
			return err
		}
	}

	var format audit.FormatJSON
	return format.FormatResponse(b.f, auth, req, resp, err)
}
開發者ID:vincentaubert,項目名稱:vault,代碼行數:54,代碼來源:backend.go

示例5: Apply

func (r Base) Apply(bndl goci.Bndl, spec gardener.DesiredContainerSpec) goci.Bndl {
	if spec.Privileged {
		copiedBndl, err := copystructure.Copy(r.PrivilegedBase)
		if err != nil {
			panic(err)
		}
		return copiedBndl.(goci.Bndl)
	} else {
		copiedBndl, err := copystructure.Copy(r.UnprivilegedBase)
		if err != nil {
			panic(err)
		}
		return copiedBndl.(goci.Bndl)
	}
}
開發者ID:cloudfoundry,項目名稱:guardian,代碼行數:15,代碼來源:base.go

示例6: Clone

func (self Dictionary) Clone() Dictionary {
	newDict := Create()
	newDict.Init()

	for key, value := range self {
		if nil == key || nil == value {
			continue
		}
		newKey, _ := copystructure.Copy(key)
		newValue, _ := copystructure.Copy(value)
		newDict.SetObjectForKey(newValue, newKey)
	}

	return newDict
}
開發者ID:ziyouchutuwenwu,項目名稱:objective-go,代碼行數:15,代碼來源:dictionary.go

示例7: init

func init() {
	copystructure.Copiers[reflect.TypeOf(Response{})] = func(v interface{}) (interface{}, error) {
		input := v.(Response)
		ret := Response{
			Redirect: input.Redirect,
		}

		if input.Secret != nil {
			retSec, err := copystructure.Copy(input.Secret)
			if err != nil {
				return nil, fmt.Errorf("error copying Secret: %v", err)
			}
			ret.Secret = retSec.(*Secret)
		}

		if input.Auth != nil {
			retAuth, err := copystructure.Copy(input.Auth)
			if err != nil {
				return nil, fmt.Errorf("error copying Auth: %v", err)
			}
			ret.Auth = retAuth.(*Auth)
		}

		if input.Data != nil {
			retData, err := copystructure.Copy(&input.Data)
			if err != nil {
				return nil, fmt.Errorf("error copying Data: %v", err)
			}
			ret.Data = retData.(map[string]interface{})
		}

		if input.Warnings() != nil {
			for _, warning := range input.Warnings() {
				ret.AddWarning(warning)
			}
		}

		if input.WrapInfo != nil {
			retWrapInfo, err := copystructure.Copy(input.WrapInfo)
			if err != nil {
				return nil, fmt.Errorf("error copying WrapInfo: %v", err)
			}
			ret.WrapInfo = retWrapInfo.(*WrapInfo)
		}

		return &ret, nil
	}
}
開發者ID:GauntletWizard,項目名稱:vault,代碼行數:48,代碼來源:response.go

示例8: TestCopy_response

func TestCopy_response(t *testing.T) {
	// Make a non-pointer one so that it can't be modified directly
	expected := logical.Response{
		Data: map[string]interface{}{
			"foo": "bar",
		},
		WrapInfo: &logical.WrapInfo{
			TTL:             60,
			Token:           "foo",
			CreationTime:    time.Now(),
			WrappedAccessor: "abcd1234",
		},
	}
	arg := expected

	// Copy it
	dup, err := copystructure.Copy(&arg)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	// Check equality
	arg2 := dup.(*logical.Response)
	if !reflect.DeepEqual(*arg2, expected) {
		t.Fatalf("bad:\n\n%#v\n\n%#v", *arg2, expected)
	}
}
開發者ID:quixoten,項目名稱:vault,代碼行數:27,代碼來源:hashstructure_test.go

示例9: merge

func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
	if r == nil && r2 == nil {
		return nil
	}

	if r == nil {
		r = &RawConfig{}
	}

	rawRaw, err := copystructure.Copy(r.Raw)
	if err != nil {
		panic(err)
	}

	raw := rawRaw.(map[string]interface{})
	if r2 != nil {
		for k, v := range r2.Raw {
			raw[k] = v
		}
	}

	result, err := NewRawConfig(raw)
	if err != nil {
		panic(err)
	}

	return result
}
開發者ID:hooklift,項目名稱:terraform,代碼行數:28,代碼來源:raw_config.go

示例10: DeepCopy

func (c *Cluster) DeepCopy() *Cluster {
	nc, err := copystructure.Copy(c)
	if err != nil {
		panic(err)
	}
	return nc.(*Cluster)
}
開發者ID:sgotti,項目名稱:stolon,代碼行數:7,代碼來源:cluster.go

示例11: deepcopy

func (r *ResourceState) deepcopy() *ResourceState {
	copy, err := copystructure.Copy(r)
	if err != nil {
		panic(err)
	}

	return copy.(*ResourceState)
}
開發者ID:Originate,項目名稱:terraform,代碼行數:8,代碼來源:state.go

示例12: Clone

func (self Object) Clone() Object {
	newObject := Create()

	if nil != self.Value {
		newObject.Value, _ = copystructure.Copy(self.Value)
	}
	return newObject
}
開發者ID:ziyouchutuwenwu,項目名稱:objective-go,代碼行數:8,代碼來源:object.go

示例13: DeepCopy

// DeepCopy performs a deep copy of the state structure and returns
// a new structure.
func (s *State) DeepCopy() *State {
	copy, err := copystructure.Copy(s)
	if err != nil {
		panic(err)
	}

	return copy.(*State)
}
開發者ID:Originate,項目名稱:terraform,代碼行數:10,代碼來源:state.go

示例14: Compile

// Compile validates a prepared query template and returns an opaque compiled
// object that can be used later to render the template.
func Compile(query *structs.PreparedQuery) (*CompiledTemplate, error) {
	// Make sure it's a type we understand.
	if query.Template.Type != structs.QueryTemplateTypeNamePrefixMatch {
		return nil, fmt.Errorf("Bad Template.Type '%s'", query.Template.Type)
	}

	// Start compile.
	ct := &CompiledTemplate{
		trees: make(map[string]ast.Node),
	}

	// Make a copy of the query to use as the basis for rendering later.
	dup, err := copystructure.Copy(query)
	if err != nil {
		return nil, err
	}
	var ok bool
	ct.query, ok = dup.(*structs.PreparedQuery)
	if !ok {
		return nil, fmt.Errorf("Failed to copy query")
	}

	// Walk over all the string fields in the Service sub-structure and
	// parse them as HIL.
	parse := func(path string, v reflect.Value) error {
		tree, err := hil.Parse(v.String())
		if err != nil {
			return fmt.Errorf("Bad format '%s' in Service%s: %s", v.String(), path, err)
		}

		ct.trees[path] = tree
		return nil
	}
	if err := walk(&ct.query.Service, parse); err != nil {
		return nil, err
	}

	// If they supplied a regexp then compile it.
	if ct.query.Template.Regexp != "" {
		var err error
		ct.re, err = regexp.Compile(ct.query.Template.Regexp)
		if err != nil {
			return nil, fmt.Errorf("Bad Regexp: %s", err)
		}
	}

	// Finally do a test render with the supplied name prefix. This will
	// help catch errors before run time, and this is the most minimal
	// prefix it will be expected to run with. The results might not make
	// sense and create a valid service to lookup, but it should render
	// without any errors.
	if _, err = ct.Render(ct.query.Name); err != nil {
		return nil, err
	}

	return ct, nil
}
開發者ID:pulcy,項目名稱:vault-monkey,代碼行數:59,代碼來源:template.go

示例15: decode

func (d *Decoder) decode(ch chan<- interface{}, pairs consul.KVPairs) error {
	raw := make(map[string]interface{})
	for _, p := range pairs {
		// Trim the prefix off our key first
		key := strings.TrimPrefix(p.Key, d.Prefix)

		// Determine what map we're writing the value to. We split by '/'
		// to determine any sub-maps that need to be created.
		m := raw
		children := strings.Split(key, "/")
		if len(children) > 0 {
			key = children[len(children)-1]
			children = children[:len(children)-1]
			for _, child := range children {
				if m[child] == nil {
					m[child] = make(map[string]interface{})
				}

				subm, ok := m[child].(map[string]interface{})
				if !ok {
					return fmt.Errorf("child is both a data item and dir: %s", child)
				}

				m = subm
			}
		}

		m[key] = string(p.Value)
	}

	// First copy our initial value
	target, err := copystructure.Copy(d.Target)
	if err != nil {
		return err
	}

	// Now decode into it
	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		Metadata:         nil,
		Result:           target,
		WeaklyTypedInput: true,
		TagName:          "consul",
	})
	if err != nil {
		return err
	}
	if err := decoder.Decode(raw); err != nil {
		return err
	}

	// Send it
	ch <- target
	return nil
}
開發者ID:carriercomm,項目名稱:consulstructure,代碼行數:54,代碼來源:consulstructure.go


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