copier

package
v1.14.4 Latest Latest
Warning

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

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

README

copyopt

copier is github.com/jinzhu/copier, default option is add converters for time.Time <--> String.

Example of use

package main

import (
    "fmt"
    "time"

    "github.com/go-dev-frame/sponge/pkg/copier"
)

type Model struct {
    ID        int64
    MyIP      string
    OrderID   uint32
    CreatedAt *time.Time
    UpdatedAt *time.Time
    DeletedAt *time.Time
}

type Reply struct {
    Id        int
    MyIp      string
    OrderId   int
    CreatedAt string
    UpdatedAt string
    DeletedAt string
}

func main() {
    now := time.Now()
    updated := now.Add(time.Hour)
    deleted := updated.Add(time.Hour)

    src := &Model{
        ID:        123,
        MyIP:      "127.0.0.1",
        OrderID:   888,
        CreatedAt: &now,
        UpdatedAt: &updated,
        DeletedAt: &deleted,
    }
    dst := &Reply{}
    err := copier.Copy(dst, src)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v\n", dst)
}

Documentation

Overview

Package copier is github.com/jinzhu/copier, default option is add converters for time.Time <--> String

Index

Constants

This section is empty.

Variables

View Source
var Converter = copier.Option{
	DeepCopy: true,
	Converters: []copier.TypeConverter{

		{
			SrcType: time.Time{},
			DstType: copier.String,
			Fn: func(src interface{}) (interface{}, error) {
				s, ok := src.(time.Time)
				if !ok {
					return nil, fmt.Errorf("expected time.Time got %T", src)
				}
				return s.Format(time.RFC3339), nil
			},
		},

		{
			SrcType: &time.Time{},
			DstType: copier.String,
			Fn: func(src interface{}) (interface{}, error) {
				s, ok := src.(*time.Time)
				if !ok {
					return nil, fmt.Errorf("expected *time.Time got %T", src)
				}
				if s == nil {
					return "", nil
				}
				return s.Format(time.RFC3339), nil
			},
		},

		{
			SrcType: copier.String,
			DstType: time.Time{},
			Fn: func(src interface{}) (interface{}, error) {
				s, ok := src.(string)
				if !ok {
					return nil, fmt.Errorf("expected string got %T", src)
				}
				if s == "" {
					return time.Time{}, nil
				}
				return time.Parse(time.RFC3339, s)
			},
		},

		{
			SrcType: copier.String,
			DstType: &time.Time{},
			Fn: func(src interface{}) (interface{}, error) {
				s, ok := src.(string)
				if !ok {
					return nil, fmt.Errorf("expected string got %T", src)
				}
				if s == "" {
					return nil, nil
				}
				t, err := time.Parse(time.RFC3339, s)
				return &t, err
			},
		},
	},
}

Converter Converters for time.Time <--> String

Functions

func Copy

func Copy(dst interface{}, src interface{}) error

Copy src to dst with option Converters for time.Time <--> String

func CopyDefault

func CopyDefault(dst interface{}, src interface{}) error

CopyDefault copy src to dst with default option

func CopyWithOption

func CopyWithOption(dst interface{}, src interface{}, options copier.Option) error

CopyWithOption copy src to dst with option

Types

This section is empty.

Jump to

Keyboard shortcuts

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