diff --git a/examples/crud/crud.go b/examples/crud/crud.go index 0d90e2a..6b08ddd 100644 --- a/examples/crud/crud.go +++ b/examples/crud/crud.go @@ -9,12 +9,14 @@ import ( "github.com/vingarcia/kissorm/nullable" ) +// User ... type User struct { ID int `kissorm:"id"` Name string `kissorm:"name"` Age int `kissorm:"age"` } +// PartialUpdateUser ... type PartialUpdateUser struct { ID int `kissorm:"id"` Name *string `kissorm:"name"` diff --git a/mocks.go b/mocks.go index 664c56b..98431ad 100644 --- a/mocks.go +++ b/mocks.go @@ -4,6 +4,7 @@ import "context" var _ ORMProvider = MockORMProvider{} +// MockORMProvider ... type MockORMProvider struct { InsertFn func(ctx context.Context, records ...interface{}) error DeleteFn func(ctx context.Context, ids ...interface{}) error @@ -17,34 +18,42 @@ type MockORMProvider struct { TransactionFn func(ctx context.Context, fn func(db ORMProvider) error) error } +// Insert ... func (m MockORMProvider) Insert(ctx context.Context, records ...interface{}) error { return m.InsertFn(ctx, records...) } +// Delete ... func (m MockORMProvider) Delete(ctx context.Context, ids ...interface{}) error { return m.DeleteFn(ctx, ids...) } +// Update ... func (m MockORMProvider) Update(ctx context.Context, records ...interface{}) error { return m.UpdateFn(ctx, records...) } +// Query ... func (m MockORMProvider) Query(ctx context.Context, records interface{}, query string, params ...interface{}) error { return m.QueryFn(ctx, records, query, params...) } +// QueryOne ... func (m MockORMProvider) QueryOne(ctx context.Context, record interface{}, query string, params ...interface{}) error { return m.QueryOneFn(ctx, record, query, params...) } +// QueryChunks ... func (m MockORMProvider) QueryChunks(ctx context.Context, parser ChunkParser) error { return m.QueryChunksFn(ctx, parser) } +// Exec ... func (m MockORMProvider) Exec(ctx context.Context, query string, params ...interface{}) error { return m.ExecFn(ctx, query, params...) } +// Transaction ... func (m MockORMProvider) Transaction(ctx context.Context, fn func(db ORMProvider) error) error { return m.TransactionFn(ctx, fn) } diff --git a/structs.go b/structs.go index efadb15..50f0134 100644 --- a/structs.go +++ b/structs.go @@ -103,7 +103,7 @@ func FillStructWith(record interface{}, dbRow map[string]interface{}) error { return nil } -// This type was created to make it easier +// PtrConverter was created to make it easier // to handle conversion between ptr and non ptr types, e.g.: // // - *type to *type @@ -117,6 +117,13 @@ type PtrConverter struct { ElemValue reflect.Value } +// NewPtrConverter instantiates a PtrConverter from +// an empty interface. +// +// The input argument can be of any type, but +// if it is a pointer then its Elem() will be +// used as source value for the PtrConverter.Convert() +// method. func NewPtrConverter(v interface{}) PtrConverter { if v == nil { // This is necessary so that reflect.ValueOf @@ -141,6 +148,8 @@ func NewPtrConverter(v interface{}) PtrConverter { } } +// Convert attempts to convert the ElemValue to the destType received +// as argument and then returns the converted reflect.Value or an error func (p PtrConverter) Convert(destType reflect.Type) (reflect.Value, error) { destElemType := destType if destType.Kind() == reflect.Ptr {