Example #1
0
// Create creates a new object on the server.  The new object will consist
// of the results of merging Init(o) with toMerge.  This will ensure that
// all of the otherwise unused fields on the object will be set to their defaults.
func Create(o Crudder, toMerge interface{}) error {
	if err := Init(o); err != nil {
		return err
	}
	var initBuf interface{}
	if err := utils.Remarshal(o, &initBuf); err != nil {
		return err
	}
	merged := utils.Merge(initBuf, toMerge)
	if err := utils.Remarshal(merged, o); err != nil {
		return err
	}
	return BaseCreate(o)
}
Example #2
0
func safeMergeJSON(target, toMerge []byte) ([]byte, error) {
	targetObj := make(map[string]interface{})
	toMergeObj := make(map[string]interface{})
	if err := json.Unmarshal(target, &targetObj); err != nil {
		return nil, err
	}
	if err := json.Unmarshal(toMerge, &toMergeObj); err != nil {
		return nil, err
	}
	outObj, ok := utils.Merge(targetObj, toMergeObj).(map[string]interface{})
	if !ok {
		return nil, errors.New("Cannot happen in safeMergeJSON")
	}
	keys := make([]string, 0)
	for k := range outObj {
		if _, ok := targetObj[k]; !ok {
			keys = append(keys, k)
		}
	}
	for _, k := range keys {
		delete(outObj, k)
	}
	return json.Marshal(outObj)
}