Пример #1
0
func (format Format) unmarshalTime(field string) func([]byte) (time.Time, error) {
	switch format {
	case "json":
		return func(msg []byte) (time.Time, error) {
			m := make(map[string]interface{})
			if err := json.Unmarshal(msg, &m); err != nil {
				return time.Time{}, err
			}
			timeField, _ := m[field].(string)
			return parseTime(timeField)
		}
	case "url":
		return func(msg []byte) (time.Time, error) {
			values, err := url.ParseQuery(string(msg))
			if err != nil {
				return time.Time{}, err
			}
			return parseTime(values.Get(field))
		}
	case "msgpack":
		return func(msg []byte) (time.Time, error) {
			m := make(map[string]interface{})
			if err := msgpack.Unmarshal(msg, &m); err != nil {
				return time.Time{}, err
			}
			m = hexid.Restore(m).(map[string]interface{})
			return parseTime(m[field])
		}
	}
	return nil
}
Пример #2
0
func (format Format) Sprint(value []byte) (string, error) {
	switch format {
	case MsgPackFormat:
		m := make(map[string]interface{})
		if err := msgpack.Unmarshal(value, &m); err != nil {
			break
		}
		buf, err := json.Marshal(hexid.Restore(m))
		if err != nil {
			return "", fmt.Errorf("fail to marshal %#v: %s", m, err.Error())
		}
		return string(buf), nil
	}
	return string(value), nil
}