From 5a3ec773d5d8333330d328201298400244cad591 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 1 Aug 2025 17:41:28 +0200 Subject: [PATCH 1/5] feat: add tag `field` to specify the naming strategy (case) --- util/inspect.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/util/inspect.go b/util/inspect.go index 76c8e9f..6f2369d 100644 --- a/util/inspect.go +++ b/util/inspect.go @@ -17,7 +17,10 @@ package util import ( "errors" + "fmt" "reflect" + + "github.com/iancoleman/strcase" ) func InspectStruct(input interface{}) (map[string]interface{}, error) { @@ -37,8 +40,22 @@ func InspectStruct(input interface{}) (map[string]interface{}, error) { for i := 0; i < val.NumField(); i++ { field := typ.Field(i) value := val.Field(i) + tags := typ.Field(i).Tag + name := field.Name - result[field.Name] = value.Interface() + fieldTag := tags.Get("field") + + fmt.Printf("%+v\n", fieldTag) + + if fieldTag == "snake" { + name = strcase.ToSnake(name) + } else if fieldTag == "lowerCamel" { + name = strcase.ToLowerCamel(name) + } + + fmt.Printf("%+v\n", name) + + result[name] = value.Interface() } return result, nil From f0a94dec933f8331c464b5439dac3b2b6cdf637a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 1 Aug 2025 17:41:41 +0200 Subject: [PATCH 2/5] feat: add tag `field` to specify the naming strategy (case) --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 0b43ae4..3973949 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( ) require ( + github.com/iancoleman/strcase v0.3.0 // indirect github.com/samber/lo v1.51.0 // indirect golang.org/x/text v0.22.0 // indirect maragu.dev/gomponents v1.1.0 // indirect diff --git a/go.sum b/go.sum index fd3f0f9..f6698b1 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= From c6fc6e45e4e5c0957ef446ebba8d942c6bfef375 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 1 Aug 2025 17:42:27 +0200 Subject: [PATCH 3/5] fix(validation/notblank): nil value validation --- validation/notblank.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validation/notblank.go b/validation/notblank.go index a417e22..1962cb2 100644 --- a/validation/notblank.go +++ b/validation/notblank.go @@ -39,7 +39,7 @@ func (c NotBlank) Validate(data any) []Error { v := reflect.ValueOf(data) - if v.IsZero() { + if data == nil || v.IsZero() { errors = append(errors, Error(c.Message)) return errors From 37eaf6b3485ab0a337639721b1e34eab2f582472 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 1 Aug 2025 17:42:44 +0200 Subject: [PATCH 4/5] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f207094..1629054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +### Added + +- feat: add tag `field` to specify the naming strategy (case) + ## v1.1.6 ### Fixed From 1a063823871ccffa1ae0e2b11f141d1b48fd774e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 1 Aug 2025 18:02:25 +0200 Subject: [PATCH 5/5] feat: remove 'snake' as field case choice --- util/inspect.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/util/inspect.go b/util/inspect.go index 6f2369d..17c0839 100644 --- a/util/inspect.go +++ b/util/inspect.go @@ -17,7 +17,6 @@ package util import ( "errors" - "fmt" "reflect" "github.com/iancoleman/strcase" @@ -45,16 +44,10 @@ func InspectStruct(input interface{}) (map[string]interface{}, error) { fieldTag := tags.Get("field") - fmt.Printf("%+v\n", fieldTag) - - if fieldTag == "snake" { - name = strcase.ToSnake(name) - } else if fieldTag == "lowerCamel" { + if fieldTag == "lowerCamel" { name = strcase.ToLowerCamel(name) } - fmt.Printf("%+v\n", name) - result[name] = value.Interface() }