oleiade / reflections

High level abstractions over the Go reflect library
https://pkg.go.dev/github.com/oleiade/reflections
MIT License
524 stars 51 forks source link

GET_NAME_FIELD_BY_TAG: Added get name of field from it's json tag, ad… #16

Closed Mansouri147 closed 2 years ago

Mansouri147 commented 2 years ago

…ded converting a map to a struct

oleiade commented 2 years ago

Hi @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!

Mansouri147 commented 2 years ago

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 https://github.com/oleiade/reflections/pull/16#issuecomment-1232656551, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOZLQMWNONEGK7OAVIPOCJLV34MQPANCNFSM574BU4HQ . You are receiving this because you were mentioned.Message ID: @.***>

oleiade commented 2 years ago

I also realized that it would be nice to a usage example to the example_test.go file 🙇🏻

Mansouri147 commented 2 years ago

Done :100: :muscle: :+1:

Mansouri147 commented 2 years ago

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 "cooking" from order json GetField(order, GetFieldNameByTagValue(order, condition.Field, "json"))

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 example_test.go

Mansouri147 commented 2 years ago

Thanks for the effort in the review :rocket: :raised_hands:

Mansouri147 commented 2 years ago

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 https://github.com/oleiade/reflections/pull/16#issuecomment-1236285402, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOZLQMTQBYTI6KFWYQQAX3TV4RKRJANCNFSM574BU4HQ . You are receiving this because you were mentioned.Message ID: @.***>