dongzi0857 2019-01-02 19:24
浏览 25
已采纳

在Firestore中忽略Nil值的最佳做法

I am building custom webservices using Golang (Go-Swagger for API Definition) and Firestore as my document store. Everything is working fine except it gets "awkward" on Updates to the database. I have a solution but I am not convinced it is the most elegant one. Has anyone else run into this, and how did they solve it....

Problem Statement: Nil values are overwriting existing values in firebase when I write to the database

Proposed Solution - MY API has json:"omitempty" on optional parameters and nil values will be passed in (as opposed to getting the default golang values). I am planning on taking the Struct passed into the API and one attr at a time populating it into my Struct that firestore is using. I have performance concerns but don't want to couple my API Struct with my DB Struct. I am also going to set the firestore:"omitempty"` string literal on my firestore struct much like the API Struct. I hope this will tell firestore to not write any values that are nil.

Is this the best way? Are their other alternatives? The default values of golang seems to have pluses and minuses.

UPDATE:

I ended up in a similar place as my proposed solution but also followed the suggestion that Dimitry suggested. I created an []firestore.Update object to contain only the values that I want to update. When I called firestore it will only update those values.

Here is the function that I am using to call firestore.

func updateProfileUsingUpdateMethod(documentName string, values []firestore.Update) error {
    ctx := context.Background()
    app := firestorehelper.GetFirestoreApp()

    client, err := app.Firestore(ctx)
    if err != nil {
        return err
    }
    defer client.Close()

    //Set the updated date

    wr, error := client.Doc(collectionName+"/"+documentName).Update(ctx, values)
    if error != nil {
        return error
    }
    fmt.Println(wr.UpdateTime)
    //Assume success
    return nil
} 

I also create a firestore.Update object and will append that to my []firestore.Update slice.

firestore.Update{Path: "PATH_TO_ATTR", Value: VALUE_TO_PASS_IN}
  • 写回答

1条回答 默认 最新

  • douwen3127 2019-01-02 20:27
    关注

    You have two options:

    1. Use map[string]interface{}
    2. Use custom structure with firestore attribute -firestore:"FieldName,omitempty".

      _, err := client.Collection("cities").Doc("DC").Set(ctx, map[string]interface{}{ "capital": true, }, firestore.MergeAll) if err != nil { // Handle any errors in an appropriate way, such as returning them. log.Printf("An error has occurred: %s", err) }

    Do not confuse json omitempty with firestore.

    You also must to provide field name as this syntax will use omitempty as name.

    firestore:"omitempty" - not correct
    

    https://firebase.google.com/docs/firestore/manage-data/add-data

    code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”