package utils

type MapHelper struct {
}

func (h *MapHelper) GetString(m map[string]interface{}, key string, defaultVal string) string {
	val, ok := m[key]
	if !ok {
		return defaultVal
	}

	result, ok := val.(string)
	if !ok {
		return defaultVal
	}

	return result
}

func (h *MapHelper) GetData(m map[string]interface{}, key string) []byte {
	val, ok := m[key]
	if !ok {
		return nil
	}

	// TODO maybe there is a better way to serialize and deserialize JSON RPC binary data
	// Go by default does not convert []uint JSON into []byte. Thus the code below
	vals, ok := val.([]interface{})
	if !ok {
		return nil
	}

	var x int64
	result := make([]byte, len(vals))
	for i, value := range vals {
		x = int64(value.(float64))
		result[i] = byte(x)
	}

	return result
}
