Example #1
0
// createMapResponse is a helper for generating a response value from
// a value of type map.
func createMapResponse(value reflect.Value, options objx.Map, constructor func(interface{}, interface{}) interface{}, domain string) interface{} {
	response := reflect.MakeMap(value.Type())
	for _, key := range value.MapKeys() {
		var elementOptions objx.Map
		keyStr := key.Interface().(string)
		if options != nil {
			var elementOptionsValue *objx.Value
			if options.Has(keyStr) {
				elementOptionsValue = options.Get(keyStr)
			} else if options.Has("*") {
				elementOptionsValue = options.Get("*")
			}
			if elementOptionsValue.IsMSI() {
				elementOptions = objx.Map(elementOptionsValue.MSI())
			} else if elementOptionsValue.IsObjxMap() {
				elementOptions = elementOptionsValue.ObjxMap()
			} else {
				panic("Don't know what to do with option")
			}
		}
		itemResponse := createResponseValue(value.MapIndex(key), elementOptions, constructor, domain)
		response.SetMapIndex(key, reflect.ValueOf(itemResponse))
	}
	return response.Interface()
}
Example #2
0
// createStructResponse is a helper for generating a response value
// from a value of type struct.
func createStructResponse(value reflect.Value, options objx.Map, constructor func(interface{}, interface{}) interface{}, domain string) interface{} {
	structType := value.Type()

	// Support "database/sql".Null* types, and any other types
	// matching that structure
	if v, err := createNullableDbResponse(value, structType); err == nil {
		return v
	}

	response := make(objx.Map)

	for i := 0; i < value.NumField(); i++ {
		fieldType := structType.Field(i)
		fieldValue := value.Field(i)

		if fieldType.Anonymous {
			embeddedResponse := CreateResponse(fieldValue.Interface(), options, constructor, domain).(objx.Map)
			for key, value := range embeddedResponse {
				// Don't overwrite values from the base struct
				if _, ok := response[key]; !ok {
					response[key] = value
				}
			}
		} else if unicode.IsUpper(rune(fieldType.Name[0])) {
			name := ResponseTag(fieldType)
			switch name {
			case "-":
				continue
			default:
				var subOptions objx.Map
				if options != nil && (options.Has(name) || options.Has("*")) {
					var subOptionsValue *objx.Value
					if options.Has(name) {
						subOptionsValue = options.Get(name)
					} else {
						subOptionsValue = options.Get("*")
					}
					if subOptionsValue.IsMSI() {
						subOptions = objx.Map(subOptionsValue.MSI())
					} else if subOptionsValue.IsObjxMap() {
						subOptions = subOptionsValue.ObjxMap()
					} else {
						panic("Don't know what to do with option")
					}
				}
				response[name] = createResponseValue(fieldValue, subOptions, constructor, domain)
			}
		}
	}
	return response
}
Example #3
0
func (t *Template) castValueToString(value *objx.Value) string {
	switch {
	case value.IsStr():
		return value.Str()
	case value.IsBool():
		return strconv.FormatBool(value.Bool())
	case value.IsFloat32():
		return strconv.FormatFloat(float64(value.Float32()), 'f', -1, 32)
	case value.IsFloat64():
		return strconv.FormatFloat(value.Float64(), 'f', -1, 64)
	case value.IsInt():
		return strconv.FormatInt(int64(value.Int()), 10)
	case value.IsInt():
		return strconv.FormatInt(int64(value.Int()), 10)
	case value.IsInt8():
		return strconv.FormatInt(int64(value.Int8()), 10)
	case value.IsInt16():
		return strconv.FormatInt(int64(value.Int16()), 10)
	case value.IsInt32():
		return strconv.FormatInt(int64(value.Int32()), 10)
	case value.IsInt64():
		return strconv.FormatInt(value.Int64(), 10)
	case value.IsUint():
		return strconv.FormatUint(uint64(value.Uint()), 10)
	case value.IsUint8():
		return strconv.FormatUint(uint64(value.Uint8()), 10)
	case value.IsUint16():
		return strconv.FormatUint(uint64(value.Uint16()), 10)
	case value.IsUint32():
		return strconv.FormatUint(uint64(value.Uint32()), 10)
	case value.IsUint64():
		return strconv.FormatUint(value.Uint64(), 10)
	}

	return ""
}