• Stars
    star
    447
  • Rank 97,700 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

simple struct copying for golang

Deepcopier

Build Status

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

Installation

go get -u github.com/ulule/deepcopier

Usage

// 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)

Available options for deepcopier struct tag:

Option Description
field Field or method name in source instance
skip Ignores the field
context Takes a map[string]interface{} as first argument (for methods)
force Set the value of a sql.Null* field (instead of copying the struct)

Options example:

type Source struct {
    Name                         string
    SkipMe                       string
    SQLNullStringToSQLNullString sql.NullString
    SQLNullStringToString        sql.NullString

}

func (Source) MethodThatTakesContext(c map[string]interface{}) string {
    return "whatever"
}

type Destination struct {
    FieldWithAnotherNameInSource      string         `deepcopier:"field:Name"`
    SkipMe                            string         `deepcopier:"skip"`
    MethodThatTakesContext            string         `deepcopier:"context"`
    SQLNullStringToSQLNullString      sql.NullString 
    SQLNullStringToString             string         `deepcopier:"force"`
}

Example:

package main

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

// Model
type User struct {
    // Basic string field
    Name  string
    // Deepcopier supports https://golang.org/pkg/database/sql/driver/#Valuer
    Email sql.NullString
}

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

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

}

func main() {
    user := &User{
        Name: "gilles",
        Email: sql.NullString{
            Valid: true,
            String: "[email protected]",
        },
    }

    resource := &UserResource{}

    deepcopier.Copy(user).To(resource)

    fmt.Println(resource.DisplayName)
    fmt.Println(resource.Email)
}

Looking for more information about the usage?

We wrote an introduction article. Have a look and feel free to give us your feedback.

Contributing

Don't hesitate ;)

More Repositories

1

limiter

Dead simple rate limit middleware for Go.
Go
2,027
star
2

loukoum

A simple SQL Query Builder
Go
320
star
3

python-logstash-formatter

python JSON log formatter with a logstash compatible schema
Python
158
star
4

django-safety

Generic Django application for safer user accounts.
Python
142
star
5

paging

A small set of utilities to paginate your data in Go
Go
140
star
6

django-linguist

An application to manage translations in Django models
Python
118
star
7

django-courriers

A generic application to manage your newsletters
Python
86
star
8

django-badgify

A reusable application to create your own badge engine using Django
Python
83
star
9

gostorages

A unified interface to manipulate storage engine (file system, s3, etc.) for Go
Go
70
star
10

geoipfix

A Go service (HTTP+RPC) to retrieve location information
Go
48
star
11

limiter-examples

Examples to use limiter
Go
38
star
12

django-separatedvaluesfield

Custom field for Django to separate multiple values in database with a separator and retrieve them as list
Python
26
star
13

go

Ulule bookshelf for Go programing language
22
star
14

gokvstores

A collection of custom key/value storage backends for Go
Go
21
star
15

picfit-go

A Go client library for generating URLs with picfit
Go
14
star
16

makroud

A high level SQL Connector
Go
11
star
17

django-metasettings

A reusable Django application to control the currency rate and favorite language code, inspired by etsy
Python
9
star
18

django-discussions

Messaging system for your users
Python
9
star
19

dekiteru

Dekiteru is a tool that check if a service is ready to use
Go
4
star
20

python-mangopay

A client library for interacting with the Mangopay API
Python
4
star
21

pybbm

A fork of pybbm designed for high traffic websites
Python
4
star
22

ulule-rs

Rust API bindings for the Ulule HTTP API.
Rust
1
star
23

mover

Move data from a database to another
Go
1
star
24

helloapp

Simple hello world application to deploy on kubernetes
Go
1
star