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 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
type Transformer ¶
Value transformer
type TypeMatcher ¶
Matcher of Transformer target
type TypeMatcherFunc ¶
TypeMatcher func
func (TypeMatcherFunc) Matches ¶
func (f TypeMatcherFunc) Matches(target Target) bool
Matches of TypeMatcher
Source Files ¶
Click to show internal directories.
Click to hide internal directories.