func mapForm(formStruct reflect.Value, form map[string][]string,
	formfile map[string][]*multipart.FileHeader, errors Errors) {

	if formStruct.Kind() == reflect.Ptr {
		formStruct = formStruct.Elem()
	}
	typ := formStruct.Type()

	for i := 0; i < typ.NumField(); i++ {
		typeField := typ.Field(i)
		structField := formStruct.Field(i)

		if typeField.Type.Kind() == reflect.Ptr && typeField.Anonymous {
			structField.Set(reflect.New(typeField.Type.Elem()))
			mapForm(structField.Elem(), form, formfile, errors)
			if reflect.DeepEqual(structField.Elem().Interface(), reflect.Zero(structField.Elem().Type()).Interface()) {
				structField.Set(reflect.Zero(structField.Type()))
			}
		} else if typeField.Type.Kind() == reflect.Struct {
			mapForm(structField, form, formfile, errors)
		} else if inputFieldName := typeField.Tag.Get("form"); inputFieldName != "" {
			if !structField.CanSet() {
				continue
			}

			inputValue, exists := form[inputFieldName]
			inputFile, existsFile := formfile[inputFieldName]
			if exists && !existsFile {
				numElems := len(inputValue)
				if structField.Kind() == reflect.Slice && numElems > 0 {
					sliceOf := structField.Type().Elem().Kind()
					slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
					for i := 0; i < numElems; i++ {
						setWithProperType(sliceOf, inputValue[i], slice.Index(i), inputFieldName, errors)
					}
					formStruct.Field(i).Set(slice)
				} else {
					kind := typeField.Type.Kind()
					if structField.Kind() == reflect.Ptr {
						structField.Set(reflect.New(typeField.Type.Elem()))
						structField = structField.Elem()
						kind = typeField.Type.Elem().Kind()
					}
					setWithProperType(kind, inputValue[0], structField, inputFieldName, errors)
				}
				continue
			}

			if !existsFile {
				continue
			}
			fhType := reflect.TypeOf((*multipart.FileHeader)(nil))
			numElems := len(inputFile)
			if structField.Kind() == reflect.Slice && numElems > 0 && structField.Type().Elem() == fhType {
				slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
				for i := 0; i < numElems; i++ {
					slice.Index(i).Set(reflect.ValueOf(inputFile[i]))
				}
				structField.Set(slice)
			} else if structField.Type() == fhType {
				structField.Set(reflect.ValueOf(inputFile[0]))
			}
		}
	}
}
func mapForm(formStruct reflect.Value, form map[string][]string,
	formfile map[string][]*multipart.FileHeader, errors Errors) {

	if formStruct.Kind() == reflect.Ptr {
		formStruct = formStruct.Elem()
	}
	typ := formStruct.Type()

	for i := 0; i < typ.NumField(); i++ {
		typeField := typ.Field(i)
		structField := formStruct.Field(i)

		if typeField.Type.Kind() == reflect.Ptr && typeField.Anonymous {
			structField.Set(reflect.New(typeField.Type.Elem()))
			mapForm(structField.Elem(), form, formfile, errors)
			if reflect.DeepEqual(structField.Elem().Interface(), reflect.Zero(structField.Elem().Type()).Interface()) {
				structField.Set(reflect.Zero(structField.Type()))
			}
		} else if typeField.Type.Kind() == reflect.Struct {
			mapForm(structField, form, formfile, errors)
		} else if inputFieldName := typeField.Tag.Get("form"); inputFieldName != "" {
			if !structField.CanSet() {
				continue
			}

			inputValue, exists := form[inputFieldName]
			inputFile, existsFile := formfile[inputFieldName]
			if exists && !existsFile {
				numElems := len(inputValue)
				if structField.Kind() == reflect.Slice && numElems > 0 {
					sliceOf := structField.Type().Elem().Kind()
					slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
					for i := 0; i < numElems; i++ {
						setWithProperType(sliceOf, inputValue[i], slice.Index(i), inputFieldName, errors)
					}
					formStruct.Field(i).Set(slice)
				} else {
					kind := typeField.Type.Kind()
					if structField.Kind() == reflect.Ptr {
						structField.Set(reflect.New(typeField.Type.Elem()))
						structField = structField.Elem()
						kind = typeField.Type.Elem().Kind()
					}
					setWithProperType(kind, inputValue[0], structField, inputFieldName, errors)
				}
				continue
			}

			if !existsFile {
				continue
			}
			fhType := reflect.TypeOf((*multipart.FileHeader)(nil))
			numElems := len(inputFile)
			if structField.Kind() == reflect.Slice && numElems > 0 && structField.Type().Elem() == fhType {
				slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
				for i := 0; i < numElems; i++ {
					slice.Index(i).Set(reflect.ValueOf(inputFile[i]))
				}
				structField.Set(slice)
			} else if structField.Type() == fhType {
				structField.Set(reflect.ValueOf(inputFile[0]))
			}
		}
	}
}
func mapForm ( formStruct reflect . Value , form map [ string ] [ ] string , formfile map [ string ] [ ] * multipart . FileHeader , errors Errors ) { if formStruct . Kind ( ) == reflect . Ptr { formStruct = formStruct . Elem ( ) } typ := formStruct . Type ( ) for i := 0 ; i < typ . NumField ( ) ; i ++ { typeField := typ . Field ( i ) structField := formStruct . Field ( i ) if typeField . Type . Kind ( ) == reflect . Ptr && typeField . Anonymous { structField . Set ( reflect . New ( typeField . Type . Elem ( ) ) ) mapForm ( structField . Elem ( ) , form , formfile , errors ) if reflect . DeepEqual ( structField . Elem ( ) . Interface ( ) , reflect . Zero ( structField . Elem ( ) . Type ( ) ) . Interface ( ) ) { structField . Set ( reflect . Zero ( structField . Type ( ) ) ) } } else if typeField . Type . Kind ( ) == reflect . Struct { mapForm ( structField , form , formfile , errors ) } else if inputFieldName := typeField . Tag . Get ( " " ) ; inputFieldName != " " { if ! structField . CanSet ( ) { continue } inputValue , exists := form [ inputFieldName ] inputFile , existsFile := formfile [ inputFieldName ] if exists && ! existsFile { numElems := len ( inputValue ) if structField . Kind ( ) == reflect . Slice && numElems > 0 { sliceOf := structField . Type ( ) . Elem ( ) . Kind ( ) slice := reflect . MakeSlice ( structField . Type ( ) , numElems , numElems ) for i := 0 ; i < numElems ; i ++ { setWithProperType ( sliceOf , inputValue [ i ] , slice . Index ( i ) , inputFieldName , errors ) } formStruct . Field ( i ) . Set ( slice ) } else { kind := typeField . Type . Kind ( ) if structField . Kind ( ) == reflect . Ptr { structField . Set ( reflect . New ( typeField . Type . Elem ( ) ) ) structField = structField . Elem ( ) kind = typeField . Type . Elem ( ) . Kind ( ) } setWithProperType ( kind , inputValue [ 0 ] , structField , inputFieldName , errors ) } continue } if ! existsFile { continue } fhType := reflect . TypeOf ( ( * multipart . FileHeader ) ( nil ) ) numElems := len ( inputFile ) if structField . Kind ( ) == reflect . Slice && numElems > 0 && structField . Type ( ) . Elem ( ) == fhType { slice := reflect . MakeSlice ( structField . Type ( ) , numElems , numElems ) for i := 0 ; i < numElems ; i ++ { slice . Index ( i ) . Set ( reflect . ValueOf ( inputFile [ i ] ) ) } structField . Set ( slice ) } else if structField . Type ( ) == fhType { structField . Set ( reflect . ValueOf ( inputFile [ 0 ] ) ) } } } }
// Takes values from the form data and puts them into a struct
Takes values from the form data and puts them into a struct