I am writing a rest api. I don't want to use PUT request for update. I will use PATCH. But how to create a dynamic SQL UPDATE query with golang. I use MySQL as a database.
I found this Http://stackoverflow.com/questions/38206479/golang-rest-patch-and-building-an-update-query
But it is working. Do you have any information about the subject?
评论:
bkeroack:
victrolla:If your payloads are JSON you could accept jsonpatch documents as the PATCH request body.
parnacsata:I had no idea Jsonpatch existed. Thanks!
circuitously:PUT or PATCH, it doesn't really matter. It just the method.
You have to think up a logic to handle. Imagine a struct which holds your data model, for a userprofile or a single todo. You have some fields and an id for each row in your DB.
You may require the whole fieldset for one item, then you dont have to do anything: Update all the fields where the id matches.
Or you may want just the id of the item, and the field what you want to modify. Now there is two road you can take. Implement a modify for each field(you can write a smart func for that which does that) or get the data for that id, change that data which was modified and overwrite every field like in the first method I could think of.
If you want to do it really neat, that would require some time.
parnacsata:I mean, technically there are differences between PUT and PATCH. With a PUT you're sending the whole object back, and a PATCH is for amending specific fields in a specific object.
snakegums:That is why I wrote:
it doesn't really matter
tmornini:Its not clear where you got stuck. Are you trying to solve this problem generically in Go? Go does not support generic programming directly. Common alternatives include code generation and runtime reflection.
Not every problem lends itself well to generic programming either. It may be that the best solution is to write code specific to every resource you will handle.
quiI:I don't want to use PUT request for update. I will use PATCH.
Why? You'll just be making the API development and usage more difficult...
google_you:PUT and PATCH have clearly defined different semantics and use cases so its a little odd to just say that PATCH makes usage more difficult
You end up string manipulation to build query, for array fields at least. Just use Mongodb
