Skip to content

Commit 756bb00

Browse files
committed
cache wip
1 parent 0df3933 commit 756bb00

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ go test -run=Bench -bench=. -benchtime 5000000x
3535
2. Enums Validation
3636
3. Nested Struct Validation
3737
4. Benchmarking
38+
5. Cache Implementation

validate.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"go.nandlabs.io/l3"
55
"reflect"
66
"strings"
7+
"sync"
78
)
89

910
var logger = l3.Get()
@@ -12,14 +13,26 @@ var mandatory = [...]string{"required", "nillable"}
1213

1314
type StructValidatorFunc func(v reflect.Value, typ reflect.Type, param string) error
1415

16+
type field struct {
17+
name string
18+
constraints sync.Map //map[string]string
19+
}
20+
21+
type structFields struct {
22+
list []field
23+
}
24+
1525
type StructValidator struct {
16-
validationFuncs map[string]StructValidatorFunc
17-
tagName string
26+
fields structFields
27+
validationFunc map[string]StructValidatorFunc
28+
tagName string
1829
}
1930

31+
var validatorCache sync.Map
32+
2033
func NewStructValidator() *StructValidator {
2134
return &StructValidator{
22-
validationFuncs: map[string]StructValidatorFunc{
35+
validationFunc: map[string]StructValidatorFunc{
2336
// Base Constraints
2437
// boolean value
2538
// mandatory field
@@ -49,12 +62,25 @@ func NewStructValidator() *StructValidator {
4962
func (sv *StructValidator) Validate(v interface{}) error {
5063
//logger.Info("starting struct validation")
5164
// add a logic to check for the empty struct input in order to skip the validation of the struct
65+
66+
//check for cache
67+
val := StructValidator{fields: cachedTypeFields(reflect.ValueOf(v).Type())}
68+
if err := val.validateFields(); err != nil {
69+
return err
70+
}
71+
5272
if err := sv.deepFields(v); err != nil {
5373
return err
5474
}
5575
return nil
5676
}
5777

78+
func (sv *StructValidator) validateFields() error {
79+
for i, v = range sv.fields.list {
80+
81+
}
82+
}
83+
5884
func (sv *StructValidator) deepFields(itr interface{}) error {
5985
ifv := reflect.ValueOf(itr)
6086
ift := ifv.Type()
@@ -90,8 +116,7 @@ func (sv *StructValidator) parseTag(fieldValue reflect.Value, tag string, typ re
90116
for _, str := range split {
91117
constraintName := strings.Split(str, "=")[0]
92118
constraintValue := strings.Split(str, "=")[1]
93-
//m[constraintName] = constraintValue
94-
if err := sv.validationFuncs[constraintName](fieldValue, typ, constraintValue); err != nil {
119+
if err := sv.validationFunc[constraintName](fieldValue, typ, constraintValue); err != nil {
95120
logger.ErrorF("constraint validation failed")
96121
return err
97122
} else {
@@ -115,3 +140,17 @@ func check(list []string) bool {
115140
}
116141
return false
117142
}
143+
144+
func parseFields(t reflect.Type) structFields {
145+
146+
}
147+
148+
var fieldCache sync.Map //map[reflect.Type]structFields
149+
150+
func cachedTypeFields(t reflect.Type) structFields {
151+
if f, ok := fieldCache.Load(t); ok {
152+
return f.(structFields)
153+
}
154+
f, _ := fieldCache.LoadOrStore(t, parseFields(t))
155+
return f.(structFields)
156+
}

0 commit comments

Comments
 (0)