Skip to content

Commit 957e7a8

Browse files
committed
fix: added nil and struct type check on NewMapper function, and dereference it if neccessary
1 parent d43ec3e commit 957e7a8

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

mapper/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ var (
1010
ErrProtobufTagNotFound = "missing protobuf tag: %s"
1111
ErrProtobufTagNameNotFound = "missing protobuf tag name: %s"
1212
ErrEmptyJSONTag = "empty json tag: %s"
13+
ErrNilStructInstance = errors.New("struct instance cannot be nil")
14+
ErrStructInstanceNotStruct = errors.New("struct instance must be a struct")
15+
ErrInvalidStructInstance = errors.New("invalid struct instance")
1316
)

mapper/json.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ func (j JSONGenerator) NewMapper(structInstance any) (
5959
structTypeName := reflectedType.Name()
6060

6161
// Initialize the root map of fields and the map of nested mappers
62-
rootMapper := NewMapper(structInstance)
62+
rootMapper, err := NewMapper(structInstance)
63+
if err != nil {
64+
return nil, err
65+
}
6366

6467
// Reflection of the type of data
6568
var jsonTag string

mapper/mapper.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,32 @@ type (
3030
// Returns:
3131
//
3232
// - *Mapper: instance of the mapper
33-
func NewMapper(structInstance any) *Mapper {
34-
return &Mapper{structInstance: structInstance}
33+
// - error: error if the struct instance is nil
34+
func NewMapper(structInstance any) (*Mapper, error) {
35+
// Check if the struct instance is nil
36+
if structInstance == nil {
37+
return nil, ErrNilStructInstance
38+
}
39+
40+
// 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+
}
49+
50+
// Ensure it's a struct
51+
if structInstanceType.Kind() != reflect.Struct {
52+
return nil, ErrInvalidStructInstance
53+
}
54+
55+
// Use the dereferenced value (as interface{})
56+
dereferencedStructInstance := structInstanceValue.Interface()
57+
58+
return &Mapper{structInstance: dereferencedStructInstance}, nil
3559
}
3660

3761
// GetStructInstance returns the instance of the struct

mapper/protobuf.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ func NewProtobufGenerator(logger *slog.Logger) *ProtobufGenerator {
5151
func (p ProtobufGenerator) NewMapper(structInstance any) (
5252
*Mapper,
5353
error,
54-
) {
54+
) {
5555
// Reflection of data
5656
reflectedType := goreflect.GetDereferencedType(structInstance)
5757

5858
// Get the struct type name
5959
structTypeName := goreflect.GetTypeName(reflectedType)
6060

6161
// Initialize the root map of fields and the map of nested mappers
62-
rootMapper := NewMapper(structInstance)
62+
rootMapper, err := NewMapper(structInstance)
63+
if err != nil {
64+
return nil, err
65+
}
6366

6467
// Reflection of the type of data
6568
for i := 0; i < reflectedType.NumField(); i++ {

0 commit comments

Comments
 (0)