Skip to content

Commit

Permalink
fix: Schemas are now public. New complex & primtive schema interfaces (
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins authored Jan 13, 2025
1 parent a4f90d3 commit 9e659e5
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 138 deletions.
32 changes: 16 additions & 16 deletions boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/Oudwins/zog/zconst"
)

var _ ZogSchema = &boolProcessor{}
var _ PrimitiveZogSchema[bool] = &BoolSchema{}

type boolProcessor struct {
type BoolSchema struct {
preTransforms []p.PreTransform
tests []p.Test
postTransforms []p.PostTransform
Expand All @@ -21,25 +21,25 @@ type boolProcessor struct {
// ! INTERNALS

// Returns the type of the schema
func (v *boolProcessor) getType() zconst.ZogType {
func (v *BoolSchema) getType() zconst.ZogType {
return zconst.TypeBool
}

// Sets the coercer for the schema
func (v *boolProcessor) setCoercer(c conf.CoercerFunc) {
func (v *BoolSchema) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}

// Internal function to process the data
func (v *boolProcessor) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
func (v *BoolSchema) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
primitiveProcessor(val, dest, path, ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}

// ! USER FACING FUNCTIONS

// Returns a new Bool Schema
func Bool(opts ...SchemaOption) *boolProcessor {
b := &boolProcessor{
func Bool(opts ...SchemaOption) *BoolSchema {
b := &BoolSchema{
coercer: conf.Coercers.Bool, // default coercer
}
for _, opt := range opts {
Expand All @@ -48,7 +48,7 @@ func Bool(opts ...SchemaOption) *boolProcessor {
return b
}

func (v *boolProcessor) Parse(data any, dest *bool, options ...ParsingOption) p.ZogErrList {
func (v *BoolSchema) Parse(data any, dest *bool, options ...ParsingOption) p.ZogErrList {
errs := p.NewErrsList()
ctx := p.NewParseCtx(errs, conf.ErrorFormatter)
for _, opt := range options {
Expand All @@ -64,7 +64,7 @@ func (v *boolProcessor) Parse(data any, dest *bool, options ...ParsingOption) p.
// GLOBAL METHODS

// Adds pretransform function to schema
func (v *boolProcessor) PreTransform(transform p.PreTransform) *boolProcessor {
func (v *BoolSchema) PreTransform(transform p.PreTransform) *BoolSchema {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
Expand All @@ -73,7 +73,7 @@ func (v *boolProcessor) PreTransform(transform p.PreTransform) *boolProcessor {
}

// Adds posttransform function to schema
func (v *boolProcessor) PostTransform(transform p.PostTransform) *boolProcessor {
func (v *BoolSchema) PostTransform(transform p.PostTransform) *BoolSchema {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
Expand All @@ -83,7 +83,7 @@ func (v *boolProcessor) PostTransform(transform p.PostTransform) *boolProcessor

// ! MODIFIERS
// marks field as required
func (v *boolProcessor) Required(options ...TestOption) *boolProcessor {
func (v *BoolSchema) Required(options ...TestOption) *BoolSchema {
r := p.Required()
for _, opt := range options {
opt(&r)
Expand All @@ -93,31 +93,31 @@ func (v *boolProcessor) Required(options ...TestOption) *boolProcessor {
}

// marks field as optional
func (v *boolProcessor) Optional() *boolProcessor {
func (v *BoolSchema) Optional() *BoolSchema {
v.required = nil
return v
}

// sets the default value
func (v *boolProcessor) Default(val bool) *boolProcessor {
func (v *BoolSchema) Default(val bool) *BoolSchema {
v.defaultVal = &val
return v
}

// sets the catch value (i.e the value to use if the validation fails)
func (v *boolProcessor) Catch(val bool) *boolProcessor {
func (v *BoolSchema) Catch(val bool) *BoolSchema {
v.catch = &val
return v
}

// UNIQUE METHODS

func (v *boolProcessor) True() *boolProcessor {
func (v *BoolSchema) True() *BoolSchema {
v.tests = append(v.tests, p.EQ[bool](true))
return v
}

func (v *boolProcessor) False() *boolProcessor {
func (v *BoolSchema) False() *BoolSchema {
v.tests = append(v.tests, p.EQ[bool](false))
return v
}
46 changes: 23 additions & 23 deletions numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type Numeric interface {
~int | ~float64
}

var _ ZogSchema = &numberProcessor[int]{}
var _ PrimitiveZogSchema[int] = &NumberSchema[int]{}

type numberProcessor[T Numeric] struct {
type NumberSchema[T Numeric] struct {
preTransforms []p.PreTransform
tests []p.Test
postTransforms []p.PostTransform
Expand All @@ -25,25 +25,25 @@ type numberProcessor[T Numeric] struct {
// ! INTERNALS

// Returns the type of the schema
func (v *numberProcessor[T]) getType() zconst.ZogType {
func (v *NumberSchema[T]) getType() zconst.ZogType {
return zconst.TypeNumber
}

// Sets the coercer for the schema
func (v *numberProcessor[T]) setCoercer(c conf.CoercerFunc) {
func (v *NumberSchema[T]) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}

// Internal function to process the data
func (v *numberProcessor[T]) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
func (v *NumberSchema[T]) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
primitiveProcessor(val, dest, path, ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}

// ! USER FACING FUNCTIONS

// creates a new float64 schema
func Float(opts ...SchemaOption) *numberProcessor[float64] {
s := &numberProcessor[float64]{
func Float(opts ...SchemaOption) *NumberSchema[float64] {
s := &NumberSchema[float64]{
coercer: conf.Coercers.Float64,
}
for _, opt := range opts {
Expand All @@ -53,8 +53,8 @@ func Float(opts ...SchemaOption) *numberProcessor[float64] {
}

// creates a new int schema
func Int(opts ...SchemaOption) *numberProcessor[int] {
s := &numberProcessor[int]{
func Int(opts ...SchemaOption) *NumberSchema[int] {
s := &NumberSchema[int]{
coercer: conf.Coercers.Int,
}
for _, opt := range opts {
Expand All @@ -64,7 +64,7 @@ func Int(opts ...SchemaOption) *numberProcessor[int] {
}

// parses the value and stores it in the destination
func (v *numberProcessor[T]) Parse(data any, dest *T, options ...ParsingOption) p.ZogErrList {
func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ParsingOption) p.ZogErrList {
errs := p.NewErrsList()
ctx := p.NewParseCtx(errs, conf.ErrorFormatter)
for _, opt := range options {
Expand All @@ -80,7 +80,7 @@ func (v *numberProcessor[T]) Parse(data any, dest *T, options ...ParsingOption)

// GLOBAL METHODS

func (v *numberProcessor[T]) PreTransform(transform p.PreTransform) *numberProcessor[T] {
func (v *NumberSchema[T]) PreTransform(transform p.PreTransform) *NumberSchema[T] {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
Expand All @@ -89,7 +89,7 @@ func (v *numberProcessor[T]) PreTransform(transform p.PreTransform) *numberProce
}

// Adds posttransform function to schema
func (v *numberProcessor[T]) PostTransform(transform p.PostTransform) *numberProcessor[T] {
func (v *NumberSchema[T]) PostTransform(transform p.PostTransform) *NumberSchema[T] {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
Expand All @@ -100,7 +100,7 @@ func (v *numberProcessor[T]) PostTransform(transform p.PostTransform) *numberPro
// ! MODIFIERS

// marks field as required
func (v *numberProcessor[T]) Required(options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) Required(options ...TestOption) *NumberSchema[T] {
r := p.Required()
for _, opt := range options {
opt(&r)
Expand All @@ -110,25 +110,25 @@ func (v *numberProcessor[T]) Required(options ...TestOption) *numberProcessor[T]
}

// marks field as optional
func (v *numberProcessor[T]) Optional() *numberProcessor[T] {
func (v *NumberSchema[T]) Optional() *NumberSchema[T] {
v.required = nil
return v
}

// sets the default value
func (v *numberProcessor[T]) Default(val T) *numberProcessor[T] {
func (v *NumberSchema[T]) Default(val T) *NumberSchema[T] {
v.defaultVal = &val
return v
}

// sets the catch value (i.e the value to use if the validation fails)
func (v *numberProcessor[T]) Catch(val T) *numberProcessor[T] {
func (v *NumberSchema[T]) Catch(val T) *NumberSchema[T] {
v.catch = &val
return v
}

// custom test function call it -> schema.Test(test, options)
func (v *numberProcessor[T]) Test(t p.Test, opts ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) Test(t p.Test, opts ...TestOption) *NumberSchema[T] {
for _, opt := range opts {
opt(&t)
}
Expand All @@ -139,7 +139,7 @@ func (v *numberProcessor[T]) Test(t p.Test, opts ...TestOption) *numberProcessor
// UNIQUE METHODS

// Check that the value is one of the enum values
func (v *numberProcessor[T]) OneOf(enum []T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) OneOf(enum []T, options ...TestOption) *NumberSchema[T] {
t := p.In(enum)
for _, opt := range options {
opt(&t)
Expand All @@ -149,7 +149,7 @@ func (v *numberProcessor[T]) OneOf(enum []T, options ...TestOption) *numberProce
}

// checks for equality
func (v *numberProcessor[T]) EQ(n T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) EQ(n T, options ...TestOption) *NumberSchema[T] {
t := p.EQ(n)
for _, opt := range options {
opt(&t)
Expand All @@ -159,7 +159,7 @@ func (v *numberProcessor[T]) EQ(n T, options ...TestOption) *numberProcessor[T]
}

// checks for lesser or equal
func (v *numberProcessor[T]) LTE(n T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) LTE(n T, options ...TestOption) *NumberSchema[T] {
t := p.LTE(n)
for _, opt := range options {
opt(&t)
Expand All @@ -169,7 +169,7 @@ func (v *numberProcessor[T]) LTE(n T, options ...TestOption) *numberProcessor[T]
}

// checks for greater or equal
func (v *numberProcessor[T]) GTE(n T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) GTE(n T, options ...TestOption) *NumberSchema[T] {
t := p.GTE(n)
for _, opt := range options {
opt(&t)
Expand All @@ -179,7 +179,7 @@ func (v *numberProcessor[T]) GTE(n T, options ...TestOption) *numberProcessor[T]
}

// checks for lesser
func (v *numberProcessor[T]) LT(n T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) LT(n T, options ...TestOption) *NumberSchema[T] {
t := p.LT(n)
for _, opt := range options {
opt(&t)
Expand All @@ -189,7 +189,7 @@ func (v *numberProcessor[T]) LT(n T, options ...TestOption) *numberProcessor[T]
}

// checks for greater
func (v *numberProcessor[T]) GT(n T, options ...TestOption) *numberProcessor[T] {
func (v *NumberSchema[T]) GT(n T, options ...TestOption) *NumberSchema[T] {
t := p.GT(n)
for _, opt := range options {
opt(&t)
Expand Down
18 changes: 10 additions & 8 deletions pointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/Oudwins/zog/zconst"
)

type pointerProcessor struct {
var _ ComplexZogSchema = &PointerSchema{}

type PointerSchema struct {
// preTransforms []p.PreTransform
tests []p.Test
schema ZogSchema
Expand All @@ -18,24 +20,24 @@ type pointerProcessor struct {
// catch *any
}

func (v *pointerProcessor) getType() zconst.ZogType {
func (v *PointerSchema) getType() zconst.ZogType {
return zconst.TypePtr
}

func (v *pointerProcessor) setCoercer(c conf.CoercerFunc) {
func (v *PointerSchema) setCoercer(c conf.CoercerFunc) {
v.schema.setCoercer(c)
}

// Ptr creates a pointer ZogSchema
func Ptr(schema ZogSchema) *pointerProcessor {
return &pointerProcessor{
func Ptr(schema ZogSchema) *PointerSchema {
return &PointerSchema{
tests: []p.Test{},
schema: schema,
}
}

// Parse the data into the destination pointer
func (v *pointerProcessor) Parse(data any, dest any, options ...ParsingOption) p.ZogErrMap {
func (v *PointerSchema) Parse(data any, dest any, options ...ParsingOption) p.ZogErrMap {
errs := p.NewErrsMap()
ctx := p.NewParseCtx(errs, conf.ErrorFormatter)
for _, opt := range options {
Expand All @@ -48,7 +50,7 @@ func (v *pointerProcessor) Parse(data any, dest any, options ...ParsingOption) p
return errs.M
}

func (v *pointerProcessor) process(data any, dest any, path p.PathBuilder, ctx ParseCtx) {
func (v *PointerSchema) process(data any, dest any, path p.PathBuilder, ctx ParseCtx) {
isZero := p.IsParseZeroValue(data, ctx)
if isZero {
if v.required != nil {
Expand All @@ -70,7 +72,7 @@ func (v *pointerProcessor) process(data any, dest any, path p.PathBuilder, ctx P
v.schema.process(data, di, path, ctx)
}

func (v *pointerProcessor) NotNil(options ...TestOption) *pointerProcessor {
func (v *PointerSchema) NotNil(options ...TestOption) *PointerSchema {
r := p.Test{
ErrCode: zconst.ErrCodeNotNil,
}
Expand Down
Loading

0 comments on commit 9e659e5

Please sign in to comment.