-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response: support use any to data field #20
Conversation
fix #19 |
@hetao92 This may help you. |
Codecov Report
@@ Coverage Diff @@
## master #20 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 16 16
Lines 769 795 +26
=========================================
+ Hits 769 795 +26
Continue to review full report at Codecov.
|
func (*standardHandler) getData(data interface{}) interface{} { | ||
if data == nil { | ||
return nil | ||
} | ||
if v, ok := data.(*standardHandlerDataFieldAny); ok { | ||
return v.data | ||
} | ||
|
||
reflectType := reflect.TypeOf(data) | ||
reflectValue := reflect.Indirect(reflect.ValueOf(data)) | ||
if reflectType.Kind() == reflect.Ptr { | ||
reflectType = reflectType.Elem() | ||
} | ||
if reflectType.Kind() != reflect.Struct || reflectType.NumField() != 1 { | ||
return data | ||
} | ||
field := reflectValue.Field(0) | ||
if !field.CanInterface() { | ||
return data | ||
} | ||
if v, ok := field.Interface().(*standardHandlerDataFieldAny); ok { | ||
return v.data | ||
} | ||
return data | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can give a simple example to help other new Goers to understand the code ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add some comments.
if reflectType.Kind() != reflect.Struct || reflectType.NumField() != 1 { | ||
return data | ||
} | ||
field := reflectValue.Field(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the first field of the data? why just judge the first one enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is more than one field, it will not be processed here, and the original one will be returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
StandardHandlerDataFieldAny
to response any data.