golang json

    科技2022-07-12  137

    golang json

    介绍 (Introduction)

    HTTP is the widely used protocol for fetching resources over the web. It’s the building block for any data exchange that happens over the world wide web. Several applications that use HTTP protocol for accessing and storing resources needs a feature to partially modify the resources.

    HTTP是广泛使用的协议,用于通过Web获取资源。 这是在互联网上进行任何数据交换的基础。 一些使用HTTP协议访问和存储资源的应用程序需要一种功能来部分修改资源。

    HTTP supports a PUT method that does a complete replacement of the resource. It overrides the resource with a completely new body and cannot be re-purposed for partial modification of resources.

    HTTP支持可以完全替换资源的PUT方法。 它以全新的主体覆盖资源,并且不能重新用于部分修改资源。

    In this article, we will learn how to apply a JSON patch to the existing resource in conjunction with the HTTP PATCH method in GoLang.

    在本文中,我们将学习如何结合GoLang中的HTTP PATCH方法将JSON补丁应用于现有资源。

    概念 (Concept)

    The JSON patch document can be used to partially update a resource. For instance, if you only need to modify one field of the resource, sending the entire new resource with just one field modified is not going to be a good option.

    JSON修补程序文档可用于部分更新资源。 例如,如果您只需要修改资源的一个字段,那么仅修改一个字段就发送整个新资源将不是一个好的选择。

    Considering we are going to make a remote call, PUTting a complete resource representation is going to utilize more bandwidth and might be a cumbersome job.

    考虑到我们将要进行远程调用,因此将完整的资源表示形式放到PUT占用更多带宽,这可能会很麻烦。

    JSON PATCH文件 (JSON PATCH document)

    JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. The PATCH document is itself a JSON document.

    JSON修补程序是一种用于描述对JSON文档所做的更改的格式 。 当只更改了一部分时,可用于避免发送整个文档。 PATCH文档本身就是JSON文档。

    When used in combination with the HTTP PATCH method, it allows partial updates for HTTP APIs in a standards compliant way.

    与HTTP PATCH方法结合使用时,它允许以符合标准的方式对HTTP API进行部分更新。

    Sample JSON patch document.

    样本JSON补丁文档。

    [ {"op": "replace", "path": "/foo", "value": "Hello"}, {"op": "remove", "path": "/phi"}]

    We specify the operation as op , the field which needs to be updated as path and the new value using value . The above keywords are compliant with the RFC6902 standard. This is just a sample JSON document. Refer to JavaScript Object Notation (JSON) Patch for a detailed list.

    我们将操作指定为op ,需要将其更新为path的字段,并使用value来value新value 。 以上关键字符合RFC6902标准。 这只是一个示例JSON文档。 有关详细列表,请参阅JavaScript Object Notation(JSON)修补程序。

    PUT vs PATCH方法 (PUT vs PATCH method)

    Here are a few key differences between HTTP PUT and PATCH method.

    这是HTTP PUT和PATCH方法之间的一些主要区别。

    PUT contain a “new representation” of a resource, while PATCH contains “a set of changes” to a resource.

    PUT包含资源的“新表示” ,而PATCH包含资源的“一组更改” 。

    PUT is idempotent while PATCH is non-idempotent.

    PUT是幂等的,而PATCH 是非幂等的 。

    PUT ensures safe updates to resources, while PATCH is not safe to update, we need to make sure the PATCH operation is atomic, to avoid producing partially patched resources.

    PUT确保安全更新资源,而PATCH不安全更新,我们需要确保PATCH操作是原子的,以避免产生部分修补的资源。

    “Just because something doesn’t do what you planned it to do doesn’t mean it’s useless. “ — Thomas Edison “仅仅因为某件事没有按照您的计划去做并不意味着它就没有用。 “ - 托马斯·爱迪生

    建立 (Setup)

    We are going to use Go’s jsonpatch library for creating and merging JSON patches.

    我们将使用Go的jsonpatch库创建和合并JSON补丁。

    go get -v github.com/evanphx/json-patch/v5

    Using mux library in Golang for creating the endpoints.

    在Golang中使用mux库创建端点。

    go get -v github.com/gorilla/mux

    实作 (Implementation)

    We will cover two ways of updating a resource:

    我们将介绍两种更新资源的方法:

    Update using the JSON PATCH Document.

    使用JSON PATCH Document更新。

    Update by merging the PATCH JSON with the original JSON.

    通过将 PATCH JSON与原始JSON 合并进行更新。

    Essentially, it means either we can pass a JSON patch document that will have the instructions for updating the resource as part of an HTTP request or we can send the patched JSON directly as part of the request which can be directly merged with the original JSON.

    从本质上讲,这意味着要么我们可以传递一个JSON修补程序文档,其中将包含更新资源的说明作为HTTP请求的一部分,要么我们可以直接将修补后的JSON发送为请求的一部分,而该请求可以与原始JSON直接合并。

    使用JSON PATCH文档进行更新 (Update using the JSON PATCH Document)

    We send the instructions for updating a resource as part of the request in the form JSON patch document. It involves decoding the JSON patch document and applying it to the original JSON.

    作为请求的一部分,我们以JSON补丁文档的形式发送了用于更新资源的说明。 它涉及解码JSON修补程序文档并将其应用于原始JSON。

    Let’s define a Model which can be used for storing the data. Consider a simple scenario where we want to maintain the records of all movies which are released worldwide. The model will have the following fields.

    让我们定义一个Model可以用来存储数据。 考虑一个简单的情况,我们要维护在全球发行的所有电影的记录。 该模型将具有以下字段。

    We define additional JSON tags with each field json:"key,omitempty" . The tag key will be used as field name while Marshalling the object to JSON, omitempty avoids empty fields from marshaling to JSON.

    我们使用每个字段json:"key,omitempty"定义其他JSON标签。 在将对象编组为JSON时,标记键将用作字段名,而omitempty可以避免将空字段编组为JSON。

    “The desire to create is one of the deepest yearnings of the human soul.” — Dieter F. Uchtdorf “创造的欲望是人类灵魂最深切的向往之一。” — Dieter F. Uchtdorf

    We expect the rest of the fields to remain the same while gross can change with time. Let’s create a JSON patch document for updating the gross value and also get rid of the releasedOn field from the final JSON.

    我们希望其余字段保持不变,但gross会随着时间而变化。 让我们创建一个JSON文件的补丁更新的gross价值,也摆脱了releasedOn从最终JSON场。

    [ {"op": "replace", "path": "/gross", "value": "$3.03B"}, {"op": "remove", "path": "/releasedOn"}]

    Let’s initialize the Movie object. We will set the initial gross value to $2.768B. applyPatch method will extract the PATCH document from the request, decode the instruction, and apply it to the original JSON.

    让我们初始化Movie对象。 我们会将初始gross价值设置为$ 2.768B。 applyPatch方法将从请求中提取PATCH文档,对指令进行解码,然后将其应用于原始JSON。

    Let’s make a request to the patch endpoint, we expect the value of gross to change to $3.03B and the field releasedOn to be removed from the original JSON.

    让我们到一个请求patch终点,我们期待的价值gross更改为$ 3.03B和现场releasedOn从原来的JSON删除。

    Here’s the POSTMAN call to the service.

    这是对服务的POSTMAN调用。

    通过将 PATCH JSON与原始JSON 合并进行更新 (Update by merging the PATCH JSON with the original JSON)

    Let’s implement another way of achieving the same. But, this time instead of passing the instructions, we are going to pass the patched JSON that can be merged with the original JSON. I personally prefer this way as it helps us to avoid knowing the available instructions with RFC6902 standards. We can simply pass the resource fields that need to be updated as part of the request and can merge this with the original JSON.

    让我们实现另一种实现方法。 但是,这次我们没有传递指令,而是传递了可以与原始JSON合并的修补JSON。 我个人更喜欢这种方式,因为它可以帮助我们避免了解RFC6902标准的可用说明。 我们可以简单地将需要更新的资源字段作为请求的一部分传递,并将其与原始JSON合并。

    Let’s make a service call to the merge endpoint with the following request body.

    让我们使用以下请求正文对merge端点进行服务调用。

    { "name": "Avengers End Game", "gross": "$4.09B"}

    We expect the gross to be changed to $4.09B.

    我们预计gross金额将更改为$ 4.09B。

    路线 (Routes)

    Let’s configure the routes for calling the endpoints.

    让我们配置调用端点的路由。

    “Nature is pleased with simplicity. And nature is no dummy.” — Isaac Newton “自然对简单感到满意。 自然不是假的。” - 艾萨克·牛顿

    最后 (Finally)

    Here’s the complete code. Do give it a try!!

    这是完整的代码。 尝试一下!!

    非常感谢!!! (Thank You So Much!!!)

    I hope you found this article helpful. Thanks for taking out time to read it.Happy Coding!!! 😍

    希望本文对您有所帮助。 感谢您抽出宝贵的时间阅读它。 😍

    翻译自: https://medium.com/@mehtachetan33/creating-rfc6902-json-patches-in-golang-with-http-patch-method-357feb0dfdd0

    golang json

    相关资源:jsondiff:基于RFC6902(JSON补丁)的Go JSON diff库-源码
    Processed: 0.011, SQL: 8