Skip to content

Commit

Permalink
Replace empty interface with any (#315)
Browse files Browse the repository at this point in the history
* replace empty interface with any

* rename file

* only test in ubuntu to speed up test

* fix test was not run on different go version

* run go mod tidy

* fix build flag

* refactor fmtiface to fmtAny

* revert

* Update FUNDING.yml
  • Loading branch information
Fs02 committed Oct 10, 2022
1 parent 024d66f commit 726f0b3
Show file tree
Hide file tree
Showing 43 changed files with 347 additions and 335 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml
@@ -1,2 +1 @@
github: Fs02
open_collective: rel
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Run GoReleaser
Expand Down
6 changes: 3 additions & 3 deletions adapter.go
Expand Up @@ -12,11 +12,11 @@ type Adapter interface {
Ping(ctx context.Context) error
Aggregate(ctx context.Context, query Query, mode string, field string) (int, error)
Query(ctx context.Context, query Query) (Cursor, error)
Insert(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate, onConflict OnConflict) (interface{}, error)
InsertAll(ctx context.Context, query Query, primaryField string, fields []string, bulkMutates []map[string]Mutate, onConflict OnConflict) ([]interface{}, error)
Insert(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate, onConflict OnConflict) (any, error)
InsertAll(ctx context.Context, query Query, primaryField string, fields []string, bulkMutates []map[string]Mutate, onConflict OnConflict) ([]any, error)
Update(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate) (int, error)
Delete(ctx context.Context, query Query) (int, error)
Exec(ctx context.Context, stmt string, args []interface{}) (int64, int64, error)
Exec(ctx context.Context, stmt string, args []any) (int64, int64, error)

Begin(ctx context.Context) (Adapter, error)
Commit(ctx context.Context) error
Expand Down
12 changes: 6 additions & 6 deletions adapter_test.go
Expand Up @@ -8,7 +8,7 @@ import (

type testAdapter struct {
mock.Mock
result interface{}
result any
}

var _ Adapter = (*testAdapter)(nil)
Expand Down Expand Up @@ -41,14 +41,14 @@ func (ta *testAdapter) Query(ctx context.Context, query Query) (Cursor, error) {
return args.Get(0).(Cursor), args.Error(1)
}

func (ta *testAdapter) Insert(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate, onConflict OnConflict) (interface{}, error) {
func (ta *testAdapter) Insert(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate, onConflict OnConflict) (any, error) {
args := ta.Called(query, mutates, onConflict)
return args.Get(0), args.Error(1)
}

func (ta *testAdapter) InsertAll(ctx context.Context, query Query, primaryField string, fields []string, mutates []map[string]Mutate, onConflict OnConflict) ([]interface{}, error) {
func (ta *testAdapter) InsertAll(ctx context.Context, query Query, primaryField string, fields []string, mutates []map[string]Mutate, onConflict OnConflict) ([]any, error) {
args := ta.Called(query, fields, mutates, onConflict)
return args.Get(0).([]interface{}), args.Error(1)
return args.Get(0).([]any), args.Error(1)
}

func (ta *testAdapter) Update(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate) (int, error) {
Expand Down Expand Up @@ -81,12 +81,12 @@ func (ta *testAdapter) Apply(ctx context.Context, migration Migration) error {
return args.Error(0)
}

func (ta *testAdapter) Result(result interface{}) *testAdapter {
func (ta *testAdapter) Result(result any) *testAdapter {
ta.result = result
return ta
}

func (ta *testAdapter) Exec(ctx context.Context, stmt string, args []interface{}) (int64, int64, error) {
func (ta *testAdapter) Exec(ctx context.Context, stmt string, args []any) (int64, int64, error) {
mockArgs := ta.Called(ctx, stmt, args)
return int64(mockArgs.Int(0)), int64(mockArgs.Int(1)), mockArgs.Error(2)
}
6 changes: 6 additions & 0 deletions any_notgo1.18.go
@@ -0,0 +1,6 @@
//go:build !go118
// +build !go118

package rel

type any = interface{}
6 changes: 6 additions & 0 deletions any_notgo1.18_test.go
@@ -0,0 +1,6 @@
//go:build !go118
// +build !go118

package rel_test

type any = interface{}
4 changes: 2 additions & 2 deletions association.go
Expand Up @@ -96,7 +96,7 @@ func (a Association) ReferenceField() string {
}

// ReferenceValue of the association.
func (a Association) ReferenceValue() interface{} {
func (a Association) ReferenceValue() any {
return indirectInterface(reflectValueFieldByIndex(a.rv, a.meta.referenceIndex, false))
}

Expand All @@ -107,7 +107,7 @@ func (a Association) ForeignField() string {

// ForeignValue of the association.
// It'll panic if association type is has many.
func (a Association) ForeignValue() interface{} {
func (a Association) ForeignValue() any {
if a.Type() == HasMany {
panic("rel: cannot infer foreign value for has many or many to many association")
}
Expand Down
12 changes: 6 additions & 6 deletions association_test.go
Expand Up @@ -20,15 +20,15 @@ func TestAssociation_Document(t *testing.T) {
tests := []struct {
record string
field string
data interface{}
data any
typ AssociationType
doc *Document
loaded bool
isZero bool
referenceField string
referenceValue interface{}
referenceValue any
foreignField string
foreignValue interface{}
foreignValue any
autosave bool
autoload bool
}{
Expand Down Expand Up @@ -199,16 +199,16 @@ func TestAssociation_Collection(t *testing.T) {
tests := []struct {
record string
field string
data interface{}
data any
typ AssociationType
col *Collection
loaded bool
isZero bool
referenceField string
referenceValue interface{}
referenceValue any
referenceThrough string
foreignField string
foreignValue interface{}
foreignValue any
foreignThrough string
through string
autoload bool
Expand Down
38 changes: 19 additions & 19 deletions changeset.go
Expand Up @@ -6,20 +6,20 @@ import (
"time"
)

type pair = [2]interface{}
type pair = [2]any

// Changeset mutator for structs.
// This allows REL to efficiently to perform update operation only on updated fields and association.
// The catch is, enabling changeset will duplicates the original struct values which consumes more memory.
type Changeset struct {
doc *Document
snapshot []interface{}
snapshot []any
assoc map[string]Changeset
assocMany map[string]map[interface{}]Changeset
assocMany map[string]map[any]Changeset
}

func (c Changeset) valueChanged(typ reflect.Type, old interface{}, new interface{}) bool {
if oeq, ok := old.(interface{ Equal(interface{}) bool }); ok {
func (c Changeset) valueChanged(typ reflect.Type, old any, new any) bool {
if oeq, ok := old.(interface{ Equal(any) bool }); ok {
return !oeq.Equal(new)
}

Expand Down Expand Up @@ -53,7 +53,7 @@ func (c Changeset) FieldChanged(field string) bool {
}

// Changes returns map of changes.
func (c Changeset) Changes() map[string]interface{} {
func (c Changeset) Changes() map[string]any {
return buildChanges(c.doc, c)
}

Expand Down Expand Up @@ -118,8 +118,8 @@ func (c Changeset) applyAssocMany(field string, mut *Mutation) {
assoc = c.doc.Association(field)
col, _ = assoc.Collection()
muts = make([]Mutation, 0, col.Len())
updatedIDs = make(map[interface{}]struct{})
deletedIDs []interface{}
updatedIDs = make(map[any]struct{})
deletedIDs []any
)

for i := 0; i < col.Len(); i++ {
Expand Down Expand Up @@ -158,16 +158,16 @@ func (c Changeset) applyAssocMany(field string, mut *Mutation) {
}

// NewChangeset returns new changeset mutator for given record.
func NewChangeset(record interface{}) Changeset {
func NewChangeset(record any) Changeset {
return newChangeset(NewDocument(record))
}

func newChangeset(doc *Document) Changeset {
c := Changeset{
doc: doc,
snapshot: make([]interface{}, len(doc.Fields())),
snapshot: make([]any, len(doc.Fields())),
assoc: make(map[string]Changeset),
assocMany: make(map[string]map[interface{}]Changeset),
assocMany: make(map[string]map[any]Changeset),
}

for i, field := range doc.Fields() {
Expand Down Expand Up @@ -198,13 +198,13 @@ func initChangesetAssoc(doc *Document, assoc map[string]Changeset, field string)
assoc[field] = newChangeset(doc)
}

func initChangesetAssocMany(doc *Document, assoc map[string]map[interface{}]Changeset, field string) {
func initChangesetAssocMany(doc *Document, assoc map[string]map[any]Changeset, field string) {
col, loaded := doc.Association(field).Collection()
if !loaded {
return
}

assoc[field] = make(map[interface{}]Changeset)
assoc[field] = make(map[any]Changeset)

for i := 0; i < col.Len(); i++ {
var (
Expand All @@ -218,9 +218,9 @@ func initChangesetAssocMany(doc *Document, assoc map[string]map[interface{}]Chan
}
}

func buildChanges(doc *Document, c Changeset) map[string]interface{} {
func buildChanges(doc *Document, c Changeset) map[string]any {
var (
changes = make(map[string]interface{})
changes = make(map[string]any)
fields []string
)

Expand Down Expand Up @@ -268,7 +268,7 @@ func buildChanges(doc *Document, c Changeset) map[string]interface{} {
return changes
}

func buildChangesAssoc(out map[string]interface{}, c Changeset, field string) {
func buildChangesAssoc(out map[string]any, c Changeset, field string) {
assoc := c.doc.Association(field)
if assoc.IsZero() {
return
Expand All @@ -280,13 +280,13 @@ func buildChangesAssoc(out map[string]interface{}, c Changeset, field string) {
}
}

func buildChangesAssocMany(out map[string]interface{}, c Changeset, field string) {
func buildChangesAssocMany(out map[string]any, c Changeset, field string) {
var (
changes []map[string]interface{}
changes []map[string]any
chs = c.assocMany[field]
assoc = c.doc.Association(field)
col, _ = assoc.Collection()
updatedIDs = make(map[interface{}]struct{})
updatedIDs = make(map[any]struct{})
)

for i := 0; i < col.Len(); i++ {
Expand Down

0 comments on commit 726f0b3

Please sign in to comment.