2017-05-06 17:55:02 +02:00
package modelHelper
import (
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
"fmt"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
"net/http"
"reflect"
2017-05-06 18:52:27 +02:00
"strconv"
2017-05-06 17:55:02 +02:00
)
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// TODO: Rewrite module
// Functions are highly complex and require a lot of additional error handling
2017-05-06 17:55:02 +02:00
func IsZeroOfUnderlyingType ( x interface { } ) bool {
return x == reflect . Zero ( reflect . TypeOf ( x ) ) . Interface ( )
}
// AssignValue assign form values to model.
func AssignValue ( model interface { } , form interface { } ) {
modelIndirect := reflect . Indirect ( reflect . ValueOf ( model ) )
formElem := reflect . ValueOf ( form ) . Elem ( )
typeOfTForm := formElem . Type ( )
for i := 0 ; i < formElem . NumField ( ) ; i ++ {
2017-05-07 14:53:01 +02:00
tag := typeOfTForm . Field ( i ) . Tag
2017-05-07 19:59:38 +02:00
if tag . Get ( "omit" ) != "true" {
2017-05-07 14:53:01 +02:00
modelField := modelIndirect . FieldByName ( typeOfTForm . Field ( i ) . Name )
if modelField . IsValid ( ) {
formField := formElem . Field ( i )
modelField . Set ( formField )
} else {
log . Warnf ( "modelField : %s - %s" , typeOfTForm . Field ( i ) . Name , modelField )
}
2017-05-06 17:55:02 +02:00
}
}
}
2017-05-06 18:02:54 +02:00
// AssignValue assign form values to model.
func BindValueForm ( form interface { } , r * http . Request ) {
r . ParseForm ( )
formElem := reflect . ValueOf ( form ) . Elem ( )
for i := 0 ; i < formElem . NumField ( ) ; i ++ {
2017-05-06 18:52:27 +02:00
typeField := formElem . Type ( ) . Field ( i )
tag := typeField . Tag
2017-05-06 18:55:33 +02:00
switch typeField . Type . Name ( ) {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
case "string" :
formElem . Field ( i ) . SetString ( r . PostFormValue ( tag . Get ( "form" ) ) )
case "int" :
nbr , _ := strconv . Atoi ( r . PostFormValue ( tag . Get ( "form" ) ) )
formElem . Field ( i ) . SetInt ( int64 ( nbr ) )
case "float" :
nbr , _ := strconv . Atoi ( r . PostFormValue ( tag . Get ( "form" ) ) )
formElem . Field ( i ) . SetFloat ( float64 ( nbr ) )
case "bool" :
nbr , _ := strconv . ParseBool ( r . PostFormValue ( tag . Get ( "form" ) ) )
formElem . Field ( i ) . SetBool ( nbr )
2017-05-06 18:55:33 +02:00
}
2017-05-06 18:02:54 +02:00
}
2017-05-06 18:55:33 +02:00
}
2017-05-07 19:59:38 +02:00
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func ValidateForm ( form interface { } , errorForm map [ string ] [ ] string ) map [ string ] [ ] string {
2017-05-07 19:59:38 +02:00
formElem := reflect . ValueOf ( form ) . Elem ( )
for i := 0 ; i < formElem . NumField ( ) ; i ++ {
typeField := formElem . Type ( ) . Field ( i )
tag := typeField . Tag
inputName := typeField . Name
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if tag . Get ( "hum_name" ) != "" { // For more human input name than gibberish
2017-05-07 19:59:38 +02:00
inputName = tag . Get ( "hum_name" )
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if tag . Get ( "len_min" ) != "" && ( tag . Get ( "needed" ) != "" || formElem . Field ( i ) . Len ( ) > 0 ) { // Check minimum length
2017-05-07 19:59:38 +02:00
lenMin , _ := strconv . Atoi ( tag . Get ( "len_min" ) )
if formElem . Field ( i ) . Len ( ) < lenMin {
2017-05-07 20:21:46 +02:00
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Minimal length of %s required for the input: %s" , strconv . Itoa ( lenMin ) , inputName ) )
2017-05-07 19:59:38 +02:00
}
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if tag . Get ( "len_max" ) != "" && ( tag . Get ( "needed" ) != "" || formElem . Field ( i ) . Len ( ) > 0 ) { // Check maximum length
2017-05-07 19:59:38 +02:00
lenMax , _ := strconv . Atoi ( tag . Get ( "len_max" ) )
if formElem . Field ( i ) . Len ( ) > lenMax {
2017-05-07 20:21:46 +02:00
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Maximal length of %s required for the input: %s" , strconv . Itoa ( lenMax ) , inputName ) )
2017-05-07 19:59:38 +02:00
}
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if tag . Get ( "equalInput" ) != "" && ( tag . Get ( "needed" ) != "" || formElem . Field ( i ) . Len ( ) > 0 ) {
2017-05-07 20:21:46 +02:00
otherInput := formElem . FieldByName ( tag . Get ( "equalInput" ) )
2017-05-07 19:59:38 +02:00
if formElem . Field ( i ) . Interface ( ) != otherInput . Interface ( ) {
2017-05-07 20:21:46 +02:00
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Must be same %s" , inputName ) )
2017-05-07 19:59:38 +02:00
}
}
switch typeField . Type . Name ( ) {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
case "string" :
if tag . Get ( "equal" ) != "" && formElem . Field ( i ) . String ( ) != tag . Get ( "equal" ) {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Wrong value for the input: %s" , inputName ) )
}
if tag . Get ( "needed" ) != "" && formElem . Field ( i ) . String ( ) == "" {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Field needed: %s" , inputName ) )
}
2017-05-10 20:24:37 +02:00
if formElem . Field ( i ) . String ( ) == "" && tag . Get ( "default" ) != "" {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
formElem . Field ( i ) . SetString ( tag . Get ( "default" ) )
}
case "int" :
if tag . Get ( "equal" ) != "" { // Check minimum length
equal , _ := strconv . Atoi ( tag . Get ( "equal" ) )
if formElem . Field ( i ) . Int ( ) > int64 ( equal ) {
2017-05-07 19:59:38 +02:00
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Wrong value for the input: %s" , inputName ) )
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
if tag . Get ( "needed" ) != "" && formElem . Field ( i ) . Int ( ) == 0 {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Field needed: %s" , inputName ) )
}
2017-05-10 20:24:37 +02:00
if formElem . Field ( i ) . Interface == nil && tag . Get ( "default" ) != "" {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
defaultValue , _ := strconv . Atoi ( tag . Get ( "default" ) )
formElem . Field ( i ) . SetInt ( int64 ( defaultValue ) )
}
case "float" :
if tag . Get ( "equal" ) != "" { // Check minimum length
equal , _ := strconv . Atoi ( tag . Get ( "equal" ) )
if formElem . Field ( i ) . Float ( ) != float64 ( equal ) {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Wrong value for the input: %s" , inputName ) )
2017-05-07 19:59:38 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
if tag . Get ( "needed" ) != "" && formElem . Field ( i ) . Float ( ) == 0 {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Field needed: %s" , inputName ) )
}
2017-05-10 20:24:37 +02:00
if formElem . Field ( i ) . Interface == nil && tag . Get ( "default" ) != "" {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
defaultValue , _ := strconv . Atoi ( tag . Get ( "default" ) )
formElem . Field ( i ) . SetFloat ( float64 ( defaultValue ) )
}
case "bool" :
if tag . Get ( "equal" ) != "" { // Check minimum length
equal , _ := strconv . ParseBool ( tag . Get ( "equal" ) )
if formElem . Field ( i ) . Bool ( ) != equal {
errorForm [ tag . Get ( "form" ) ] = append ( errorForm [ tag . Get ( "form" ) ] , fmt . Sprintf ( "Wrong value for the input: %s" , inputName ) )
2017-05-09 17:47:06 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
2017-05-07 19:59:38 +02:00
}
}
return errorForm
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}