• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Oudwins / zog / 13693838411

06 Mar 2025 07:56AM UTC coverage: 85.645% (+1.1%) from 84.576%
13693838411

push

github

web-flow
refactor!: Zog issue interface is converted into a struct (#119)

* refactor!: removed deprecated errors utilities, use z.Issues instead

* refactor!: converted Issue interface into an issue map

* refactor!: removed multiple deprecated function NewDataProvider. And ZogErrList and ZogErrMap types.

81 of 117 new or added lines in 13 files covered. (69.23%)

8 existing lines in 3 files now uncovered.

1951 of 2278 relevant lines covered (85.65%)

34.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/pointers.go
1
package zog
2

3
import (
4
        "reflect"
5

6
        "github.com/Oudwins/zog/conf"
7
        p "github.com/Oudwins/zog/internals"
8
        "github.com/Oudwins/zog/zconst"
9
)
10

11
var _ ComplexZogSchema = &PointerSchema{}
12

13
type PointerSchema struct {
14
        // preTransforms  []p.PreTransform
15
        tests    []p.Test
16
        schema   ZogSchema
17
        required *p.Test
18
        // postTransforms []p.PostTransform
19
        // defaultVal     *any
20
        // catch          *any
21
}
22

23
func (v *PointerSchema) getType() zconst.ZogType {
66✔
24
        // return zconst.TypePtr
66✔
25
        return v.schema.getType()
66✔
26
}
66✔
27

28
func (v *PointerSchema) setCoercer(c conf.CoercerFunc) {
1✔
29
        v.schema.setCoercer(c)
1✔
30
}
1✔
31

32
// Ptr creates a pointer ZogSchema
33
func Ptr(schema ZogSchema) *PointerSchema {
35✔
34
        return &PointerSchema{
35✔
35
                tests:  []p.Test{},
35✔
36
                schema: schema,
35✔
37
        }
35✔
38
}
35✔
39

40
// Parse the data into the destination pointer
41
func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap {
14✔
42
        errs := p.NewErrsMap()
14✔
43
        defer errs.Free()
14✔
44
        ctx := p.NewExecCtx(errs, conf.IssueFormatter)
14✔
45
        defer ctx.Free()
14✔
46
        for _, opt := range options {
16✔
47
                opt(ctx)
2✔
48
        }
2✔
49
        path := p.NewPathBuilder()
14✔
50
        defer path.Free()
14✔
51
        v.process(ctx.NewSchemaCtx(data, dest, path, v.getType()))
14✔
52

14✔
53
        return errs.M
14✔
54
}
55

56
func (v *PointerSchema) process(ctx *p.SchemaCtx) {
32✔
57

32✔
58
        // TODO this is a mess. But couldn't figure out a simple way to support top level optional structs without doing this.
32✔
59
        // Companion code to this codde is in struct.go > process
32✔
60
        subCtx := ctx.NewSchemaCtx(ctx.Val, nil, ctx.Path, v.schema.getType())
32✔
61
        if fn, ok := ctx.Val.(p.DpFactory); ok {
32✔
NEW
62
                val, err := fn()
×
63
                if err != nil {
×
64
                        ctx.AddIssue(subCtx.IssueFromUnknownError(err))
×
65
                        return
×
66
                }
×
NEW
67
                ctx.Val = val
×
68
        }
69
        // End of messy code
70

71
        isZero := p.IsParseZeroValue(ctx.Val, ctx)
32✔
72
        if isZero {
40✔
73
                if v.required != nil {
13✔
74
                        // We set the destination type to the schema type because pointer doesn't have any issue messages. They pass through to the schema type
5✔
75
                        ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Val).SetDType(v.schema.getType()))
5✔
76
                }
5✔
77
                return
8✔
78
        }
79
        rv := reflect.ValueOf(ctx.DestPtr)
24✔
80
        destPtr := rv.Elem()
24✔
81
        if destPtr.IsNil() {
45✔
82
                // this sets the primitive also
21✔
83
                newVal := reflect.New(destPtr.Type().Elem())
21✔
84
                // this generates a new nil pointer
21✔
85
                //newVal := reflect.Zero(destPtr.Type())
21✔
86
                destPtr.Set(newVal)
21✔
87
        }
21✔
88
        di := destPtr.Interface()
24✔
89
        subCtx.DestPtr = di
24✔
90
        v.schema.process(subCtx)
24✔
91
}
92

93
// Validates a pointer pointer
94
func (v *PointerSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap {
11✔
95
        errs := p.NewErrsMap()
11✔
96
        defer errs.Free()
11✔
97
        ctx := p.NewExecCtx(errs, conf.IssueFormatter)
11✔
98
        defer ctx.Free()
11✔
99
        for _, opt := range options {
13✔
100
                opt(ctx)
2✔
101
        }
2✔
102
        path := p.NewPathBuilder()
11✔
103
        defer path.Free()
11✔
104
        v.validate(ctx.NewValidateSchemaCtx(data, path, v.getType()))
11✔
105
        return errs.M
11✔
106
}
107

108
func (v *PointerSchema) validate(ctx *p.SchemaCtx) {
28✔
109
        rv := reflect.ValueOf(ctx.Val)
28✔
110
        destPtr := rv.Elem()
28✔
111
        if !destPtr.IsValid() || destPtr.IsNil() {
36✔
112
                if v.required != nil {
12✔
113
                        // We set the destination type to the schema type because pointer doesn't have any issue messages. They pass through to the schema type
4✔
114
                        ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Val).SetDType(v.schema.getType()))
4✔
115
                }
4✔
116
                return
8✔
117
        }
118
        di := destPtr.Interface()
19✔
119
        ctx.Val = di
19✔
120
        v.schema.validate(ctx.NewValidateSchemaCtx(di, ctx.Path, v.schema.getType()))
19✔
121
}
122

123
// Validate Existing Pointer
124

125
func (v *PointerSchema) NotNil(options ...TestOption) *PointerSchema {
8✔
126
        r := p.Test{
8✔
127
                IssueCode: zconst.IssueCodeNotNil,
8✔
128
        }
8✔
129
        for _, opt := range options {
11✔
130
                opt(&r)
3✔
131
        }
3✔
132
        v.required = &r
8✔
133
        return v
8✔
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc