2017-05-20 13:45:15 +02:00
package Messages
import (
"github.com/gorilla/context"
"fmt"
"net/http"
)
const MessagesKey = "messages"
type Messages struct {
Errors map [ string ] [ ] string
Infos map [ string ] [ ] string
2017-05-20 17:01:13 +02:00
r * http . Request
2017-05-20 13:45:15 +02:00
}
func GetMessages ( r * http . Request ) Messages {
if rv := context . Get ( r , MessagesKey ) ; rv != nil {
return rv . ( Messages )
} else {
context . Set ( r , MessagesKey , Messages { } )
2017-05-20 17:01:13 +02:00
return Messages { make ( map [ string ] [ ] string ) , make ( map [ string ] [ ] string ) , r }
2017-05-20 13:45:15 +02:00
}
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) AddError ( name string , msg string ) {
2017-05-20 13:45:15 +02:00
mes . Errors [ name ] = append ( mes . Errors [ name ] , msg )
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) AddErrorf ( name string , msg string , args ... interface { } ) {
mes . AddError ( name , fmt . Sprintf ( msg , args ... ) )
}
func ( mes Messages ) ImportFromError ( name string , err error ) {
mes . AddError ( name , err . Error ( ) )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) AddInfo ( name string , msg string ) {
2017-05-20 13:45:15 +02:00
mes . Infos [ name ] = append ( mes . Infos [ name ] , msg )
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) AddInfof ( name string , msg string , args ... interface { } ) {
mes . AddInfo ( name , fmt . Sprintf ( msg , args ... ) )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) ClearErrors ( ) {
mes . Infos = nil
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) ClearInfos ( ) {
2017-05-20 13:45:15 +02:00
mes . Errors = nil
2017-05-20 17:01:13 +02:00
mes . setMessagesInContext ( )
2017-05-20 13:45:15 +02:00
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) GetAllErrors ( ) map [ string ] [ ] string {
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
return mes . Errors
2017-05-20 13:45:15 +02:00
}
func ( mes Messages ) GetErrors ( name string ) [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return mes . Errors [ name ]
}
2017-05-20 17:01:13 +02:00
2017-05-20 13:45:15 +02:00
func ( mes Messages ) GetAllInfos ( ) map [ string ] [ ] string {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return mes . Infos
}
2017-05-20 17:01:13 +02:00
func ( mes Messages ) GetInfos ( name string ) [ ] string {
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
return mes . Infos [ name ]
2017-05-20 13:45:15 +02:00
}
func ( mes Messages ) HasErrors ( ) bool {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return len ( mes . Errors ) > 0
}
func ( mes Messages ) HasInfos ( ) bool {
2017-05-20 17:01:13 +02:00
mes = GetMessages ( mes . r ) // We need to look if any new errors from other functions has updated context
2017-05-20 13:45:15 +02:00
return len ( mes . Infos ) > 0
2017-05-20 17:01:13 +02:00
}
func ( mes Messages ) setMessagesInContext ( ) {
context . Set ( mes . r , MessagesKey , mes )
2017-05-20 13:45:15 +02:00
}