summaryrefslogtreecommitdiff
path: root/formdec.go
blob: d554696fe05b5648bd44d0bd6d2bbedeb8fe1d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
	"github.com/gorilla/schema"
	"reflect"
	"regexp"
	"time"
)

type EMail string

var emailRegex = regexp.MustCompile(`^.+@.+$`)

func EMailConvert(s string) reflect.Value {
	if emailRegex.MatchString(s) {
		return reflect.ValueOf(EMail(s))
	}
	return reflect.Value{}
}

type timelocForm struct {
	Loc *time.Location
}

func locationConverter(s string) reflect.Value {
	loc, err := time.LoadLocation(s)
	if err != nil {
		return reflect.Value{}
	}
	return reflect.ValueOf(timelocForm{loc})
}

var formdec *schema.Decoder

func init() {
	formdec = schema.NewDecoder()
	formdec.RegisterConverter(EMail(""), EMailConvert)
	formdec.RegisterConverter(timelocForm{}, locationConverter)
}