gromer

#golang#htmx#ssr

git clone https://git.pyrossh.dev/gromer

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


bb9a223 — Peter John 2022-01-01T10:37:10+05:30
fix camel casing

Generated files not shown: go.sum

Files changed (3) hide show
  1. go.mod +1 -1
  2. utils.go +2 -8
  3. utils_test.go +7 -10
go.mod CHANGED
@@ -11,13 +11,13 @@ require (
11
11
  github.com/gobuffalo/velvet v0.0.0-20170320144106-d97471bf5d8f
12
12
  github.com/google/uuid v1.3.0
13
13
  github.com/gorilla/mux v1.8.0
14
- github.com/iancoleman/strcase v0.2.0
15
14
  github.com/imdario/mergo v0.3.12
16
15
  github.com/lib/pq v1.10.4
17
16
  github.com/markbates/inflect v1.0.4
18
17
  github.com/microcosm-cc/bluemonday v1.0.16 // indirect
19
18
  github.com/pkg/errors v0.9.1
20
19
  github.com/rs/zerolog v1.26.1
20
+ github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734
21
21
  github.com/sergi/go-diff v1.2.0 // indirect
22
22
  github.com/shurcooL/github_flavored_markdown v0.0.0-20210228213109-c3a9aa474629
23
23
  github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
utils.go CHANGED
@@ -2,20 +2,18 @@ package gromer
2
2
 
3
3
  import (
4
4
  "reflect"
5
- "regexp"
6
5
  "strings"
7
6
  "time"
8
7
 
9
8
  "github.com/go-playground/validator/v10"
10
- "github.com/iancoleman/strcase"
11
9
  "github.com/imdario/mergo"
10
+ "github.com/segmentio/go-camelcase"
12
11
  )
13
12
 
14
13
  var Validator = validator.New()
15
14
  var ValidatorErrorMap = map[string]string{
16
15
  "required": "is required",
17
16
  }
18
- var upperRegex = regexp.MustCompile("^[^a-z]*$")
19
17
 
20
18
  type timeTransformer struct {
21
19
  }
@@ -58,11 +56,7 @@ func GetValidationError(err validator.ValidationErrors) map[string]string {
58
56
  parts := strings.Split(e.StructNamespace(), ".")
59
57
  lowerParts := []string{}
60
58
  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))
59
+ lowerParts = append(lowerParts, camelcase.Camelcase(p))
65
- }
66
60
  }
67
61
  k := strings.Join(lowerParts, ".")
68
62
  errorMsg, ok := ValidatorErrorMap[e.Tag()]
utils_test.go CHANGED
@@ -16,40 +16,37 @@ func init() {
16
16
  })
17
17
  }
18
18
 
19
- func TestUpperRegex(t *testing.T) {
19
+ func TestValidator(t *testing.T) {
20
- assert.Equal(t, true, upperRegex.MatchString("PAN"))
21
- assert.Equal(t, false, upperRegex.MatchString("PaN"))
20
+ assert.NoError(t, Validator.Var("560001", "pincode"))
21
+ assert.EqualError(t, Validator.Var("ABCD", "pincode"), "Key: '' Error:Field validation for '' failed on the 'pincode' tag")
22
22
  }
23
23
 
24
24
  type Todo struct {
25
25
  ID string `json:"id" validate:"required"`
26
+ OrgID string `json:"orgId" validate:"required"`
26
27
  Pincode string `json:"pincode" validate:"required,pincode"`
27
28
  PAN string `json:"pan" validate:"required"`
28
29
  CreatedAt time.Time `json:"createdAt"`
29
30
  }
30
31
 
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
32
  func TestValidate(t *testing.T) {
37
33
  todo := &Todo{
38
34
  ID: "123",
35
+ OrgID: "",
39
36
  Pincode: "",
40
37
  PAN: "",
41
38
  }
42
39
  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
40
  validationErrors := err.(validator.ValidationErrors)
45
41
  assert.Equal(t, GetValidationError(validationErrors), map[string]string{
46
42
  "pincode": "is required",
47
43
  "pan": "is required",
44
+ "orgId": "is required",
48
45
  })
49
46
 
50
47
  todo.Pincode = "AWS"
48
+ todo.OrgID = "123"
51
49
  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
50
  validationErrors = err.(validator.ValidationErrors)
54
51
  assert.EqualValues(t, map[string]string{
55
52
  "pincode": "is not in valid format",