gromer
git clone https://git.pyrossh.dev/gromer
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
1656741
— Peter John
2021-12-29T01:06:02+05:30
add utils
Generated files not shown: go.sum
- go.mod +1 -0
- utils.go +76 -0
- utils_test.go +105 -0
go.mod
CHANGED
|
@@ -12,6 +12,7 @@ require (
|
|
|
12
12
|
github.com/google/uuid v1.3.0
|
|
13
13
|
github.com/gorilla/mux v1.8.0
|
|
14
14
|
github.com/iancoleman/strcase v0.2.0
|
|
15
|
+
github.com/imdario/mergo v0.3.12 // indirect
|
|
15
16
|
github.com/lib/pq v1.10.4
|
|
16
17
|
github.com/markbates/inflect v1.0.4
|
|
17
18
|
github.com/microcosm-cc/bluemonday v1.0.15 // indirect
|
utils.go
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
package gromer
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"reflect"
|
|
5
|
+
"regexp"
|
|
6
|
+
"strings"
|
|
7
|
+
"time"
|
|
8
|
+
|
|
9
|
+
"github.com/go-playground/validator/v10"
|
|
10
|
+
"github.com/iancoleman/strcase"
|
|
11
|
+
"github.com/imdario/mergo"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
var Validator = validator.New()
|
|
15
|
+
var ValidatorErrorMap = map[string]string{
|
|
16
|
+
"required": "is required",
|
|
17
|
+
}
|
|
18
|
+
var upperRegex = regexp.MustCompile("^[^a-z]*$")
|
|
19
|
+
|
|
20
|
+
type timeTransformer struct {
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
|
24
|
+
if typ == reflect.TypeOf(time.Time{}) {
|
|
25
|
+
return func(dst, src reflect.Value) error {
|
|
26
|
+
if dst.CanSet() {
|
|
27
|
+
srcResult := src.MethodByName("IsZero").Call([]reflect.Value{})
|
|
28
|
+
if !srcResult[0].Bool() {
|
|
29
|
+
dst.Set(src)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return nil
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return nil
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func Merge(dst interface{}, src interface{}) error {
|
|
39
|
+
err := mergo.Merge(dst, src, mergo.WithOverwriteWithEmptyValue)
|
|
40
|
+
if err != nil {
|
|
41
|
+
return err
|
|
42
|
+
}
|
|
43
|
+
return Validate(dst)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func Validate(dst interface{}) error {
|
|
47
|
+
return Validator.Struct(dst)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func RegisterValidation(k, msg string, validate func(fl validator.FieldLevel) bool) {
|
|
51
|
+
ValidatorErrorMap[k] = msg
|
|
52
|
+
Validator.RegisterValidation(k, validate)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func GetValidationError(err validator.ValidationErrors) map[string]string {
|
|
56
|
+
emap := map[string]string{}
|
|
57
|
+
for _, e := range err {
|
|
58
|
+
parts := strings.Split(e.StructNamespace(), ".")
|
|
59
|
+
lowerParts := []string{}
|
|
60
|
+
for _, p := range parts[1:] {
|
|
61
|
+
if upperRegex.MatchString(p) {
|
|
62
|
+
lowerParts = append(lowerParts, strings.ToLower(p))
|
|
63
|
+
} else {
|
|
64
|
+
lowerParts = append(lowerParts, strcase.ToLowerCamel(p))
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
k := strings.Join(lowerParts, ".")
|
|
68
|
+
errorMsg, ok := ValidatorErrorMap[e.Tag()]
|
|
69
|
+
if ok {
|
|
70
|
+
emap[k] = errorMsg
|
|
71
|
+
} else {
|
|
72
|
+
emap[k] = "is not valid" // e.Error()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return emap
|
|
76
|
+
}
|
utils_test.go
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
package gromer
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"regexp"
|
|
5
|
+
"testing"
|
|
6
|
+
"time"
|
|
7
|
+
|
|
8
|
+
"github.com/go-playground/validator/v10"
|
|
9
|
+
"github.com/stretchr/testify/assert"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func init() {
|
|
13
|
+
pinCodeRegex := regexp.MustCompile("^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$")
|
|
14
|
+
RegisterValidation("pincode", "is not in valid format", func(fl validator.FieldLevel) bool {
|
|
15
|
+
return pinCodeRegex.MatchString(fl.Field().String())
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func TestUpperRegex(t *testing.T) {
|
|
20
|
+
assert.Equal(t, true, upperRegex.MatchString("PAN"))
|
|
21
|
+
assert.Equal(t, false, upperRegex.MatchString("PaN"))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Todo struct {
|
|
25
|
+
ID string `json:"id" validate:"required"`
|
|
26
|
+
Pincode string `json:"pincode" validate:"required,pincode"`
|
|
27
|
+
PAN string `json:"pan" validate:"required"`
|
|
28
|
+
CreatedAt time.Time `json:"createdAt"`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func TestValidator(t *testing.T) {
|
|
32
|
+
assert.NoError(t, Validator.Var("560001", "pincode"))
|
|
33
|
+
assert.EqualError(t, Validator.Var("ABCD", "pincode"), "Key: '' Error:Field validation for '' failed on the 'pincode' tag")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
func TestValidate(t *testing.T) {
|
|
37
|
+
todo := &Todo{
|
|
38
|
+
ID: "123",
|
|
39
|
+
Pincode: "",
|
|
40
|
+
PAN: "",
|
|
41
|
+
}
|
|
42
|
+
err := Validate(todo)
|
|
43
|
+
assert.EqualError(t, err, "Key: 'Todo.Pincode' Error:Field validation for 'Pincode' failed on the 'required' tag\nKey: 'Todo.PAN' Error:Field validation for 'PAN' failed on the 'required' tag")
|
|
44
|
+
validationErrors := err.(validator.ValidationErrors)
|
|
45
|
+
assert.Equal(t, GetValidationError(validationErrors), map[string]string{
|
|
46
|
+
"pincode": "is required",
|
|
47
|
+
"pan": "is required",
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
todo.Pincode = "AWS"
|
|
51
|
+
err = Validate(todo)
|
|
52
|
+
assert.EqualError(t, err, "Key: 'Todo.Pincode' Error:Field validation for 'Pincode' failed on the 'pincode' tag\nKey: 'Todo.PAN' Error:Field validation for 'PAN' failed on the 'required' tag")
|
|
53
|
+
validationErrors = err.(validator.ValidationErrors)
|
|
54
|
+
assert.EqualValues(t, map[string]string{
|
|
55
|
+
"pincode": "is not in valid format",
|
|
56
|
+
"pan": "is required",
|
|
57
|
+
}, GetValidationError(validationErrors))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Note struct {
|
|
61
|
+
ID string `json:"id"`
|
|
62
|
+
Text string `json:"text"`
|
|
63
|
+
CreatedAt time.Time `json:"createdAt"`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func TestMerge(t *testing.T) {
|
|
67
|
+
note := &Note{
|
|
68
|
+
ID: "",
|
|
69
|
+
Text: "",
|
|
70
|
+
CreatedAt: time.Time{},
|
|
71
|
+
}
|
|
72
|
+
err := Merge(note, &Note{
|
|
73
|
+
ID: "1",
|
|
74
|
+
Text: "1",
|
|
75
|
+
CreatedAt: time.Date(2020, 10, 10, 0, 0, 0, 0, time.UTC),
|
|
76
|
+
})
|
|
77
|
+
assert.NoError(t, err)
|
|
78
|
+
assert.EqualValues(t, &Note{
|
|
79
|
+
ID: "1",
|
|
80
|
+
Text: "1",
|
|
81
|
+
CreatedAt: time.Date(2020, 10, 10, 0, 0, 0, 0, time.UTC),
|
|
82
|
+
}, note)
|
|
83
|
+
err = Merge(note, &Note{
|
|
84
|
+
ID: "2",
|
|
85
|
+
Text: "2",
|
|
86
|
+
CreatedAt: time.Date(2020, 11, 11, 0, 0, 0, 0, time.UTC),
|
|
87
|
+
})
|
|
88
|
+
assert.NoError(t, err)
|
|
89
|
+
assert.EqualValues(t, &Note{
|
|
90
|
+
ID: "2",
|
|
91
|
+
Text: "2",
|
|
92
|
+
CreatedAt: time.Date(2020, 11, 11, 0, 0, 0, 0, time.UTC),
|
|
93
|
+
}, note)
|
|
94
|
+
err = Merge(note, &Note{
|
|
95
|
+
ID: "2",
|
|
96
|
+
Text: "",
|
|
97
|
+
CreatedAt: time.Time{},
|
|
98
|
+
})
|
|
99
|
+
assert.NoError(t, err)
|
|
100
|
+
assert.EqualValues(t, &Note{
|
|
101
|
+
ID: "2",
|
|
102
|
+
Text: "",
|
|
103
|
+
CreatedAt: time.Time{},
|
|
104
|
+
}, note)
|
|
105
|
+
}
|