Skip to content

IQ-tech/go-mapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mapper packages

This package holds helpers for better parses among structs.

New() Mapper

This method reutrns a new instance of Mapper.

Mapper types

Mapper interface

This interface contains one method only:

// From creates map from source
type Mapper interface {
    From(src interface{}) Result
}

Result interface

This interface contains two methods only:

// From creates map from source
type Result interface {
    Merge(src interface{}) Result
    To(tgr interface{}) error
}

From(src interface{}) Result

This method returns the map Result interface.

Example:

// mapping some entity
result := mapper.From(entity)

Merge(src interface{}) Result

This method returns the merged map Result interface.

// merging some entity
result := result.Merge(entity)

To(tgr interface{}) error

This method receive a pointer as parameter and returns error.

// mapping to some target
err := result.To(&target)
if err != nil {
    fmt.Println(err)
}

Note: You can use this methods combined.

Example

err := mapper.From(source).To(&target)
if err != nil {
    fmt.Println(err)
}

TAG mapper

The tag mapper allow you mapping fields with diferents names.

*Example Mapping field Sources from Source to Targes from Target

type Source struct {
		Name    string
		List    []string
		Sources []Source
	}

	type Target struct {
		Name    string
		List    []string
		Targets []Target `mapper:"Sources"`
	}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages