Skip to content

Commit 8cda5ba

Browse files
committed
wp
1 parent 347c3bb commit 8cda5ba

File tree

11 files changed

+150
-73
lines changed

11 files changed

+150
-73
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ docs-dev:
1616
cd ./docs && npm start
1717

1818
json:
19-
@go run ./cmd/toSchema/main.go
19+
@go run -tags zogmeta ./cmd/toSchema/main.go
20+

boolean.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func BoolLike[T ~bool](opts ...SchemaOption) *BoolSchema[T] {
5454
for _, opt := range opts {
5555
opt(s)
5656
}
57+
58+
if EXHAUSTIVE_METADATA {
59+
typ := getGenericTypeName[T]()
60+
registryAdd(EX_META_REGISTRY, s, "typeName", typ)
61+
}
5762
return s
5863
}
5964

cmd/toSchema/main.go

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,14 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
6-
"reflect"
7-
"strings"
85

96
z "github.com/Oudwins/zog"
10-
"github.com/Oudwins/zog/zconst"
117
)
128

139
func main() {
14-
schema := z.String().Min(1)
15-
json, err := ToJsonSchema(schema)
16-
_, _ = json, err
17-
}
18-
func ToJsonSchema(zogSchema z.ZogSchema) (string, error) {
19-
// schema := map[string]any{}
20-
Walk(zogSchema, []string{})
21-
return "", nil
22-
}
23-
24-
func Walk(zogSchema any, path []string) {
25-
refVal := reflect.ValueOf(zogSchema)
26-
// typ, _ := GetSchemaType(refVal)
27-
// fmt.Println("type found: ", typ)
28-
tests, _ := GetTests(refVal)
29-
fmt.Println("found tests", tests)
30-
}
31-
32-
// Get the tests
33-
func GetTests(s reflect.Value) ([]z.Test, error) {
34-
val := s
35-
for val.Kind() == reflect.Pointer {
36-
val = val.Elem()
37-
}
38-
testsValue := val.FieldByIndex([]int{1})
39-
tests, ok := testsValue.Interface().([]z.Test)
40-
if !ok {
41-
return nil, errors.New("Could not get tests from schema")
42-
}
43-
return tests, nil
44-
}
45-
46-
// Get the schema type
47-
func GetSchemaType(s reflect.Value) (string, error) {
48-
return getSchemaTypeViaTypeName(s)
49-
}
50-
51-
const (
52-
SchemaIdString = "zog.StringSchema["
53-
)
54-
55-
func getSchemaTypeViaTypeName(s reflect.Value) (zconst.ZogType, error) {
56-
stringType := s.Type().String()
57-
58-
if strings.Contains(stringType, SchemaIdString) {
59-
return zconst.TypeString, nil
60-
}
61-
62-
panic("unsupported schema")
63-
// return "", errors.New("Invalid schema")
64-
}
6510

66-
func getSchemaTypeViaInterface(s any) (zconst.ZogType, error) {
67-
// TODO alternative where we have an interface for each and we cast the any value to it then get the values we need from it
68-
return "", nil
11+
t := z.Time(z.Time.Format("Hello world"))
12+
fmt.Println("HELLO WORLD")
13+
fmt.Println(z.ToJson(t))
6914
}

numbers.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/Oudwins/zog/conf"
55
p "github.com/Oudwins/zog/internals"
66
"github.com/Oudwins/zog/zconst"
7+
"github.com/Oudwins/zog/zss"
78
)
89

910
type Numeric = p.Numeric
@@ -57,6 +58,10 @@ func FloatLike[T Numeric](opts ...SchemaOption) *NumberSchema[T] {
5758
for _, opt := range opts {
5859
opt(s)
5960
}
61+
if EXHAUSTIVE_METADATA {
62+
typ := getGenericTypeName[T]()
63+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, typ)
64+
}
6065
return s
6166
}
6267

@@ -67,6 +72,9 @@ func Float64(opts ...SchemaOption) *NumberSchema[float64] {
6772
for _, opt := range opts {
6873
opt(s)
6974
}
75+
if EXHAUSTIVE_METADATA {
76+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_FLOAT64)
77+
}
7078
return s
7179
}
7280

@@ -86,6 +94,9 @@ func Float32(opts ...SchemaOption) *NumberSchema[float32] {
8694
for _, opt := range opts {
8795
opt(s)
8896
}
97+
if EXHAUSTIVE_METADATA {
98+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_FLOAT32)
99+
}
89100
return s
90101
}
91102

@@ -97,6 +108,9 @@ func Int(opts ...SchemaOption) *NumberSchema[int] {
97108
for _, opt := range opts {
98109
opt(s)
99110
}
111+
if EXHAUSTIVE_METADATA {
112+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_INT)
113+
}
100114
return s
101115
}
102116

@@ -113,6 +127,10 @@ func IntLike[T Numeric](opts ...SchemaOption) *NumberSchema[T] {
113127
for _, opt := range opts {
114128
opt(s)
115129
}
130+
if EXHAUSTIVE_METADATA {
131+
typeName := getGenericTypeName[T]()
132+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, typeName)
133+
}
116134
return s
117135
}
118136

@@ -132,6 +150,9 @@ func Int64(opts ...SchemaOption) *NumberSchema[int64] {
132150
for _, opt := range opts {
133151
opt(s)
134152
}
153+
if EXHAUSTIVE_METADATA {
154+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_INT64)
155+
}
135156
return s
136157
}
137158

@@ -151,6 +172,9 @@ func Int32(opts ...SchemaOption) *NumberSchema[int32] {
151172
for _, opt := range opts {
152173
opt(s)
153174
}
175+
if EXHAUSTIVE_METADATA {
176+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_INT32)
177+
}
154178
return s
155179
}
156180

@@ -162,6 +186,10 @@ func Uint(opts ...SchemaOption) *NumberSchema[uint] {
162186
for _, opt := range opts {
163187
opt(s)
164188
}
189+
190+
if EXHAUSTIVE_METADATA {
191+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, zss.ZSS_TYPE_UINT)
192+
}
165193
return s
166194
}
167195

@@ -178,6 +206,11 @@ func UintLike[T Numeric](opts ...SchemaOption) *NumberSchema[T] {
178206
for _, opt := range opts {
179207
opt(s)
180208
}
209+
210+
if EXHAUSTIVE_METADATA {
211+
typeName := getGenericTypeName[T]()
212+
registryAdd(EX_META_REGISTRY, s, zss.ZSS_TYPE_KEY, typeName)
213+
}
181214
return s
182215
}
183216

string.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func StringLike[T likeString](opts ...SchemaOption) *StringSchema[T] {
7676
for _, opt := range opts {
7777
opt(s)
7878
}
79+
if EXHAUSTIVE_METADATA {
80+
typ := getGenericTypeName[T]()
81+
registryAdd(EX_META_REGISTRY, s, "typeName", typ)
82+
}
7983
return s
8084
}
8185

@@ -146,13 +150,20 @@ func (v *StringSchema[T]) Trim() *StringSchema[T] {
146150
return nil
147151
},
148152
})
153+
if EXHAUSTIVE_METADATA {
154+
registryAdd(EX_META_REGISTRY, v.processors[len(v.processors)-1], "ID", "trim")
155+
}
149156

150157
return v
151158
}
152159

153160
// Adds a transform function to the schema. Runs in the order it is called
154161
func (v *StringSchema[T]) Transform(transform p.Transform[*T]) *StringSchema[T] {
155162
v.processors = append(v.processors, &p.TransformProcessor[*T]{Transform: transform})
163+
164+
if EXHAUSTIVE_METADATA {
165+
registryAdd(EX_META_REGISTRY, v.processors[len(v.processors)-1], "ID", "custom")
166+
}
156167
return v
157168
}
158169

@@ -198,6 +209,10 @@ func (v *StringSchema[T]) Test(t Test[*T]) *StringSchema[T] {
198209
func (v *StringSchema[T]) TestFunc(testFunc BoolTFunc[*T], options ...TestOption) *StringSchema[T] {
199210
test := p.NewTestFunc("", p.BoolTFunc[*T](testFunc), options...)
200211
v.Test(Test[*T](*test))
212+
if EXHAUSTIVE_METADATA {
213+
registryAdd(EX_META_REGISTRY, test, "ID", "custom")
214+
registryAdd(EX_META_REGISTRY, test, "typeName", "testFunc")
215+
}
201216
return v
202217
}
203218

time.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ func (t TimeFunc) FormatFunc(format func(data string) (time.Time, error)) Schema
6262
// Usage is:
6363
// z.Time(z.Time.Format(time.RFC3339))
6464
func (t TimeFunc) Format(format string) SchemaOption {
65-
return t.FormatFunc(func(data string) (time.Time, error) {
66-
return time.Parse(format, data)
67-
})
65+
return func(s ZogSchema) {
66+
if EXHAUSTIVE_METADATA {
67+
registryAdd(EX_META_REGISTRY, s, "format", format)
68+
}
69+
s.setCoercer(conf.TimeCoercerFactory(func(data string) (time.Time, error) {
70+
return time.Parse(format, data)
71+
}))
72+
}
6873
}
6974

7075
// Parses the data into the destination time.Time. Returns a list of errors

toJson.go

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,46 @@ import (
1010
"github.com/Oudwins/zog/zconst"
1111
)
1212

13+
type ExMetaRegistry map[any]map[string]any
14+
15+
func registryAdd(r ExMetaRegistry, key any, path string, value any) {
16+
if _, ok := r[key]; !ok {
17+
r[key] = map[string]any{}
18+
}
19+
r[key][path] = value
20+
}
21+
22+
func getGenericTypeName[T any]() string {
23+
var zero T
24+
t := reflect.TypeOf(zero)
25+
26+
// If T is a pointer or interface, handle nil case
27+
if t == nil {
28+
t = reflect.TypeOf((*T)(nil)).Elem()
29+
}
30+
31+
return t.Name()
32+
}
33+
34+
func (r ExMetaRegistry) Add(key any, path string, value any) {
35+
if _, ok := r[key]; !ok {
36+
r[key] = map[string]any{}
37+
}
38+
39+
}
40+
41+
// EXPERIMENTAL. PLEASE DO NOT USE UNLESS YOU KNOW WHAT YOU ARE DOING!
42+
var EX_META_REGISTRY = map[any]map[string]any{}
43+
1344
// TODO make zog schemas for all of these to validate them!
1445
type JsonProcessor struct {
1546
Type string // "transform", "validator", "required"
1647

1748
// Validator
18-
IssueCode *string
49+
ID string // issue code or transform ID
1950
IssuePath *string
51+
Message *string
2052
Params map[string]any
21-
22-
// transform
23-
TransformId *string
2453
}
2554

2655
type JsonTest struct {
@@ -39,7 +68,8 @@ type JsonTransformer struct {
3968
type JsonZogSchema struct {
4069
Type string // "string"
4170
Processors []any // JsonTest or JsonTransformer
42-
Child any // *JsonZogSchema | map[string]JsonZogSchema
71+
Format *string
72+
Child any // *JsonZogSchema | map[string]JsonZogSchema
4373
Required *JsonTest
4474
DefaultValue any
4575
CatchValue any
@@ -93,13 +123,18 @@ func (s *BoolSchema[T]) toJson() *JsonZogSchema {
93123
}
94124

95125
func (s *TimeSchema) toJson() *JsonZogSchema {
96-
rvP := reflect.ValueOf(s.processors)
126+
// rvP := reflect.ValueOf(s.processors)
97127
j := JsonZogSchema{
98-
Type: zconst.TypeTime,
99-
Required: toJsonTest(s.required),
100-
DefaultValue: deepCopyPrimitivePtr(s.defaultVal),
101-
CatchValue: deepCopyPrimitivePtr(s.catch),
102-
Processors: processorsToJson(rvP),
128+
Type: zconst.TypeTime,
129+
// Required: toJsonTest(s.required),
130+
// DefaultValue: deepCopyPrimitivePtr(s.defaultVal),
131+
// CatchValue: deepCopyPrimitivePtr(s.catch),
132+
// Processors: processorsToJson(rvP),
133+
}
134+
exmeta, ok := EX_META_REGISTRY[s]
135+
if ok {
136+
x := exmeta["format"].(string)
137+
j.Format = &x
103138
}
104139
return &j
105140
}
@@ -159,6 +194,14 @@ func (s *PreprocessSchema[F, T]) toJson() *JsonZogSchema {
159194
return &j
160195
}
161196

197+
func (s *BoxedSchema[B, T]) toJson() *JsonZogSchema {
198+
j := JsonZogSchema{
199+
Type: "boxed",
200+
Child: s.schema.toJson(),
201+
}
202+
return &j
203+
}
204+
162205
func processRVtoJson(rv reflect.Value) any {
163206

164207
if !rv.CanInterface() {

toJsonExhaustive.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build zogmeta
2+
// +build zogmeta
3+
4+
package zog
5+
6+
const (
7+
EXHAUSTIVE_METADATA = true
8+
)

toJsonNonExhaustive.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !zogmeta
2+
// +build !zogmeta
3+
4+
package zog
5+
6+
const (
7+
EXHAUSTIVE_METADATA = false
8+
)

utilsOptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type TestOption = p.TestOption
1212
// Message is a function that allows you to set a custom message for the test.
1313
func Message(msg string) TestOption {
1414
return func(test p.TestInterface) {
15+
registryAdd(EX_META_REGISTRY, test, "message", msg)
1516
test.SetIssueFmtFunc(func(e *ZogIssue, p Ctx) {
1617
e.SetMessage(msg)
1718
})

0 commit comments

Comments
 (0)