~repos /gromer

#golang#htmx#ssr

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

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


7bd7ac9d Peter John

tag: v0.9.8

v0.9.8

3 years ago
fix camel casing
Files changed (4) hide show
  1. go.mod +1 -1
  2. go.sum +2 -2
  3. utils.go +2 -8
  4. 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
go.sum CHANGED
@@ -278,8 +278,6 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
278
278
  github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
279
279
  github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
280
280
  github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
281
- github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
282
- github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
283
281
  github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
284
282
  github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
285
283
  github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
@@ -338,6 +336,8 @@ github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+
338
336
  github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
339
337
  github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
340
338
  github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
339
+ github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
340
+ github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734/go.mod h1:hqVOMAwu+ekffC3Tvq5N1ljnXRrFKcaSjbCmQ8JgYaI=
341
341
  github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
342
342
  github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
343
343
  github.com/shurcooL/github_flavored_markdown v0.0.0-20210228213109-c3a9aa474629 h1:86e54L0i3pH3dAIA8OxBbfLrVyhoGpnNk1iJCigAWYs=
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",