Created
May 29, 2014 17:38
-
-
Save troyk/896c045094ec93c1578b to your computer and use it in GitHub Desktop.
idea to avoid using pointers in go structs just for happy patching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
// ;) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment