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

Oudwins / zog / 14550890022

19 Apr 2025 04:25PM UTC coverage: 85.673% (-1.3%) from 86.942%
14550890022

push

github

web-flow
Merge pull request #145 from Oudwins/feat/preprocess

feat!: implemented preprocess and removed preTransforms

134 of 171 new or added lines in 13 files covered. (78.36%)

12 existing lines in 1 file now uncovered.

2063 of 2408 relevant lines covered (85.67%)

42.74 hits per line

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

93.55
/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  []PreTransform
15
        tests    []Test
16
        schema   ZogSchema
17
        required *Test
18
        // postTransforms []PostTransform
19
        // defaultVal     *any
20
        // catch          *any
21
}
22

23
func (v *PointerSchema) getType() zconst.ZogType {
69✔
24
        // return zconst.TypePtr
69✔
25
        return v.schema.getType()
69✔
26
}
69✔
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 {
50✔
34
        return &PointerSchema{
50✔
35
                tests:  []Test{},
50✔
36
                schema: schema,
50✔
37
        }
50✔
38
}
50✔
39

40
// Parse the data into the destination pointer
41
func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) 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
        sctx := ctx.NewSchemaCtx(data, dest, path, v.getType())
14✔
52
        defer sctx.Free()
14✔
53
        v.process(sctx)
14✔
54

14✔
55
        return errs.M
14✔
56
}
57

58
func (v *PointerSchema) process(ctx *p.SchemaCtx) {
40✔
59

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

74
        isZero := p.IsParseZeroValue(ctx.Data, ctx)
40✔
75
        if isZero {
48✔
76
                if v.required != nil {
13✔
77
                        // 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✔
78
                        ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Data).SetDType(v.schema.getType()))
5✔
79
                }
5✔
80
                return
8✔
81
        }
82
        rv := reflect.ValueOf(ctx.ValPtr)
32✔
83
        destPtr := rv.Elem()
32✔
84
        if destPtr.IsNil() {
61✔
85
                // this sets the primitive also
29✔
86
                newVal := reflect.New(destPtr.Type().Elem())
29✔
87
                // this generates a new nil pointer
29✔
88
                //newVal := reflect.Zero(destPtr.Type())
29✔
89
                destPtr.Set(newVal)
29✔
90
        }
29✔
91
        di := destPtr.Interface()
32✔
92
        subCtx.ValPtr = di
32✔
93
        v.schema.process(subCtx)
32✔
94
}
95

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

111
func (v *PointerSchema) validate(ctx *p.SchemaCtx) {
36✔
112
        rv := reflect.ValueOf(ctx.ValPtr)
36✔
113
        destPtr := rv.Elem()
36✔
114
        if !destPtr.IsValid() || destPtr.IsNil() {
44✔
115
                if v.required != nil {
12✔
116
                        // 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✔
117
                        ctx.AddIssue(ctx.IssueFromTest(v.required, ctx.Data).SetDType(v.schema.getType()))
4✔
118
                }
4✔
119
                return
8✔
120
        }
121
        di := destPtr.Interface()
27✔
122
        ctx.ValPtr = di
27✔
123
        v.schema.validate(ctx.NewValidateSchemaCtx(di, ctx.Path, v.schema.getType()))
27✔
124
}
125

126
// Validate Existing Pointer
127

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

© 2026 Coveralls, Inc