metadata.go 2.01 KB
Newer Older
1
package eventlog
2 3 4 5 6 7 8 9 10 11 12 13 14 15

import (
	"encoding/json"
	"errors"
	"reflect"

	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
)

// Metadata is a convenience type for generic maps
type Metadata map[string]interface{}

// Loggable describes objects that can be marshalled into Metadata for logging
type Loggable interface {
16
	Loggable() map[string]interface{}
17 18
}

19 20
// Uuid returns a Metadata with the string key and UUID value
func Uuid(key string) Metadata {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	return Metadata{
		key: uuid.New(),
	}
}

// DeepMerge merges the second Metadata parameter into the first.
// Nested Metadata are merged recursively. Primitives are over-written.
func DeepMerge(b, a Metadata) Metadata {
	out := Metadata{}
	for k, v := range b {
		out[k] = v
	}
	for k, v := range a {

		maybe, err := Metadatify(v)
		if err != nil {
			// if the new value is not meta. just overwrite the dest vaue
			out[k] = v
			continue
		}

		// it is meta. What about dest?
		outv, exists := out[k]
		if !exists {
			// the new value is meta, but there's no dest value. just write it
			out[k] = v
			continue
		}

		outMetadataValue, err := Metadatify(outv)
		if err != nil {
			// the new value is meta and there's a dest value, but the dest
			// value isn't meta. just overwrite
			out[k] = v
			continue
		}

		// both are meta. merge them.
		out[k] = DeepMerge(outMetadataValue, maybe)
	}
	return out
}

// Loggable implements the Loggable interface
65
func (m Metadata) Loggable() map[string]interface{} {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	// NB: method defined on value to avoid de-referencing nil Metadata
	return m
}

func (m Metadata) JsonString() (string, error) {
	// NB: method defined on value
	b, err := json.Marshal(m)
	return string(b), err
}

// Metadatify converts maps into Metadata
func Metadatify(i interface{}) (Metadata, error) {
	value := reflect.ValueOf(i)
	if value.Kind() == reflect.Map {
		m := map[string]interface{}{}
		for _, k := range value.MapKeys() {
			m[k.String()] = value.MapIndex(k).Interface()
		}
		return Metadata(m), nil
	}
	return nil, errors.New("is not a map")
}