|
|
|
|
|
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
// ;)
}
|
|