deepcopier

package module
v0.0.0-...-7882635 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2015 License: MIT Imports: 6 Imported by: 0

README

Deepcopier
==========

.. image:: https://secure.travis-ci.org/ulule/deepcopier.png?branch=master
    :alt: Build Status
    :target: http://travis-ci.org/ulule/deepcopier

This package is meant to make copying of structs to/from others structs a bit easier.


Installation
------------

::

    $ go get github.com/ulule/deepcopier

This package requires some dependencies:

* `oleiade/reflections <https://github.com/oleiade/reflections>`_: An awesome high level abstractions over reflect library

All dependencies will be installed for you.

Basic usage
-----------

.. code-block:: go

    // Deep copy instance1 into instance2
    Copy(instance1).To(instance2)

    // Deep copy instance1 into instance2 and passes the following context (which
    // is basically a map[string]interface{}) as first argument
    // to methods of instance2 that defined the struct tag "context".
    Copy(instance1).WithContext(map[string]interface{}{"foo": "bar"}).To(instance2)

    // Deep copy instance2 into instance1
    Copy(instance1).From(instance2)

    // Deep copy instance2 into instance1 and passes the following context (which
    // is basically a map[string]interface{}) as first argument
    // to methods of instance1 that defined the struct tag "context".
    Copy(instance1).WithContext(map[string]interface{}{"foo": "bar"}).From(instance2)

You should use the following struct tags:

* **field**: name of the field in the target instance
* **context**: method will take context (map[string]interface{}) as first argument
* **skip**: skip this field (do not process anything)

Example:

.. code-block:: go

    package main

    import (
        "fmt"
        "github.com/ulule/deepcopier"
    )

    // Model
    type User struct {
        Name string
    }

    func (u *User) MethodThatTakesContext(ctx map[string]interface{}) string {
        // do whatever you want
        return ""
    }

    // Resource
    type UserResource struct {
        DisplayName            string `deepcopier:"field:Name"`
        SkipMe                 string `deepcopier:"skip"`
        MethodThatTakesContext string `deepcopier:"context"`
    }

    func main() {
        user := &User{
            Name: "gilles",
        }

        resource := &UserResource{}

        deepcopier.Copy(user).To(resource)

        fmt.Println(resource.DisplayName)
    }

Looking for more information about the usage?

We wrote `an introduction article <https://github.com/ulule/deepcopier/blob/master/examples/rest-usage/README.rst>`_,
have a look and give us your feedback.

Contributing
------------

* Ping us on twitter `@oibafsellig <https://twitter.com/oibafsellig>`_, `@thoas <https://twitter.com/thoas>`_
* Fork the `project <https://github.com/ulule/deepcopier>`_
* Fix `bugs <https://github.com/ulule/deepcopier/issues>`_

Don't hesitate ;)

Documentation

Index

Constants

View Source
const (
	// TagName is struct field tag name.
	TagName = "deepcopier"

	// FieldOptionName is the from field option name for struct tag.
	FieldOptionName = "field"

	// ContextOptionName is the context option name for struct tag.
	ContextOptionName = "context"

	// SkipOptionName is the skip option name for struct tag.
	SkipOptionName = "skip"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DeepCopier

type DeepCopier struct {
	Source      interface{}
	Destination interface{}
	Tagged      interface{}
	Context     map[string]interface{}
	Reversed    bool
}

DeepCopier deep copies a struct to/from a struct.

func Copy

func Copy(source interface{}) *DeepCopier

Copy sets the source.

func (*DeepCopier) From

func (dc *DeepCopier) From(tagged interface{}) error

From sets the given tagged struct as source and the current source as destination. Source <- Destination

func (*DeepCopier) GetTagOptions

func (dc *DeepCopier) GetTagOptions(value string) map[string]string

GetTagOptions parses deepcopier tag field and returns options.

func (*DeepCopier) HandleField

func (dc *DeepCopier) HandleField(options *FieldOptions) error

HandleField sets value for the given field.

func (*DeepCopier) HandleMethod

func (dc *DeepCopier) HandleMethod(options *FieldOptions) error

HandleMethod tries to call method on model and sets result in resource field.

func (*DeepCopier) HandleStructField

func (dc *DeepCopier) HandleStructField(options *FieldOptions) error

HandleStructField sets the value for the given supported struct field.

func (*DeepCopier) ProcessCopy

func (dc *DeepCopier) ProcessCopy() error

ProcessCopy processes copy.

func (*DeepCopier) SetField

func (dc *DeepCopier) SetField(options *FieldOptions) error

SetField sets the value of the given field.

func (*DeepCopier) SetFieldValue

func (dc *DeepCopier) SetFieldValue(entity interface{}, name string, value reflect.Value) error

SetFieldValue Sets the given value to the given field.

func (*DeepCopier) To

func (dc *DeepCopier) To(tagged interface{}) error

To sets the given tagged struct as destination struct. Source -> Destination

func (*DeepCopier) WithContext

func (dc *DeepCopier) WithContext(context map[string]interface{}) *DeepCopier

WithContext injects the given context into the builder instance.

type FieldOptions

type FieldOptions struct {
	SourceField      string
	DestinationField string
	WithContext      bool
	Skip             bool
}

FieldOptions contains options passed to SetField method.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL