Skip to content

Commit 6282929

Browse files
committed
refactor: added unique type reference comparation between root validations and its mapper
1 parent 3b1aa47 commit 6282929

4 files changed

Lines changed: 76 additions & 16 deletions

File tree

mapper/mapper.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package mapper
22

33
import (
44
"reflect"
5+
6+
goreflect "github.com/ralvarezdev/go-reflect"
57
)
68

79
type (
810
// Mapper is a map of fields to validate from a struct
911
Mapper struct {
12+
// uniqueTypeReference is the unique type reference of the struct
13+
uniqueTypeReference string
14+
1015
// structInstance is the instance of the struct
1116
structInstance any
1217

@@ -38,24 +43,36 @@ func NewMapper(structInstance any) (*Mapper, error) {
3843
}
3944

4045
// Get the type of the struct instance
41-
structInstanceType := reflect.TypeOf(structInstance)
42-
structInstanceValue := reflect.ValueOf(structInstance)
43-
44-
// Dereference pointer if necessary
45-
if structInstanceType.Kind() == reflect.Ptr {
46-
structInstanceType = structInstanceType.Elem()
47-
structInstanceValue = structInstanceValue.Elem()
48-
}
46+
structInstanceType := goreflect.GetDereferencedType(structInstance)
47+
structInstanceValue := goreflect.GetDereferencedValue(structInstance)
4948

5049
// Ensure it's a struct
5150
if structInstanceType.Kind() != reflect.Struct {
5251
return nil, ErrInvalidStructInstance
5352
}
53+
54+
// Get the value of the struct instance
55+
structInstanceValueInterface := structInstanceValue.Interface()
56+
57+
// Get the unique type identifier for the struct
58+
uniqueTypeReference := goreflect.UniqueTypeReference(structInstanceValueInterface)
59+
60+
return &Mapper{
61+
structInstance: structInstanceValueInterface,
62+
uniqueTypeReference: uniqueTypeReference,
63+
}, nil
64+
}
5465

55-
// Use the dereferenced value (as interface{})
56-
dereferencedStructInstance := structInstanceValue.Interface()
57-
58-
return &Mapper{structInstance: dereferencedStructInstance}, nil
66+
// GetUniqueTypeReference returns the unique type reference of the struct
67+
//
68+
// Returns:
69+
//
70+
// - string: unique type reference of the struct
71+
func (m *Mapper) GetUniqueTypeReference() string {
72+
if m == nil {
73+
return ""
74+
}
75+
return m.uniqueTypeReference
5976
}
6077

6178
// GetStructInstance returns the instance of the struct

mapper/validation/types.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type (
88
// StructValidations is a struct that holds the struct validations for the generated validations of a struct
99
StructValidations struct {
10+
uniqueTypeReference *string
1011
fieldName *string
1112
reflection *goreflect.Reflection
1213
fieldsValidations map[string]*FieldValidations
@@ -34,9 +35,20 @@ func NewStructValidations(instance any) (*StructValidations, error) {
3435
if instance == nil {
3536
return nil, ErrNilInstance
3637
}
38+
39+
// Get the reflection of the instance
40+
instanceReflection := goreflect.NewDereferencedReflection(instance)
41+
instanceValue := instanceReflection.GetReflectedValue()
42+
43+
// Get the interface of the instance
44+
instanceValueInterface := instanceValue.Interface()
45+
46+
// Get the unique type identifier for the instance
47+
uniqueTypeReference := goreflect.UniqueTypeReference(instanceValueInterface)
3748

3849
return &StructValidations{
39-
reflection: goreflect.NewDereferencedReflection(instance),
50+
uniqueTypeReference: &uniqueTypeReference,
51+
reflection: instanceReflection,
4052
}, nil
4153
}
4254

@@ -62,7 +74,7 @@ func NewNestedStructValidations(
6274
if fieldName == "" {
6375
return NewStructValidations(instance)
6476
}
65-
77+
6678
return &StructValidations{
6779
fieldName: &fieldName,
6880
reflection: goreflect.NewDereferencedReflection(instance),
@@ -93,6 +105,18 @@ func (s *StructValidations) GetStructTypeName() string {
93105
return s.reflection.GetReflectedTypeName()
94106
}
95107

108+
// GetUniqueTypeReference returns the unique type reference of the struct
109+
//
110+
// Returns:
111+
//
112+
// - *string: The unique type reference of the struct
113+
func (s *StructValidations) GetUniqueTypeReference() *string {
114+
if s == nil {
115+
return nil
116+
}
117+
return s.uniqueTypeReference
118+
}
119+
96120
// HasFailed returns true if there are failed validations
97121
//
98122
// Returns:

mapper/validator/errors.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import (
44
"errors"
55
)
66

7+
const (
8+
ErrFieldTagNameNotFound = "field tag name not found: %s"
9+
ErrFieldIsRequiredNotFound = "field is required not found: %s"
10+
ErrStructValidationsAndMapperTypeMismatch = "struct validations and mapper type mismatch, both must be of the same type, mapper type: %s, struct validations type: %s"
11+
)
12+
713
var (
814
ErrNilService = errors.New("mapper validator service cannot be nil")
915
ErrNilDestination = errors.New("destination cannot be nil")
1016
ErrDestinationNotPointer = errors.New("destination must be a pointer")
1117
ErrNilMapper = errors.New("mapper cannot be nil")
1218
ErrNilValidator = errors.New("mapper validator cannot be nil")
13-
ErrFieldTagNameNotFound = "field tag name not found: %s"
14-
ErrFieldIsRequiredNotFound = "field is required not found: %s"
19+
ErrStructValidationsIsNotRootLevel = errors.New("struct validations is not root level")
1520
ErrRequiredField = "%s is required"
1621
)

mapper/validator/validator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ func (d DefaultValidator) ValidateRequiredFields(
8686
if mapper == nil {
8787
return ErrNilMapper
8888
}
89+
90+
// Compare the unique type references
91+
rootStructValidationsTypeReference := rootStructValidations.GetUniqueTypeReference()
92+
mapperTypeReference := mapper.GetUniqueTypeReference()
93+
if rootStructValidationsTypeReference == nil {
94+
return ErrStructValidationsIsNotRootLevel
95+
}
96+
if *rootStructValidationsTypeReference != mapperTypeReference {
97+
return fmt.Errorf(
98+
ErrStructValidationsAndMapperTypeMismatch,
99+
mapperTypeReference,
100+
*rootStructValidationsTypeReference,
101+
)
102+
}
89103

90104
// Check if the struct has fields validations
91105
if !mapper.HasFieldsValidations() {

0 commit comments

Comments
 (0)