-
Notifications
You must be signed in to change notification settings - Fork 51
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
GET_NAME_FIELD_BY_TAG: Added get name of field from it's json tag, ad… #16
Conversation
…ded converting a map to a struct
Hi @Mansouri147 Thanks a lot for the contribution, much appreciated 🙇🏻 |
Thanks a lot 🙌
…On Wed, 31 Aug 2022, 09:53 Théo Crevon, ***@***.***> wrote:
Hi @Mansouri147 <https://github.com/Mansouri147>
Thanks a lot for the contribution, much appreciated 🙇🏻
I'm a bit "under the water" at the moment, but I'll give it a review in
the upcoming days 🌦️ Thanks for your patience!
—
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOZLQMWNONEGK7OAVIPOCJLV34MQPANCNFSM574BU4HQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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 👍🏻
I'd be very curious as to what's the use case for this feature though 🤔 Would you mind sharing in what scenario you actually needed to do that?
I've left some change requests, but I believe it should be somewhat small changes. Namely I'd like the main function prototype to change, its documentation to be somewhat more thorough, and I prefer to not merge the map to struct function as is.
Once again, thanks a lot for your contribution 🙇🏻
reflections.go
Outdated
@@ -91,6 +91,26 @@ func GetFieldTag(obj interface{}, fieldName, tagKey string) (string, error) { | |||
return field.Tag.Get(tagKey), nil | |||
} | |||
|
|||
// Function to get the the Field from it's json tag | |||
func GetNameFieldByTag(obj interface{}, tag string, key string) (string, error) { |
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 would prefer this function to be called GetFieldNameByTagValue
instead. Moreover, I would find it more intuitive if the two last arguments were inverted and renamed for clarity:
func GetFieldNameByTagValue(obj interface{}, tagKey, tagValue string) (string, error)
I also realized that it would be nice to a usage example to the |
…ToStruct and it's test, updated function name, added more test cases in reflections_test.go, added short example in Readme file
Done 💯 💪 👍 |
quite basic usage example: a service written in go for a smart-restaurant that has a conjob that checks for any change in dishes to update it based on some specific conditions, the conditions can be changed from any other service ( using NodeJS, Python… ) // GO type of order
type Order struct {
Step string `json:"order_step"`
Id string `json:"id"`
Category string `json:"category"`
}
// JSON data served to us from external DB or service
order ={
"order_step": "cooking", // we get the name of this field which is in go will be "Step"
"id": "45457-fv54f54",
"category": "Pizzas"
}
condition = {
"field": "order_step", // "step" here is the value not the key
"value": "cooking",
"next": "serve"
}
// returns filedName = "Step"
fieldName, _ := GetFieldNameByTagValue(order, condition.Field, "json")
fieldValue, _ := GetField(order, fieldName) so the name of the field that needs to changed is the value not the key we need to get the key from value stored as JSON here we need to get the external services doesn’t need to pay attention for how they are in go they just save it as normal JSON Note I've added more explained example in |
Thanks for the effort in the review 🚀 🙌 |
Done.
…On Sun, Sep 4, 2022 at 9:11 AM Théo Crevon ***@***.***> wrote:
I also realized that it would be nice to a usage example to the
example_test.go file 🙇🏻
—
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOZLQMTQBYTI6KFWYQQAX3TV4RKRJANCNFSM574BU4HQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
…ded converting a map to a struct