Пример #1
0
func valueToTextString(
	value reflect.Value,
	t types.Type,
	u units.Unit,
	isValueAPointer bool) string {
	switch {
	case t == types.Bool:
		if value.Bool() {
			return "true"
		}
		return "false"
	case t.IsInt() && !isValueAPointer:
		return strconv.FormatInt(value.Int(), 10)
	case t.IsUint() && !isValueAPointer:
		return strconv.FormatUint(value.Uint(), 10)
	case t == types.Float32 && !isValueAPointer:
		return strconv.FormatFloat(value.Float(), 'f', -1, 32)
	case t == types.Float64 && !isValueAPointer:
		return strconv.FormatFloat(value.Float(), 'f', -1, 64)
	case t == types.String && !isValueAPointer:
		return "\"" + value.String() + "\""
	case t == types.GoTime:
		return timeToDuration(
			valueToTime(
				value, isValueAPointer)).StringUsingUnits(u)
	case t == types.GoDuration && !isValueAPointer:
		return duration.New(
			valueToGoDuration(value)).StringUsingUnits(u)
	default:
		panic(panicIncompatibleTypes)
	}
}
Пример #2
0
func (v *value) AsDuration(s *session) duration.Duration {
	if v.valType == types.GoTime {
		return timeToDuration(v.AsTime(s))
	}
	if v.valType == types.GoDuration {
		return duration.New(v.AsGoDuration(s))
	}
	panic(panicIncompatibleTypes)
}
Пример #3
0
func valueToHtmlString(
	value reflect.Value,
	t types.Type,
	u units.Unit,
	isValueAPointer bool) string {
	switch {
	case t.IsInt() && !isValueAPointer:
		switch u {
		case units.Byte:
			return iCompactForm(
				value.Int(), 1024, byteSuffixes)
		case units.BytePerSecond:
			return iCompactForm(
				value.Int(), 1024, bytePerSecondSuffixes)
		default:
			return iCompactForm(
				value.Int(), 1000, suffixes)
		}
	case t.IsUint() && !isValueAPointer:
		switch u {
		case units.Byte:
			return uCompactForm(
				value.Uint(), 1024, byteSuffixes)
		case units.BytePerSecond:
			return uCompactForm(
				value.Uint(), 1024, bytePerSecondSuffixes)
		default:
			return uCompactForm(
				value.Uint(), 1000, suffixes)
		}
	case t == types.GoDuration && !isValueAPointer:
		d := duration.New(valueToGoDuration(value))
		if d.IsNegative() {
			return valueToTextString(
				value, t, u, isValueAPointer)
		}
		return d.PrettyFormat()
	case t == types.GoTime:
		t := valueToTime(value, isValueAPointer).UTC()
		return t.Format("2006-01-02T15:04:05.999999999Z")
	default:
		return valueToTextString(value, t, u, isValueAPointer)
	}
}
Пример #4
0
func durationAsString(godur time.Duration, unit units.Unit) string {
	return duration.New(godur).StringUsingUnits(unit)
}