From 37e7bb775a6cd292bfe134c615f04df586b2a2f8 Mon Sep 17 00:00:00 2001 From: "Tristan M." <54208010+Oudwins@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:06:11 +0200 Subject: [PATCH] refactor: packages zconst & primitives -> internals (#27) * refactor: moved the constants to their own package * refactor: renamed primitives to internals * docs: updated docs based on refactor --- README.md | 5 +- boolean.go | 2 +- conf/Errors.go | 87 +++++++++++----------- conf/Errors_test.go | 9 ++- {primitives => internals}/DataProviders.go | 2 +- {primitives => internals}/PathBuilder.go | 2 +- {primitives => internals}/errors.go | 18 +++-- {primitives => internals}/genericTests.go | 41 +++++----- {primitives => internals}/parsing.go | 6 +- {primitives => internals}/types.go | 2 +- {primitives => internals}/utils.go | 2 +- numbers.go | 2 +- slices.go | 23 +++--- string.go | 25 ++++--- struct.go | 9 ++- struct_test.go | 2 +- time.go | 15 ++-- utils.go | 33 ++++---- utilsOptions.go | 2 +- utilsOptions_test.go | 2 +- {primitives => zconst}/consts.go | 2 +- zenv/zenv.go | 2 +- zhttp/zhttp.go | 2 +- 23 files changed, 156 insertions(+), 139 deletions(-) rename {primitives => internals}/DataProviders.go (99%) rename {primitives => internals}/PathBuilder.go (95%) rename {primitives => internals}/errors.go (86%) rename {primitives => internals}/genericTests.go (78%) rename {primitives => internals}/parsing.go (91%) rename {primitives => internals}/types.go (96%) rename {primitives => internals}/utils.go (94%) rename {primitives => zconst}/consts.go (98%) diff --git a/README.md b/README.md index bbfaa35..3db2bbb 100644 --- a/README.md +++ b/README.md @@ -599,8 +599,9 @@ conf.ErrorFormatter = func(e p.ZogError, p z.ParseCtx) { } // override specific error messages -// For this you will need to import `zog/primitives` package. Which you should use with caution since it is an internal package -conf.DefaultErrMsgMap["string"]["my_custom_error_code"] = "my custom error message" +// For this I recommend you import `zod/zconst` which contains zog constants +conf.DefaultErrMsgMap[zconst.TypeString]["my_custom_error_code"] = "my custom error message" +conf.DefaultErrMsgMap[zconst.TypeString][zconst.ErrCodeRequired] = "Now all required errors will get this message" ``` ## Zog Schema Parsign Execution Structure diff --git a/boolean.go b/boolean.go index aa63dc0..6ea14f9 100644 --- a/boolean.go +++ b/boolean.go @@ -2,7 +2,7 @@ package zog import ( "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" ) type boolProcessor struct { diff --git a/conf/Errors.go b/conf/Errors.go index 34ff799..69d8159 100644 --- a/conf/Errors.go +++ b/conf/Errors.go @@ -1,60 +1,63 @@ package conf +// Default error messages for all schemas. Replace the text with your own messages to customize the error messages for all zog schemas +// As a general rule of thumb, if an error message only has one parameter, the parameter name will be the same as the error code import ( "fmt" "strings" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + zconst "github.com/Oudwins/zog/zconst" ) // Default error messages for all schemas. Replace the text with your own messages to customize the error messages for all zog schemas // As a general rule of thumb, if an error message only has one parameter, the parameter name will be the same as the error code -var DefaultErrMsgMap = map[p.ZogType]map[p.ZogErrCode]string{ - p.TypeString: { - p.ErrCodeRequired: "is required", - p.ErrCodeMin: "string must contain at least {{min}} character(s)", - p.ErrCodeMax: "string must contain at most {{min}} character(s)", - p.ErrCodeLen: "string must be exactly {{len}} character(s)", - p.ErrCodeEmail: "must be a valid email", - p.ErrCodeURL: "must be a valid URL", - p.ErrCodeHasPrefix: "string must start with {{prefix}}", - p.ErrCodeHasSuffix: "string must end with {{suffix}}", - p.ErrCodeContains: "string must contain {{contained}}", - p.ErrCodeContainsDigit: "string must contain at least one digit", - p.ErrCodeContainsUpper: "string must contain at least one uppercase letter", - p.ErrCodeContainsLower: "string must contain at least one lowercase letter", - p.ErrCodeContainsSpecial: "string must contain at least one special character", - p.ErrCodeOneOf: "string must be one of {{one_of_options}}", +var DefaultErrMsgMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{ + zconst.TypeString: { + zconst.ErrCodeRequired: "is required", + zconst.ErrCodeMin: "string must contain at least {{min}} character(s)", + zconst.ErrCodeMax: "string must contain at most {{min}} character(s)", + zconst.ErrCodeLen: "string must be exactly {{len}} character(s)", + zconst.ErrCodeEmail: "must be a valid email", + zconst.ErrCodeURL: "must be a valid URL", + zconst.ErrCodeHasPrefix: "string must start with {{prefix}}", + zconst.ErrCodeHasSuffix: "string must end with {{suffix}}", + zconst.ErrCodeContains: "string must contain {{contained}}", + zconst.ErrCodeContainsDigit: "string must contain at least one digit", + zconst.ErrCodeContainsUpper: "string must contain at least one uppercase letter", + zconst.ErrCodeContainsLower: "string must contain at least one lowercase letter", + zconst.ErrCodeContainsSpecial: "string must contain at least one special character", + zconst.ErrCodeOneOf: "string must be one of {{one_of_options}}", }, - p.TypeBool: { - p.ErrCodeRequired: "is required", - p.ErrCodeTrue: "must be true", - p.ErrCodeFalse: "must be false", + zconst.TypeBool: { + zconst.ErrCodeRequired: "is required", + zconst.ErrCodeTrue: "must be true", + zconst.ErrCodeFalse: "must be false", }, - p.TypeNumber: { - p.ErrCodeRequired: "is required", - p.ErrCodeLTE: "number must be less than or equal to {{lte}}", - p.ErrCodeLT: "number must be less than {{lt}}", - p.ErrCodeGTE: "number must be greater than or equal to {{gte}}", - p.ErrCodeGT: "number must be greater than {{gt}}", - p.ErrCodeEQ: "number must be equal to {{eq}}", - p.ErrCodeOneOf: "number must be one of {{options}}", + zconst.TypeNumber: { + zconst.ErrCodeRequired: "is required", + zconst.ErrCodeLTE: "number must be less than or equal to {{lte}}", + zconst.ErrCodeLT: "number must be less than {{lt}}", + zconst.ErrCodeGTE: "number must be greater than or equal to {{gte}}", + zconst.ErrCodeGT: "number must be greater than {{gt}}", + zconst.ErrCodeEQ: "number must be equal to {{eq}}", + zconst.ErrCodeOneOf: "number must be one of {{options}}", }, - p.TypeTime: { - p.ErrCodeRequired: "is required", - p.ErrCodeAfter: "time must be after {{after}}", - p.ErrCodeBefore: "time must be before {{before}}", - p.ErrCodeEQ: "time must be equal to {{eq}}", + zconst.TypeTime: { + zconst.ErrCodeRequired: "is required", + zconst.ErrCodeAfter: "time must be after {{after}}", + zconst.ErrCodeBefore: "time must be before {{before}}", + zconst.ErrCodeEQ: "time must be equal to {{eq}}", }, - p.TypeSlice: { - p.ErrCodeRequired: "is required", - p.ErrCodeMin: "slice must contain at least {{min}} items", - p.ErrCodeMax: "slice must contain at most {{max}} items", - p.ErrCodeLen: "slice must contain exactly {{len}} items", - p.ErrCodeContains: "slice must contain {{contained}}", + zconst.TypeSlice: { + zconst.ErrCodeRequired: "is required", + zconst.ErrCodeMin: "slice must contain at least {{min}} items", + zconst.ErrCodeMax: "slice must contain at most {{max}} items", + zconst.ErrCodeLen: "slice must contain exactly {{len}} items", + zconst.ErrCodeContains: "slice must contain {{contained}}", }, - p.TypeStruct: { - p.ErrCodeRequired: "is required", + zconst.TypeStruct: { + zconst.ErrCodeRequired: "is required", }, } diff --git a/conf/Errors_test.go b/conf/Errors_test.go index 12c471c..fb03597 100644 --- a/conf/Errors_test.go +++ b/conf/Errors_test.go @@ -3,7 +3,8 @@ package conf import ( "testing" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + zconst "github.com/Oudwins/zog/zconst" "github.com/stretchr/testify/assert" ) @@ -12,9 +13,9 @@ func TestDefaultErrorFormatter(t *testing.T) { input p.ZogError want string }{ - {input: &p.ZogErr{C: p.ErrCodeRequired, Typ: p.TypeString}, want: DefaultErrMsgMap[p.TypeString][p.ErrCodeRequired]}, - {input: &p.ZogErr{C: p.ErrCodeRequired, Typ: p.TypeString, Msg: "DON'T OVERRIDE ME"}, want: "DON'T OVERRIDE ME"}, - {input: &p.ZogErr{C: "INVALID_ERR_CODE", Typ: p.TypeString}, want: "string is invalid"}, + {input: &p.ZogErr{C: zconst.ErrCodeRequired, Typ: zconst.TypeString}, want: DefaultErrMsgMap[zconst.TypeString][zconst.ErrCodeRequired]}, + {input: &p.ZogErr{C: zconst.ErrCodeRequired, Typ: zconst.TypeString, Msg: "DON'T OVERRIDE ME"}, want: "DON'T OVERRIDE ME"}, + {input: &p.ZogErr{C: "INVALID_ERR_CODE", Typ: zconst.TypeString}, want: "string is invalid"}, } for _, test := range tests { diff --git a/primitives/DataProviders.go b/internals/DataProviders.go similarity index 99% rename from primitives/DataProviders.go rename to internals/DataProviders.go index 16f01af..cd931f0 100644 --- a/primitives/DataProviders.go +++ b/internals/DataProviders.go @@ -1,4 +1,4 @@ -package primitives +package internals import "reflect" diff --git a/primitives/PathBuilder.go b/internals/PathBuilder.go similarity index 95% rename from primitives/PathBuilder.go rename to internals/PathBuilder.go index 2074ac9..95883e1 100644 --- a/primitives/PathBuilder.go +++ b/internals/PathBuilder.go @@ -1,4 +1,4 @@ -package primitives +package internals import "strings" diff --git a/primitives/errors.go b/internals/errors.go similarity index 86% rename from primitives/errors.go rename to internals/errors.go index 192cc09..18f70cf 100644 --- a/primitives/errors.go +++ b/internals/errors.go @@ -1,8 +1,12 @@ -package primitives +package internals + +import ( + zconst "github.com/Oudwins/zog/zconst" +) // Error interface returned from all processors type ZogError interface { - Code() ZogErrCode + Code() zconst.ZogErrCode Value() any // the error value Dtype() string // destination type Params() map[string]any @@ -19,16 +23,16 @@ type ErrFmtFunc = func(e ZogError, p ParseCtx) // Error implementation type ZogErr struct { - C ZogErrCode // error code - ParamsM map[string]any // params for the error (e.g. min, max, len, etc) - Typ string // destination type - Val any // value that caused the error + C zconst.ZogErrCode // error code + ParamsM map[string]any // params for the error (e.g. min, max, len, etc) + Typ string // destination type + Val any // value that caused the error Msg string Err error // the underlying error } // error code, err uuid -func (e *ZogErr) Code() ZogErrCode { +func (e *ZogErr) Code() zconst.ZogErrCode { return e.C } diff --git a/primitives/genericTests.go b/internals/genericTests.go similarity index 78% rename from primitives/genericTests.go rename to internals/genericTests.go index 4c21d2b..2fc519e 100644 --- a/primitives/genericTests.go +++ b/internals/genericTests.go @@ -1,14 +1,15 @@ -package primitives +package internals import ( "reflect" + zconst "github.com/Oudwins/zog/zconst" "golang.org/x/exp/constraints" ) func Required() Test { t := Test{ - ErrCode: ErrCodeRequired, + ErrCode: zconst.ErrCodeRequired, ValidateFunc: func(val any, ctx ParseCtx) bool { return !IsZeroValue(val) }, @@ -22,20 +23,20 @@ type LengthCapable[K any] interface { func LenMin[T LengthCapable[any]](n int) Test { t := Test{ - ErrCode: ErrCodeMin, + ErrCode: zconst.ErrCodeMin, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { x := val.(T) return len(x) >= n }, } - t.Params[ErrCodeMin] = n + t.Params[zconst.ErrCodeMin] = n return t } func LenMax[T LengthCapable[any]](n int) Test { t := Test{ - ErrCode: ErrCodeMax, + ErrCode: zconst.ErrCodeMax, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(T) @@ -45,13 +46,13 @@ func LenMax[T LengthCapable[any]](n int) Test { return len(val) <= n }, } - t.Params[ErrCodeMax] = n + t.Params[zconst.ErrCodeMax] = n return t } func Len[T LengthCapable[any]](n int) Test { t := Test{ - ErrCode: ErrCodeLen, + ErrCode: zconst.ErrCodeLen, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(T) @@ -61,13 +62,13 @@ func Len[T LengthCapable[any]](n int) Test { return len(val) == n }, } - t.Params[ErrCodeLen] = n + t.Params[zconst.ErrCodeLen] = n return t } func In[T any](values []T) Test { t := Test{ - ErrCode: ErrCodeOneOf, + ErrCode: zconst.ErrCodeOneOf, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { for _, value := range values { @@ -79,13 +80,13 @@ func In[T any](values []T) Test { return false }, } - t.Params[ErrCodeOneOf] = values + t.Params[zconst.ErrCodeOneOf] = values return t } func EQ[T comparable](n T) Test { t := Test{ - ErrCode: ErrCodeEQ, + ErrCode: zconst.ErrCodeEQ, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { v, ok := val.(T) @@ -95,13 +96,13 @@ func EQ[T comparable](n T) Test { return v == n }, } - t.Params[ErrCodeEQ] = n + t.Params[zconst.ErrCodeEQ] = n return t } func LTE[T constraints.Ordered](n T) Test { t := Test{ - ErrCode: ErrCodeLTE, + ErrCode: zconst.ErrCodeLTE, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { v, ok := val.(T) @@ -111,13 +112,13 @@ func LTE[T constraints.Ordered](n T) Test { return v <= n }, } - t.Params[ErrCodeLTE] = n + t.Params[zconst.ErrCodeLTE] = n return t } func GTE[T constraints.Ordered](n T) Test { t := Test{ - ErrCode: ErrCodeGTE, + ErrCode: zconst.ErrCodeGTE, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { v, ok := val.(T) @@ -127,13 +128,13 @@ func GTE[T constraints.Ordered](n T) Test { return v >= n }, } - t.Params[ErrCodeGTE] = n + t.Params[zconst.ErrCodeGTE] = n return t } func LT[T constraints.Ordered](n T) Test { t := Test{ - ErrCode: ErrCodeLT, + ErrCode: zconst.ErrCodeLT, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { v, ok := val.(T) @@ -143,13 +144,13 @@ func LT[T constraints.Ordered](n T) Test { return v < n }, } - t.Params[ErrCodeLT] = n + t.Params[zconst.ErrCodeLT] = n return t } func GT[T constraints.Ordered](n T) Test { t := Test{ - ErrCode: ErrCodeGT, + ErrCode: zconst.ErrCodeGT, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { v, ok := val.(T) @@ -159,6 +160,6 @@ func GT[T constraints.Ordered](n T) Test { return v > n }, } - t.Params[ErrCodeGT] = n + t.Params[zconst.ErrCodeGT] = n return t } diff --git a/primitives/parsing.go b/internals/parsing.go similarity index 91% rename from primitives/parsing.go rename to internals/parsing.go index 6926f4a..b9850d9 100644 --- a/primitives/parsing.go +++ b/internals/parsing.go @@ -1,4 +1,6 @@ -package primitives +package internals + +import zconst "github.com/Oudwins/zog/zconst" type ZogParseCtx struct { Fmter ErrFmtFunc @@ -49,7 +51,7 @@ func NewParseCtx(errs ZogErrors, fmter ErrFmtFunc) *ZogParseCtx { type TestFunc = func(val any, ctx ParseCtx) bool type Test struct { - ErrCode ZogErrCode + ErrCode zconst.ZogErrCode Params map[string]any ErrFmt ErrFmtFunc ValidateFunc TestFunc diff --git a/primitives/types.go b/internals/types.go similarity index 96% rename from primitives/types.go rename to internals/types.go index 6677e64..b7795bb 100644 --- a/primitives/types.go +++ b/internals/types.go @@ -1,4 +1,4 @@ -package primitives +package internals import "time" diff --git a/primitives/utils.go b/internals/utils.go similarity index 94% rename from primitives/utils.go rename to internals/utils.go index ddc9b7c..e89598e 100644 --- a/primitives/utils.go +++ b/internals/utils.go @@ -1,4 +1,4 @@ -package primitives +package internals import "reflect" diff --git a/numbers.go b/numbers.go index cec9c97..be8b4e3 100644 --- a/numbers.go +++ b/numbers.go @@ -2,7 +2,7 @@ package zog import ( "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" ) type Numeric interface { diff --git a/slices.go b/slices.go index ee9c492..1417aac 100644 --- a/slices.go +++ b/slices.go @@ -5,7 +5,8 @@ import ( "reflect" "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + "github.com/Oudwins/zog/zconst" ) type sliceProcessor struct { @@ -39,7 +40,7 @@ func (v *sliceProcessor) Parse(data any, dest any, options ...ParsingOption) p.Z } func (v *sliceProcessor) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) { - destType := p.TypeSlice + destType := zconst.TypeSlice // 1. preTransforms if v.preTransforms != nil { for _, fn := range v.preTransforms { @@ -86,7 +87,7 @@ func (v *sliceProcessor) process(val any, dest any, path p.PathBuilder, ctx Pars // make sure val is a slice if not try to make it one v, err := conf.Coercers.Slice(val) if err != nil { - ctx.NewError(path, Errors.New(p.ErrCodeCoerce, val, destType, nil, "", err)) + ctx.NewError(path, Errors.New(zconst.ErrCodeCoerce, val, destType, nil, "", err)) return } refVal = reflect.ValueOf(v) @@ -217,7 +218,7 @@ func (v *sliceProcessor) Len(n int, options ...TestOption) *sliceProcessor { func (v *sliceProcessor) Contains(value any, options ...TestOption) *sliceProcessor { v.tests = append(v.tests, p.Test{ - ErrCode: p.ErrCodeContains, + ErrCode: zconst.ErrCodeContains, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { rv := reflect.ValueOf(val).Elem() @@ -236,7 +237,7 @@ func (v *sliceProcessor) Contains(value any, options ...TestOption) *sliceProces }, }, ) - v.tests[len(v.tests)-1].Params[p.ErrCodeContains] = value + v.tests[len(v.tests)-1].Params[zconst.ErrCodeContains] = value for _, opt := range options { opt(&v.tests[len(v.tests)-1]) } @@ -245,7 +246,7 @@ func (v *sliceProcessor) Contains(value any, options ...TestOption) *sliceProces func sliceMin(n int) p.Test { t := p.Test{ - ErrCode: p.ErrCodeMin, + ErrCode: zconst.ErrCodeMin, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { rv := reflect.ValueOf(val).Elem() @@ -255,12 +256,12 @@ func sliceMin(n int) p.Test { return rv.Len() >= n }, } - t.Params[p.ErrCodeMin] = n + t.Params[zconst.ErrCodeMin] = n return t } func sliceMax(n int) p.Test { t := p.Test{ - ErrCode: p.ErrCodeMax, + ErrCode: zconst.ErrCodeMax, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { rv := reflect.ValueOf(val).Elem() @@ -270,12 +271,12 @@ func sliceMax(n int) p.Test { return rv.Len() <= n }, } - t.Params[p.ErrCodeMax] = n + t.Params[zconst.ErrCodeMax] = n return t } func sliceLength(n int) p.Test { t := p.Test{ - ErrCode: p.ErrCodeLen, + ErrCode: zconst.ErrCodeLen, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx ParseCtx) bool { rv := reflect.ValueOf(val).Elem() @@ -285,6 +286,6 @@ func sliceLength(n int) p.Test { return rv.Len() == n }, } - t.Params[p.ErrCodeLen] = n + t.Params[zconst.ErrCodeLen] = n return t } diff --git a/string.go b/string.go index 073720e..91a4102 100644 --- a/string.go +++ b/string.go @@ -6,7 +6,8 @@ import ( "strings" "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + "github.com/Oudwins/zog/zconst" ) var ( @@ -146,7 +147,7 @@ func (v *stringProcessor) Len(n int, options ...TestOption) *stringProcessor { // checks that the value is a valid email address func (v *stringProcessor) Email(options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeEmail, + ErrCode: zconst.ErrCodeEmail, ValidateFunc: func(v any, ctx ParseCtx) bool { email, ok := v.(string) if !ok { @@ -164,7 +165,7 @@ func (v *stringProcessor) Email(options ...TestOption) *stringProcessor { func (v *stringProcessor) URL(options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeURL, + ErrCode: zconst.ErrCodeURL, ValidateFunc: func(v any, ctx ParseCtx) bool { s, ok := v.(string) if !ok { @@ -183,7 +184,7 @@ func (v *stringProcessor) URL(options ...TestOption) *stringProcessor { func (v *stringProcessor) HasPrefix(s string, options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeHasPrefix, + ErrCode: zconst.ErrCodeHasPrefix, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) @@ -193,7 +194,7 @@ func (v *stringProcessor) HasPrefix(s string, options ...TestOption) *stringProc return strings.HasPrefix(val, s) }, } - t.Params[p.ErrCodeHasPrefix] = s + t.Params[zconst.ErrCodeHasPrefix] = s for _, opt := range options { opt(&t) } @@ -203,7 +204,7 @@ func (v *stringProcessor) HasPrefix(s string, options ...TestOption) *stringProc func (v *stringProcessor) HasSuffix(s string, options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeHasSuffix, + ErrCode: zconst.ErrCodeHasSuffix, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) @@ -213,7 +214,7 @@ func (v *stringProcessor) HasSuffix(s string, options ...TestOption) *stringProc return strings.HasSuffix(val, s) }, } - t.Params[p.ErrCodeHasSuffix] = s + t.Params[zconst.ErrCodeHasSuffix] = s for _, opt := range options { opt(&t) } @@ -223,7 +224,7 @@ func (v *stringProcessor) HasSuffix(s string, options ...TestOption) *stringProc func (v *stringProcessor) Contains(sub string, options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeContains, + ErrCode: zconst.ErrCodeContains, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) @@ -233,7 +234,7 @@ func (v *stringProcessor) Contains(sub string, options ...TestOption) *stringPro return strings.Contains(val, sub) }, } - t.Params[p.ErrCodeContains] = sub + t.Params[zconst.ErrCodeContains] = sub for _, opt := range options { opt(&t) } @@ -243,7 +244,7 @@ func (v *stringProcessor) Contains(sub string, options ...TestOption) *stringPro func (v *stringProcessor) ContainsUpper(options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeContainsUpper, + ErrCode: zconst.ErrCodeContainsUpper, ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) if !ok { @@ -266,7 +267,7 @@ func (v *stringProcessor) ContainsUpper(options ...TestOption) *stringProcessor func (v *stringProcessor) ContainsDigit(options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeContainsDigit, + ErrCode: zconst.ErrCodeContainsDigit, ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) if !ok { @@ -292,7 +293,7 @@ func (v *stringProcessor) ContainsDigit(options ...TestOption) *stringProcessor func (v *stringProcessor) ContainsSpecial(options ...TestOption) *stringProcessor { t := p.Test{ - ErrCode: p.ErrCodeContainsSpecial, + ErrCode: zconst.ErrCodeContainsSpecial, ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(string) if !ok { diff --git a/struct.go b/struct.go index d3bd993..b7eed4d 100644 --- a/struct.go +++ b/struct.go @@ -8,7 +8,8 @@ import ( "strings" "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + "github.com/Oudwins/zog/zconst" ) type StructParser interface { @@ -83,7 +84,7 @@ func (v *structProcessor) Parse(data any, destPtr any, options ...ParsingOption) } func (v *structProcessor) process(data any, dest any, path p.PathBuilder, ctx ParseCtx) { - destType := p.TypeStruct + destType := zconst.TypeStruct // 1. preTransforms if v.preTransforms != nil { for _, fn := range v.preTransforms { @@ -126,7 +127,7 @@ func (v *structProcessor) process(data any, dest any, path p.PathBuilder, ctx Pa dataProv, ok := data.(p.DataProvider) if !ok { if dataProv, ok = p.TryNewAnyDataProvider(data); !ok { - ctx.NewError(path, Errors.New(p.ErrCodeCoerce, data, destType, nil, "", errors.New("could not convert data to a data provider"))) + ctx.NewError(path, Errors.New(zconst.ErrCodeCoerce, data, destType, nil, "", errors.New("could not convert data to a data provider"))) return } } @@ -145,7 +146,7 @@ func (v *structProcessor) process(data any, dest any, path p.PathBuilder, ctx Pa } destPtr := structVal.FieldByName(key).Addr().Interface() - fieldTag, ok := fieldMeta.Tag.Lookup(p.ZogTag) + fieldTag, ok := fieldMeta.Tag.Lookup(zconst.ZogTag) if ok { fieldKey = fieldTag } diff --git a/struct_test.go b/struct_test.go index fb9980b..634bc4a 100644 --- a/struct_test.go +++ b/struct_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" "github.com/stretchr/testify/assert" ) diff --git a/time.go b/time.go index 2ac4600..77d191a 100644 --- a/time.go +++ b/time.go @@ -4,7 +4,8 @@ import ( "time" "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + "github.com/Oudwins/zog/zconst" ) type timeProcessor struct { @@ -103,7 +104,7 @@ func (v *timeProcessor) Test(t p.Test, opts ...TestOption) *timeProcessor { // Checks that the value is after the given time func (v *timeProcessor) After(t time.Time) *timeProcessor { r := p.Test{ - ErrCode: p.ErrCodeAfter, + ErrCode: zconst.ErrCodeAfter, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(time.Time) @@ -113,7 +114,7 @@ func (v *timeProcessor) After(t time.Time) *timeProcessor { return val.After(t) }, } - r.Params[p.ErrCodeAfter] = t + r.Params[zconst.ErrCodeAfter] = t for _, opt := range v.tests { r.ErrFmt = opt.ErrFmt } @@ -125,7 +126,7 @@ func (v *timeProcessor) After(t time.Time) *timeProcessor { func (v *timeProcessor) Before(t time.Time) *timeProcessor { r := p.Test{ - ErrCode: p.ErrCodeBefore, + ErrCode: zconst.ErrCodeBefore, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(time.Time) @@ -135,7 +136,7 @@ func (v *timeProcessor) Before(t time.Time) *timeProcessor { return val.Before(t) }, } - r.Params[p.ErrCodeBefore] = t + r.Params[zconst.ErrCodeBefore] = t for _, opt := range v.tests { r.ErrFmt = opt.ErrFmt } @@ -147,7 +148,7 @@ func (v *timeProcessor) Before(t time.Time) *timeProcessor { // Checks that the value is equal to the given time func (v *timeProcessor) EQ(t time.Time) *timeProcessor { r := p.Test{ - ErrCode: p.ErrCodeEQ, + ErrCode: zconst.ErrCodeEQ, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx ParseCtx) bool { val, ok := v.(time.Time) @@ -157,7 +158,7 @@ func (v *timeProcessor) EQ(t time.Time) *timeProcessor { return val.Equal(t) }, } - r.Params[p.ErrCodeEQ] = t + r.Params[zconst.ErrCodeEQ] = t for _, opt := range v.tests { r.ErrFmt = opt.ErrFmt } diff --git a/utils.go b/utils.go index 36dfa7d..6ef42b5 100644 --- a/utils.go +++ b/utils.go @@ -6,7 +6,8 @@ import ( "time" "github.com/Oudwins/zog/conf" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" + "github.com/Oudwins/zog/zconst" ) type Processor interface { @@ -23,7 +24,7 @@ type ZogErrMap = p.ZogErrMap type ZogErrList = p.ZogErrList // ! TESTS -func TestFunc(errCode p.ZogErrCode, validateFunc p.TestFunc) p.Test { +func TestFunc(errCode zconst.ZogErrCode, validateFunc p.TestFunc) p.Test { t := p.Test{ ErrCode: errCode, ValidateFunc: validateFunc, @@ -39,7 +40,7 @@ type errHelpers struct { var Errors = errHelpers{} // Create error from (originValue any, destinationValue any, test *p.Test) -func (e *errHelpers) FromTest(o any, destType p.ZogType, t *p.Test, p ParseCtx) p.ZogError { +func (e *errHelpers) FromTest(o any, destType zconst.ZogType, t *p.Test, p ParseCtx) p.ZogError { er := e.New(t.ErrCode, o, destType, t.Params, "", nil) if t.ErrFmt != nil { t.ErrFmt(er, p) @@ -48,11 +49,11 @@ func (e *errHelpers) FromTest(o any, destType p.ZogType, t *p.Test, p ParseCtx) } // Create error from -func (e *errHelpers) FromErr(o any, destType p.ZogType, err error) p.ZogError { - return e.New(p.ErrCodeCustom, o, destType, nil, "", err) +func (e *errHelpers) FromErr(o any, destType zconst.ZogType, err error) p.ZogError { + return e.New(zconst.ErrCodeCustom, o, destType, nil, "", err) } -func (e *errHelpers) WrapUnknown(o any, destType p.ZogType, err error) p.ZogError { +func (e *errHelpers) WrapUnknown(o any, destType zconst.ZogType, err error) p.ZogError { zerr, ok := err.(p.ZogError) if !ok { return e.FromErr(o, destType, err) @@ -60,7 +61,7 @@ func (e *errHelpers) WrapUnknown(o any, destType p.ZogType, err error) p.ZogErro return zerr } -func (e *errHelpers) New(code p.ZogErrCode, o any, destType p.ZogType, params map[string]any, msg string, err error) p.ZogError { +func (e *errHelpers) New(code zconst.ZogErrCode, o any, destType zconst.ZogType, params map[string]any, msg string, err error) p.ZogError { return &p.ZogErr{ C: code, ParamsM: params, @@ -96,27 +97,27 @@ func NewMapDataProvider[T any](m map[string]T) p.DataProvider { // ! PRIMITIVE PROCESSING -func getDestType(dest any) p.ZogType { +func getDestType(dest any) zconst.ZogType { switch reflect.TypeOf(dest).Kind() { case reflect.Slice: - return p.TypeSlice + return zconst.TypeSlice case reflect.Struct: if reflect.TypeOf(dest) == reflect.TypeOf(time.Time{}) { - return p.TypeTime + return zconst.TypeTime } - return p.TypeStruct + return zconst.TypeStruct case reflect.Float64: case reflect.Int: - return p.TypeNumber + return zconst.TypeNumber case reflect.Bool: - return p.TypeBool + return zconst.TypeBool case reflect.String: - return p.TypeString + return zconst.TypeString default: log.Fatal("Unsupported destination type") } // should never get here - return p.TypeString + return zconst.TypeString } func primitiveProcessor[T p.ZogPrimitive](val any, dest any, path p.PathBuilder, ctx ParseCtx, preTransforms []p.PreTransform, tests []p.Test, postTransforms []p.PostTransform, defaultVal *T, required *p.Test, catch *T, coercer conf.CoercerFunc) { @@ -187,7 +188,7 @@ func primitiveProcessor[T p.ZogPrimitive](val any, dest any, path p.PathBuilder, *destPtr = *catch hasCatched = true } else { - ctx.NewError(path, Errors.New(p.ErrCodeCoerce, val, destType, nil, "", err)) + ctx.NewError(path, Errors.New(zconst.ErrCodeCoerce, val, destType, nil, "", err)) return } } diff --git a/utilsOptions.go b/utilsOptions.go index 86818e3..b352074 100644 --- a/utilsOptions.go +++ b/utilsOptions.go @@ -1,7 +1,7 @@ package zog import ( - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" ) type TestOption = func(test *p.Test) diff --git a/utilsOptions_test.go b/utilsOptions_test.go index 66bd793..1e8eed2 100644 --- a/utilsOptions_test.go +++ b/utilsOptions_test.go @@ -3,7 +3,7 @@ package zog import ( "testing" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" "github.com/stretchr/testify/assert" ) diff --git a/primitives/consts.go b/zconst/consts.go similarity index 98% rename from primitives/consts.go rename to zconst/consts.go index 99aacc6..2c0e9ad 100644 --- a/primitives/consts.go +++ b/zconst/consts.go @@ -1,4 +1,4 @@ -package primitives +package zconst const ( ZogTag = "zog" diff --git a/zenv/zenv.go b/zenv/zenv.go index 894a4a9..398a30b 100644 --- a/zenv/zenv.go +++ b/zenv/zenv.go @@ -3,7 +3,7 @@ package zenv import ( "os" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" ) type envDataProvider struct { diff --git a/zhttp/zhttp.go b/zhttp/zhttp.go index 10c86a5..0c21144 100644 --- a/zhttp/zhttp.go +++ b/zhttp/zhttp.go @@ -5,7 +5,7 @@ import ( "net/http" "net/url" - p "github.com/Oudwins/zog/primitives" + p "github.com/Oudwins/zog/internals" ) type urlDataProvider struct {