mercredi 23 septembre 2020

Golang Need Custom Json layer in Rest Api Where Client send json format he want to receive in result

i need custome view json layer in my rest api what it does is client send the json format in which he want to recieve result e.g i have author table(name, age, id, ,city, books relation , ...) and book table(title, description, id , created_at, updated_at ,....) they both have some fields. Client only need some field not all like just author name and book title. Two case here either client send format of json or we set default json format in server.(its like graphql but not completely we have many endpoint, we just need to control our json response or make it flexible )

i used gorm (ORM) which return struct

So how i implement it.

In my research i need runtime struct made from the client given json format . So we marshal gorm struct to json and unmarshall json to our runtime struct OR convert gorm struct to map and manuplate it with the use of reflect Any Idea suggestion how to implement it well in go

For Example Client send json format like this

{
    author{
     name,
     []Book{
       title
      }
     }
   }

We send

{
    author{
     name: "John",
     Books:[
       {title: "Last Words"}, {title: "Good By"}
      ]
     }
   }



Aucun commentaire:

Enregistrer un commentaire