Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internals/Issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (e *ZogErr) String() string {
return fmt.Sprintf("ZogError{Code: %v, Params: %v, Type: %v, Value: %v, Message: '%v', Error: %v}", SafeString(e.C), SafeString(e.ParamsM), SafeString(e.Typ), SafeString(e.Val), SafeString(e.Msg), SafeError(e.Err))
}

func (e *ZogErr) Free() {
ZogIssuePool.Put(e)
}
Comment on lines +166 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Reset fields before returning to pool.

The Free() method should reset fields before returning the instance to the pool to prevent potential data leaks and ensure clean state for reuse.

Add field reset:

 func (e *ZogErr) Free() {
+	// Reset fields to zero values before returning to pool
+	e.C = ""
+	e.EPath = ""
+	e.ParamsM = nil
+	e.Typ = ""
+	e.Val = nil
+	e.Msg = ""
+	e.Err = nil
 	ZogIssuePool.Put(e)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (e *ZogErr) Free() {
ZogIssuePool.Put(e)
}
func (e *ZogErr) Free() {
// Reset fields to zero values before returning to pool
e.C = ""
e.EPath = ""
e.ParamsM = nil
e.Typ = ""
e.Val = nil
e.Msg = ""
e.Err = nil
ZogIssuePool.Put(e)
}


// list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)
type ZogIssueList = []ZogError

Expand Down
53 changes: 27 additions & 26 deletions internals/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,21 @@ type TestCtx struct {

func (c *SchemaCtx) Issue() ZogIssue {
// TODO handle catch here
return &ZogErr{
EPath: c.Path.String(),
Typ: c.DType,
Val: c.Val,
}
e := ZogIssuePool.Get().(*ZogErr)
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = c.Val
return e
}
Comment on lines 110 to 117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for type assertion.

The type assertion from pool.Get() could panic if the pool returns an incorrect type.

Add error handling:

 func (c *SchemaCtx) Issue() ZogIssue {
-	e := ZogIssuePool.Get().(*ZogErr)
+	e, ok := ZogIssuePool.Get().(*ZogErr)
+	if !ok {
+		// Fallback to creating a new instance if pool returns wrong type
+		e = &ZogErr{}
+	}
 	e.EPath = c.Path.String()
 	e.Typ = c.DType
 	e.Val = c.Val
 	return e
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (c *SchemaCtx) Issue() ZogIssue {
// TODO handle catch here
return &ZogErr{
EPath: c.Path.String(),
Typ: c.DType,
Val: c.Val,
}
e := ZogIssuePool.Get().(*ZogErr)
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = c.Val
return e
}
func (c *SchemaCtx) Issue() ZogIssue {
// TODO handle catch here
e, ok := ZogIssuePool.Get().(*ZogErr)
if !ok {
// Fallback to creating a new instance if pool returns wrong type
e = &ZogErr{}
}
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = c.Val
return e
}


// Please don't depend on this method it may change
func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue {
e := &ZogErr{
EPath: c.Path.String(),
Typ: c.DType,
Val: val,
C: test.IssueCode,
ParamsM: test.Params,
}
e := ZogIssuePool.Get().(*ZogErr)
e.C = test.IssueCode
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = val
e.ParamsM = test.Params
if test.IssueFmtFunc != nil {
test.IssueFmtFunc(e, c)
}
Expand All @@ -136,13 +135,13 @@ func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue {

// Please don't depend on this method it may change
func (c *SchemaCtx) IssueFromCoerce(err error) ZogIssue {
return &ZogErr{
C: zconst.IssueCodeCoerce,
EPath: c.Path.String(),
Typ: c.DType,
Val: c.Val,
Err: err,
}
e := ZogIssuePool.Get().(*ZogErr)
e.C = zconst.IssueCodeCoerce
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = c.Val
e.Err = err
return e
}

// Please don't depend on this method it may change
Expand All @@ -162,13 +161,15 @@ func (c *SchemaCtx) Free() {

func (c *TestCtx) Issue() ZogIssue {
// TODO handle catch here
return &ZogErr{
EPath: c.Path.String(),
Typ: c.DType,
Val: c.Val,
C: c.Test.IssueCode,
ParamsM: c.Test.Params,
}
zerr := ZogIssuePool.Get().(*ZogErr)
zerr.C = c.Test.IssueCode
zerr.EPath = c.Path.String()
zerr.Err = nil
zerr.Msg = ""
zerr.ParamsM = c.Test.Params
zerr.Typ = c.DType
zerr.Val = c.Val
return zerr
}

func (c *TestCtx) FmtErr(e ZogIssue) {
Expand Down
11 changes: 11 additions & 0 deletions internals/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var InternalIssueMapPool = sync.Pool{
},
}

var ZogIssuePool = sync.Pool{
New: func() any {
return &ZogErr{}
},
}

func ClearPools() {
ExecCtxPool = sync.Pool{
New: func() any {
Expand All @@ -47,6 +53,11 @@ func ClearPools() {
return &ErrsMap{}
},
}
ZogIssuePool = sync.Pool{
New: func() any {
return &ZogErr{}
},
}

}

Expand Down
26 changes: 26 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (i *issueHelpers) SanitizeMap(m ZogIssueMap) map[string][]string {
return errs
}

func (i issueHelpers) SanitizeMapAndCollect(m ZogIssueMap) map[string][]string {
errs := i.SanitizeMap(m)
i.CollectMap(m)
return errs
}

func (i *issueHelpers) SanitizeList(l ZogIssueList) []string {
errs := make([]string, len(l))
for i, err := range l {
Expand All @@ -104,6 +110,26 @@ func (i *issueHelpers) SanitizeList(l ZogIssueList) []string {
return errs
}

func (i *issueHelpers) SanitizeListAndCollect(l ZogIssueList) []string {
errs := i.SanitizeList(l)
i.CollectList(l)
return errs
}

func (i *issueHelpers) CollectMap(issues ZogIssueMap) {
for _, list := range issues {
i.CollectList(list)
}
}

func (i *issueHelpers) CollectList(issues ZogIssueList) {
for _, err := range issues {
if err, ok := err.(*p.ZogErr); ok {
err.Free()
}
}
}

// ! ERRORS -> Deprecated
// Deprecated: This will be removed in the future.
type errHelpers struct {
Expand Down
Loading