Hacker News new | ask | show | jobs
by troyk 4400 days ago
Seems like the issue is trying to keep type safety when doing a PATCH so instead of polluting the struct with pointers so it can be used to do a PATCH, why not create a separate type specific to that purpose:

  type Repository struct {
      Name        string `json:"name"`
      Description string `json:"description"`
      Private     bool   `json:"private"`
  }

  // response to blog post https://willnorris.com/2014/05/go-rest-apis-and-pointers

  type RepositoryPatch struct {
    Repository
    patched_fields []string
  }

  func (rp *RepositoryPatch) SetPrivate(private bool) {
    rp.patch_fields = append(rp.patch_fields,"private")
    rp.Private = private
  }

  func (rp *RepositoryPatch) MarshalJSON() ([]byte, error) {
    // marshal rp to []byte then unmarshal to map[string]interface{}
    // filter patched_fields and then marshal again
    // ;)
  }
1 comments

yeah, that's similar to the field mask that was discussed on the issue (https://github.com/google/go-github/issues/19#issuecomment-2...), though your approach does provide some nice encapsulation. Given how large some of the GitHub data types are, you'd probably want to auto-generate the setter methods, similar to what goprotobuf does for accessor methods.