Example #1
0
func (j *jsonReader) Parse(changes ...*ChangeSet) (*ChangeSet, error) {
	var merged map[string]interface{}

	for _, m := range changes {
		if len(m.Data) == 0 {
			m.Data = []byte(`{}`)
		}

		var data map[string]interface{}
		if err := json.Unmarshal(m.Data, &data); err != nil {
			return nil, err
		}
		if err := mergo.MapWithOverwrite(&merged, data); err != nil {
			return nil, err
		}
	}

	b, err := json.Marshal(merged)
	if err != nil {
		return nil, err
	}

	h, err := hash.Hash(merged, nil)
	if err != nil {
		return nil, err
	}

	return &ChangeSet{
		Timestamp: time.Now(),
		Data:      b,
		Checksum:  fmt.Sprintf("%x", h),
		Source:    "json",
	}, nil
}
// MergeMap TODO: Slow function: It wants to change fast function
func MergeMap(src, dst map[string]interface{}) map[string]interface{} {
	mergo.MapWithOverwrite(&dst, src)
	return dst
}