pull out callID method call

This commit is contained in:
c9s 2022-05-05 13:05:01 +08:00
parent 21f81dec29
commit 30c85d2969
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 10 additions and 13 deletions

View File

@ -129,11 +129,7 @@ func newTypeValueInterface(typ reflect.Type) interface{} {
}
}
func loadPersistenceFields(obj interface{}, persistence service.PersistenceService) error {
id := callID(obj)
if len(id) == 0 {
return fmt.Errorf("object does not provide ID() method for persistency")
}
func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return iterateFieldsByTag(obj, "persistence", func(tag string, field reflect.StructField, value reflect.Value) error {
newValueInf := newTypeValueInterface(value.Type())
@ -155,12 +151,7 @@ func loadPersistenceFields(obj interface{}, persistence service.PersistenceServi
})
}
func storePersistenceFields(obj interface{}, persistence service.PersistenceService) error {
id := callID(obj)
if len(id) == 0 {
return fmt.Errorf("object does not provide ID() method for persistency")
}
func storePersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return iterateFieldsByTag(obj, "persistence", func(tag string, ft reflect.StructField, fv reflect.Value) error {
inf := fv.Interface()

View File

@ -42,6 +42,11 @@ func preparePersistentServices() []service.PersistenceService {
return pss
}
func Test_callID(t *testing.T) {
id := callID(&TestStruct{})
assert.NotEmpty(t, id)
}
func Test_storePersistenceFields(t *testing.T) {
var pss = preparePersistentServices()
@ -56,7 +61,8 @@ func Test_storePersistenceFields(t *testing.T) {
for _, ps := range pss {
t.Run(reflect.TypeOf(ps).Elem().String(), func(t *testing.T) {
err := storePersistenceFields(a, ps)
id := callID(a)
err := storePersistenceFields(a, id, ps)
assert.NoError(t, err)
var i int64
@ -73,7 +79,7 @@ func Test_storePersistenceFields(t *testing.T) {
assert.Equal(t, fixedpoint.NewFromFloat(3343.0), p.AverageCost)
var b = &TestStruct{}
err = loadPersistenceFields(b, ps)
err = loadPersistenceFields(b, id, ps)
assert.NoError(t, err)
assert.Equal(t, a.Integer, b.Integer)
assert.Equal(t, a.Integer2, b.Integer2)