Skip to content
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

Merged
merged 1 commit into from
May 30, 2022

Conversation

veezhang
Copy link
Contributor

@veezhang veezhang commented May 28, 2022

  • use StandardHandlerDataFieldAny to response any data.
  • 100% test coverage
// StandardHandlerDataFieldAny is to solve the problem that interface{} cannot be directly returned as the data field.
// For examples:
// 	var data interface{} = ...
// 	return &XxxResp {
//      Data: data,
// 	}
// The response body is:
// 	{
//      "code": 0,
//      "message": "Success",
//      "data": {
//          "data": ...
//      }
//  }
//
// Once you use StandardHandlerDataFieldAny,
// 	var data interface{} = ...
// 	return &XxxResp {
//      Data: StandardHandlerDataFieldAny(data),
// 	}
// The response body is:
// 	{
//      "code": 0,
//      "message": "Success",
//      "data": ...
//  }

@veezhang veezhang requested a review from nianiaJR May 28, 2022 16:55
@veezhang
Copy link
Contributor Author

fix #19

@veezhang
Copy link
Contributor Author

@hetao92 This may help you.

@codecov-commenter
Copy link

Codecov Report

Merging #20 (95f6ed1) into master (5ce93ad) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master       #20   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           16        16           
  Lines          769       795   +26     
=========================================
+ Hits           769       795   +26     
Impacted Files Coverage Δ
response/standard_handler.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ce93ad...95f6ed1. Read the comment docs.

Comment on lines +134 to +185
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
}

Copy link
Contributor

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 ?

Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@nianiaJR nianiaJR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@veezhang veezhang merged commit 17c1bd2 into vesoft-inc:master May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants