title: Best Practices For Ignoring Nil Values in Firestore
body: <p>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....</p>
  
  <p><strong>Problem Statement</strong>: Nil values are overwriting existing values in firebase when I write to the database </p>
  
  <p><strong>Proposed Solution</strong> - 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.</p>
  
  <p>Is this the best way?   Are their other alternatives?   The default values of golang seems to have pluses and minuses.  </p>
  
  <p>UPDATE: </p>
  
  <p>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.</p>
  
  <p>Here is the function that I am using to call firestore.</p>
  
  <pre><code>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
  } 
  </code></pre>
  
  <p>I also create a firestore.Update object and will append that to my []firestore.Update slice.</p>
  
  <pre><code>firestore.Update{Path: "PATH_TO_ATTR", Value: VALUE_TO_PASS_IN}
  </code></pre>