-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New Feature: Add TypeWrapper used to register custom Type Checker * New Feature: Add UseWrapper used to add your TypeWrapper * Update: remove isTimeField, add TimeWrapper * Detail: - now only support IsType used to check type * 2019-02-03 16:00
- Loading branch information
Showing
6 changed files
with
126 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/devfeel/mapper" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
type ( | ||
User struct { | ||
Name string | ||
Age int | ||
Score decimal | ||
Time time.Time | ||
} | ||
|
||
Student struct { | ||
Name string | ||
Age int | ||
Score decimal | ||
Time int64 | ||
} | ||
) | ||
|
||
type decimal struct { | ||
value float32 | ||
} | ||
|
||
type DecimalWrapper struct { | ||
mapper.BaseTypeWrapper | ||
} | ||
|
||
func (w *DecimalWrapper) IsType(value reflect.Value) bool { | ||
if _, ok := value.Interface().(decimal); ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func main() { | ||
mapper.UseWrapper(&DecimalWrapper{}) | ||
user := &User{Name: "test", Age: 10, Score: decimal{value: 1}, Time: time.Now()} | ||
stu := &Student{} | ||
|
||
mapper.AutoMapper(user, stu) | ||
|
||
fmt.Println(user) | ||
fmt.Println(stu) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package mapper | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
) | ||
|
||
type TypeWrapper interface { | ||
IsType(value reflect.Value) bool | ||
SetNext(m TypeWrapper) | ||
} | ||
|
||
type BaseTypeWrapper struct { | ||
next TypeWrapper | ||
} | ||
|
||
func (bm *BaseTypeWrapper) SetNext(m TypeWrapper) { | ||
bm.next = m | ||
} | ||
|
||
type TimeWrapper struct { | ||
BaseTypeWrapper | ||
} | ||
|
||
func (w *TimeWrapper) IsType(value reflect.Value) bool { | ||
if _, ok := value.Interface().(time.Time); ok { | ||
return true | ||
} | ||
if _, ok := value.Interface().(JSONTime); ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func NewTimeWrapper() *TimeWrapper { | ||
return &TimeWrapper{} | ||
} |