Skip to content

Commit 0859b9e

Browse files
authored
Merge branch 'development' into development
2 parents e4b2210 + 31b001b commit 0859b9e

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

docs/quick-start/add-rest-handlers/page.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ This method can be used to implement custom logic for filtering, sorting, or ret
100100

101101

102102
> Few Points to consider:
103-
> 1. Field Naming Convention: GoFr assumes the struct fields in snake-case match the database column names. For example, `IsEmployed` field in the struct matches `is_employed` column in the database, `Age` field matches `age` column, etc.
104-
> 2. Primary Key: The first field of the struct is typically used as the primary key for data operations. However, user can customize this behavior using GoFr's features.
103+
> 1. The struct should always be passed by reference in the method `AddRESTHandlers`.
104+
> 2. Field Naming Convention: GoFr assumes the struct fields in snake-case match the database column names. For example, `IsEmployed` field in the struct matches `is_employed` column in the database, `Age` field matches `age` column, etc.
105+
> 3. Primary Key: The first field of the struct is typically used as the primary key for data operations. However, user can customize this behavior using GoFr's features.

pkg/gofr/crud_handlers.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
)
1111

1212
var (
13-
errInvalidObject = errors.New("unexpected object given for AddRESTHandlers")
14-
errEntityNotFound = errors.New("entity not found")
13+
errInvalidObject = errors.New("unexpected object given for AddRESTHandlers")
14+
errEntityNotFound = errors.New("entity not found")
15+
errObjectIsNil = errors.New("object given for AddRESTHandlers is nil")
16+
errNonPointerObject = errors.New("passed object is not pointer")
1517
)
1618

1719
type Create interface {
@@ -61,7 +63,16 @@ type entity struct {
6163

6264
// scanEntity extracts entity information for CRUD operations.
6365
func scanEntity(object interface{}) (*entity, error) {
64-
entityType := reflect.TypeOf(object).Elem()
66+
if object == nil {
67+
return nil, errObjectIsNil
68+
}
69+
70+
objType := reflect.TypeOf(object)
71+
if objType.Kind() != reflect.Ptr {
72+
return nil, fmt.Errorf("failed to register routes for '%s' struct, %w", objType.Name(), errNonPointerObject)
73+
}
74+
75+
entityType := objType.Elem()
6576
if entityType.Kind() != reflect.Struct {
6677
return nil, errInvalidObject
6778
}

pkg/gofr/crud_handlers_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"database/sql/driver"
77
"encoding/json"
88
"errors"
9+
"fmt"
910
"net/http"
1011
"net/http/httptest"
1112
"reflect"
@@ -96,6 +97,12 @@ func Test_scanEntity(t *testing.T) {
9697
resp: nil,
9798
err: errInvalidObject,
9899
},
100+
{
101+
desc: "invalid object type",
102+
input: userEntity{},
103+
resp: nil,
104+
err: fmt.Errorf("failed to register routes for 'userEntity' struct, %w", errNonPointerObject),
105+
},
99106
}
100107

101108
for i, tc := range tests {

pkg/gofr/gofr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,11 @@ func (a *App) Subscribe(topic string, handler SubscribeFunc) {
484484
a.subscriptionManager.subscriptions[topic] = handler
485485
}
486486

487+
// AddRESTHandlers creates and registers CRUD routes for the given struct, the struct should always be passed by reference.
487488
func (a *App) AddRESTHandlers(object interface{}) error {
488489
cfg, err := scanEntity(object)
489490
if err != nil {
490-
a.container.Logger.Errorf("invalid object for AddRESTHandlers")
491-
491+
a.container.Logger.Errorf(err.Error())
492492
return err
493493
}
494494

pkg/gofr/gofr_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ func Test_AddRESTHandlers(t *testing.T) {
311311
}{
312312
{"success case", &user{}, nil},
313313
{"invalid object", &invalidObject, errInvalidObject},
314+
{"invalid object", user{}, fmt.Errorf("failed to register routes for 'user' struct, %w", errNonPointerObject)},
315+
{"invalid object", nil, errObjectIsNil},
314316
}
315317

316318
for i, tc := range tests {

0 commit comments

Comments
 (0)