Skip to content

z.Any() type proposition #113

@udfordria

Description

@udfordria

Hello,

I would to know if there is a way to add z.any() that matches with everything in zog.

Piece of a code that matches my need :

package zog

import (
	"reflect"

	"github.com/Oudwins/zog/conf"
	p "github.com/Oudwins/zog/internals"
	"github.com/Oudwins/zog/zconst"
)

var _ ComplexZogSchema = &AnySchema{}

type AnySchema struct {
}

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

func (v *AnySchema) setCoercer(c conf.CoercerFunc) {
}

// Any creates a Any ZogSchema
func Any() *AnySchema {
	return &AnySchema{}
}

// Parse the data into the destination Any
func (v *AnySchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap {
	errs := p.NewErrsMap()
	defer errs.Free()
	ctx := p.NewExecCtx(errs, conf.IssueFormatter)
	defer ctx.Free()
	for _, opt := range options {
		opt(ctx)
	}
	path := p.NewPathBuilder()
	defer path.Free()
	v.process(ctx.NewSchemaCtx(data, dest, path, v.getType()))

	return errs.M
}

func (v *AnySchema) process(ctx *p.SchemaCtx) {
	d := reflect.ValueOf(ctx.Val)
	if !d.IsValid() {
		return
	}

	k := d.Kind()
	if (k == reflect.Ptr || k == reflect.Slice || k == reflect.Map || k == reflect.Chan) && d.IsNil() {
		return
	}

	rv := reflect.ValueOf(ctx.DestPtr)
	rv.Elem().Set(d)
}

// Validates a Any Any
func (v *AnySchema) Validate(data any, options ...ExecOption) p.ZogIssueMap {
	errs := p.NewErrsMap()
	defer errs.Free()
	ctx := p.NewExecCtx(errs, conf.IssueFormatter)
	defer ctx.Free()
	for _, opt := range options {
		opt(ctx)
	}
	path := p.NewPathBuilder()
	defer path.Free()
	v.validate(ctx.NewValidateSchemaCtx(data, path, v.getType()))
	return errs.M
}

func (v *AnySchema) validate(ctx *p.SchemaCtx) {
}

// Validate Existing Any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions