structmapper

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: MIT Imports: 12 Imported by: 0

README

structmapper

structmapper for golang, copy value from struct to struct. Extends copier(https://github.com/jinzhu/copier)

Feature

  • Copy from field to field with same name, structmapper tag, json tag
  • Copy different types with Transformer func

Usage

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Age  int32  `json:"age" structmapper:"description"`
}

type Node struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

func Example() {
	user := &User{
		ID:   "12345",
		Name: "山田太郎",
		Age:  32,
	}

	node := new(Node)

	err := structmapper.New().
		RegisterTransformer(
			structmapper.Target{
				From: reflect.TypeOf(int32(0)),
				To:   reflect.TypeOf(""),
			},
			func(from reflect.Value, _ reflect.Type) (reflect.Value, error) {
				if !from.IsValid() {
					return reflect.ValueOf(nil), nil
				}
				return reflect.ValueOf(strconv.FormatInt(from.Int(), 10)), nil
			},
		).
		From(user).
		CopyTo(node)
	if err != nil {
		panic(err)
	}

	fmt.Printf("User %+v\n", user)
	fmt.Printf("Node %+v\n", node)

	// Output:
	// User &{ID:12345 Name:山田太郎 Age:32}
	// Node &{Id:12345 Name:山田太郎 Description:32}
}

Documentation

Overview

Example
package main

import (
	"fmt"
	"reflect"
	"strconv"

	"github.com/structmapper/structmapper"
)

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Age  int32  `json:"age" structmapper:"description"`
}

type Node struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

func main() {
	user := &User{
		ID:   "12345",
		Name: "山田太郎",
		Age:  32,
	}

	node := new(Node)

	err := structmapper.New().
		RegisterTransformer(
			structmapper.Target{
				From: reflect.TypeOf(int32(0)),
				To:   reflect.TypeOf(""),
			},
			func(from reflect.Value, _ reflect.Type) (reflect.Value, error) {
				if !from.IsValid() {
					return reflect.ValueOf(nil), nil
				}
				return reflect.ValueOf(strconv.FormatInt(from.Int(), 10)), nil
			},
		).
		From(user).
		CopyTo(node)
	if err != nil {
		panic(err)
	}

	fmt.Printf("User %+v\n", user)
	fmt.Printf("Node %+v\n", node)

}
Output:

User &{ID:12345 Name:山田太郎 Age:32}
Node &{Id:12345 Name:山田太郎 Description:32}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProtobufModule

func ProtobufModule(m Mapper)

ProtobufModule is Transformer Module of between golang/protobuf/ptypes types and go types

func StringerModule

func StringerModule(m Mapper)

Types

type CopyCommand

type CopyCommand interface {
	// Copy struct to other struct. Field mapping by `structmapper` tag, `json` tag, or field name.
	CopyTo(toValue interface{}) error
}

Copy to ...

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

type Mapper

type Mapper interface {
	// Copy struct to other struct. Field mapping by `structmapper` tag, `json` tag, or field name.
	From(fromValue interface{}) CopyCommand

	// Register Transformer matches by TargerMatcher
	RegisterTransformer(matcher TypeMatcher, transformer Transformer) Mapper

	// Register Transformer matches by TargerMatcher
	RegisterTransformerFunc(matcher TypeMatcherFunc, transformer Transformer) Mapper

	// Install Module
	Install(Module) Mapper

	EnableLogging() Mapper
}

Mapper Struct mapper

func New

func New() Mapper

New Mapper

type Module

type Module func(Mapper)

Mapper installable module

type Target

type Target struct {
	// From type
	From reflect.Type
	// To type
	To reflect.Type
}

Pair of Transformer target

func (Target) Matches

func (t Target) Matches(target Target) bool

Matches of TypeMatcher

func (Target) String

func (t Target) String() string

String of Stringer

type Transformer

type Transformer func(from reflect.Value, toType reflect.Type) (reflect.Value, error)

Value transformer

type TypeMatcher

type TypeMatcher interface {
	// Matches
	Matches(Target) bool
}

Matcher of Transformer target

type TypeMatcherFunc

type TypeMatcherFunc func(Target) bool

TypeMatcher func

func (TypeMatcherFunc) Matches

func (f TypeMatcherFunc) Matches(target Target) bool

Matches of TypeMatcher

Directories

Path Synopsis
test
dto

Jump to

Keyboard shortcuts

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