2023-01-31 14:40:12 +00:00
package pkg
import (
"context"
2023-02-01 13:15:02 +00:00
"database/sql/driver"
2023-02-01 10:30:30 +00:00
"errors"
2023-02-01 13:15:02 +00:00
"fmt"
2023-02-12 20:06:48 +00:00
"github.com/jackc/pgtype"
2023-02-01 13:15:02 +00:00
"html/template"
2023-02-01 10:02:32 +00:00
"net/http"
2023-01-31 14:40:12 +00:00
"net/mail"
2023-02-01 13:15:02 +00:00
"net/url"
"regexp"
"strconv"
2023-02-01 10:02:32 +00:00
"strings"
2023-02-12 20:06:48 +00:00
"time"
2023-01-31 14:40:12 +00:00
)
2023-02-01 10:02:32 +00:00
type Attribute struct {
Key , Val string
}
2023-01-31 14:40:12 +00:00
type InputField struct {
2023-02-01 10:02:32 +00:00
Name string
Label string
Type string
2023-02-01 13:15:02 +00:00
Val string
2023-02-01 10:02:32 +00:00
Required bool
2023-02-01 13:15:02 +00:00
Attributes [ ] template . HTMLAttr
2023-02-01 10:02:32 +00:00
Errors [ ] error
}
2023-02-01 13:15:02 +00:00
func ( field * InputField ) Scan ( value interface { } ) error {
if value == nil {
field . Val = ""
return nil
}
field . Val = fmt . Sprintf ( "%v" , value )
return nil
}
func ( field * InputField ) Value ( ) ( driver . Value , error ) {
return field . Val , nil
}
2023-02-01 10:02:32 +00:00
func ( field * InputField ) FillValue ( r * http . Request ) {
2023-02-01 13:15:02 +00:00
field . Val = strings . TrimSpace ( r . FormValue ( field . Name ) )
}
func ( field * InputField ) Integer ( ) int {
2023-02-20 10:42:21 +00:00
value , err := strconv . Atoi ( field . Val )
if err != nil {
panic ( err )
}
return value
}
func ( field * InputField ) Float64 ( ) float64 {
value , err := strconv . ParseFloat ( field . Val , 64 )
if err != nil {
panic ( err )
}
2023-02-01 13:15:02 +00:00
return value
2023-01-31 14:40:12 +00:00
}
type SelectOption struct {
Value string
Label string
2023-03-01 10:40:23 +00:00
Group string
2023-01-31 14:40:12 +00:00
}
type SelectField struct {
2023-02-01 13:15:02 +00:00
Name string
Label string
2023-02-08 12:47:36 +00:00
Selected [ ] string
2023-02-01 13:15:02 +00:00
Options [ ] * SelectOption
Attributes [ ] template . HTMLAttr
2023-02-05 13:06:33 +00:00
Required bool
2023-02-08 12:47:36 +00:00
Multiple bool
2023-02-05 13:06:33 +00:00
EmptyLabel string
2023-02-01 13:15:02 +00:00
Errors [ ] error
}
func ( field * SelectField ) Scan ( value interface { } ) error {
if value == nil {
2023-02-08 12:47:36 +00:00
field . Selected = append ( field . Selected , "" )
2023-02-01 13:15:02 +00:00
return nil
}
2023-02-12 20:06:48 +00:00
if str , ok := value . ( string ) ; ok {
if array , err := pgtype . ParseUntypedTextArray ( str ) ; err == nil {
for _ , element := range array . Elements {
field . Selected = append ( field . Selected , element )
}
return nil
}
}
2023-02-08 12:47:36 +00:00
field . Selected = append ( field . Selected , fmt . Sprintf ( "%v" , value ) )
2023-02-01 13:15:02 +00:00
return nil
}
func ( field * SelectField ) Value ( ) ( driver . Value , error ) {
2023-02-08 12:47:36 +00:00
if field . Selected == nil {
return "" , nil
}
return field . Selected [ 0 ] , nil
2023-01-31 14:40:12 +00:00
}
2023-02-01 10:02:32 +00:00
func ( field * SelectField ) FillValue ( r * http . Request ) {
2023-02-08 12:47:36 +00:00
field . Selected = r . Form [ field . Name ]
}
func ( field * SelectField ) HasValidOptions ( ) bool {
for _ , selected := range field . Selected {
if ! field . isValidOption ( selected ) {
return false
}
}
return true
}
func ( field * SelectField ) IsSelected ( v string ) bool {
for _ , selected := range field . Selected {
if selected == v {
return true
}
}
return false
2023-02-01 10:02:32 +00:00
}
2023-03-01 10:55:26 +00:00
func ( field * SelectField ) FindOption ( value string ) * SelectOption {
2023-01-31 14:40:12 +00:00
for _ , option := range field . Options {
2023-03-01 10:55:26 +00:00
if option . Value == value {
return option
2023-01-31 14:40:12 +00:00
}
}
2023-03-01 10:55:26 +00:00
return nil
}
func ( field * SelectField ) isValidOption ( selected string ) bool {
return field . FindOption ( selected ) != nil
2023-01-31 14:40:12 +00:00
}
2023-03-08 10:26:02 +00:00
func ( field * SelectField ) Clear ( ) {
field . Selected = [ ] string { }
}
2023-01-31 14:40:12 +00:00
func MustGetOptions ( ctx context . Context , conn * Conn , sql string , args ... interface { } ) [ ] * SelectOption {
rows , err := conn . Query ( ctx , sql , args ... )
if err != nil {
panic ( err )
}
defer rows . Close ( )
var options [ ] * SelectOption
for rows . Next ( ) {
option := & SelectOption { }
err = rows . Scan ( & option . Value , & option . Label )
if err != nil {
panic ( err )
}
options = append ( options , option )
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return options
}
2023-02-01 10:30:30 +00:00
2023-03-01 10:40:23 +00:00
func MustGetGroupedOptions ( ctx context . Context , conn * Conn , sql string , args ... interface { } ) [ ] * SelectOption {
rows , err := conn . Query ( ctx , sql , args ... )
if err != nil {
panic ( err )
}
defer rows . Close ( )
var options [ ] * SelectOption
for rows . Next ( ) {
option := & SelectOption { }
err = rows . Scan ( & option . Value , & option . Label , & option . Group )
if err != nil {
panic ( err )
}
options = append ( options , option )
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return options
}
2023-02-01 13:15:02 +00:00
func mustGetCountryOptions ( ctx context . Context , conn * Conn , locale * Locale ) [ ] * SelectOption {
return MustGetOptions ( ctx , conn , "select country.country_code, coalesce(i18n.name, country.name) as l10n_name from country left join country_i18n as i18n on country.country_code = i18n.country_code and i18n.lang_tag = $1 order by l10n_name" , locale . Language )
}
2023-02-01 10:30:30 +00:00
type FormValidator struct {
Valid bool
}
func newFormValidator ( ) * FormValidator {
return & FormValidator { true }
}
func ( v * FormValidator ) AllOK ( ) bool {
return v . Valid
}
func ( v * FormValidator ) CheckRequiredInput ( field * InputField , message string ) bool {
2023-02-01 13:15:02 +00:00
return v . checkInput ( field , field . Val != "" , message )
2023-02-01 10:30:30 +00:00
}
func ( v * FormValidator ) CheckValidEmailInput ( field * InputField , message string ) bool {
2023-02-01 13:15:02 +00:00
_ , err := mail . ParseAddress ( field . Val )
2023-02-01 10:30:30 +00:00
return v . checkInput ( field , err == nil , message )
}
2023-02-01 13:15:02 +00:00
func ( v * FormValidator ) CheckValidVATINInput ( field * InputField , country string , message string ) bool {
// TODO: actual VATIN validation
return v . checkInput ( field , true , message )
}
func ( v * FormValidator ) CheckValidPhoneInput ( field * InputField , country string , message string ) bool {
// TODO: actual phone validation
return v . checkInput ( field , true , message )
}
2023-02-01 10:30:30 +00:00
func ( v * FormValidator ) CheckPasswordConfirmation ( password * InputField , confirm * InputField , message string ) bool {
2023-02-01 13:15:02 +00:00
return v . checkInput ( confirm , password . Val == confirm . Val , message )
2023-02-01 10:30:30 +00:00
}
func ( v * FormValidator ) CheckValidSelectOption ( field * SelectField , message string ) bool {
2023-02-08 12:47:36 +00:00
return v . checkSelect ( field , field . HasValidOptions ( ) , message )
2023-02-01 10:30:30 +00:00
}
2023-03-01 10:55:26 +00:00
func ( v * FormValidator ) CheckAtMostOneOfEachGroup ( field * SelectField , message string ) bool {
repeated := false
groups := map [ string ] bool { }
for _ , selected := range field . Selected {
option := field . FindOption ( selected )
if exists := groups [ option . Group ] ; exists {
repeated = true
break
}
groups [ option . Group ] = true
}
return v . checkSelect ( field , ! repeated , message )
}
2023-02-12 20:01:20 +00:00
func ( v * FormValidator ) CheckValidURL ( field * InputField , message string ) bool {
2023-02-01 13:15:02 +00:00
_ , err := url . ParseRequestURI ( field . Val )
return v . checkInput ( field , err == nil , message )
}
2023-02-12 20:06:48 +00:00
func ( v * FormValidator ) CheckValidDate ( field * InputField , message string ) bool {
2023-02-13 09:34:55 +00:00
_ , err := time . Parse ( "2006-01-02" , field . Val )
2023-02-12 20:06:48 +00:00
return v . checkInput ( field , err == nil , message )
}
2023-02-01 13:15:02 +00:00
func ( v * FormValidator ) CheckValidPostalCode ( ctx context . Context , conn * Conn , field * InputField , country string , message string ) bool {
pattern := "^" + conn . MustGetText ( ctx , ".{1,255}" , "select postal_code_regex from country where country_code = $1" , country ) + "$"
match , err := regexp . MatchString ( pattern , field . Val )
if err != nil {
panic ( err )
}
return v . checkInput ( field , match , message )
}
func ( v * FormValidator ) CheckValidInteger ( field * InputField , min int , max int , message string ) bool {
value , err := strconv . Atoi ( field . Val )
return v . checkInput ( field , err == nil && value >= min && value <= max , message )
}
Convert from cents to “price” and back
I do not want to use floats in the Go lang application, because it is
not supposed to do anything with these values other than to print and
retrieve them from the user; all computations will be performed by
PostgreSQL in cents.
That means i have to “convert” from the price format that users expect
to see (e.g., 1.234,56) to cents (e.g., 123456) and back when passing
data between Go and PostgreSQL, and that conversion depends on the
currency’s decimal places.
At first i did everything in Go, but saw that i would need to do it in
a loop when retrieving the list of products, and immediately knew it was
a mistake—i needed a PL/pgSQL function for that.
I still need to convert from string to float, however, when printing the
value to the user. Because the string representation is in C, but i
need to format it according to the locale with golang/x/text. That
package has the information of how to correctly format numbers, but it
is in an internal package that i can not use, and numbers.Digit only
accepts numeric types, not a string.
2023-02-05 12:55:12 +00:00
func ( v * FormValidator ) CheckValidDecimal ( field * InputField , min float64 , max float64 , message string ) bool {
value , err := strconv . ParseFloat ( field . Val , 64 )
return v . checkInput ( field , err == nil && value >= min && value <= max , message )
}
2023-02-01 10:30:30 +00:00
func ( v * FormValidator ) checkInput ( field * InputField , ok bool , message string ) bool {
if ! ok {
field . Errors = append ( field . Errors , errors . New ( message ) )
v . Valid = false
}
return ok
}
func ( v * FormValidator ) checkSelect ( field * SelectField , ok bool , message string ) bool {
if ! ok {
field . Errors = append ( field . Errors , errors . New ( message ) )
v . Valid = false
}
return ok
}