Skip to content

Commit 8068289

Browse files
author
phuonghn
committed
add updateby, createby custom field
1 parent 332f6a0 commit 8068289

File tree

4 files changed

+129
-45
lines changed

4 files changed

+129
-45
lines changed

field/custom_field.go

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package field
1515

1616
import (
17+
"context"
1718
"fmt"
1819
"reflect"
1920
"time"
@@ -24,9 +25,13 @@ import (
2425

2526
// CustomFields defines struct of supported custom fields
2627
type CustomFields struct {
27-
createAt string
28-
updateAt string
29-
id string
28+
createAt string
29+
createBy string
30+
createByKey string
31+
updateAt string
32+
updateBy string
33+
updateByKey string
34+
id string
3035
}
3136

3237
// CustomFieldsHook defines the interface, CustomFields return custom field user want to change
@@ -37,7 +42,9 @@ type CustomFieldsHook interface {
3742
// CustomFieldsBuilder defines the interface which user use to set custom fields
3843
type CustomFieldsBuilder interface {
3944
SetUpdateAt(fieldName string) CustomFieldsBuilder
45+
SetUpdateBy(fieldName string, key string) CustomFieldsBuilder
4046
SetCreateAt(fieldName string) CustomFieldsBuilder
47+
SetCreateBy(fieldName string, key string) CustomFieldsBuilder
4148
SetId(fieldName string) CustomFieldsBuilder
4249
}
4350

@@ -52,51 +59,87 @@ func (c *CustomFields) SetUpdateAt(fieldName string) CustomFieldsBuilder {
5259
return c
5360
}
5461

62+
// SetUpdateBy set the custom UpdateAt field
63+
func (c *CustomFields) SetUpdateBy(fieldName string, key string) CustomFieldsBuilder {
64+
c.updateBy = fieldName
65+
c.updateByKey = key
66+
return c
67+
}
68+
5569
// SetCreateAt set the custom CreateAt field
5670
func (c *CustomFields) SetCreateAt(fieldName string) CustomFieldsBuilder {
5771
c.createAt = fieldName
5872
return c
5973
}
6074

75+
// SetCreateBy set the custom CreateAt field
76+
func (c *CustomFields) SetCreateBy(fieldName string, key string) CustomFieldsBuilder {
77+
c.createBy = fieldName
78+
c.createByKey = key
79+
return c
80+
}
81+
6182
// SetId set the custom Id field
6283
func (c *CustomFields) SetId(fieldName string) CustomFieldsBuilder {
6384
c.id = fieldName
6485
return c
6586
}
6687

6788
// CustomCreateTime changes the custom create time
68-
func (c CustomFields) CustomCreateTime(doc interface{}) {
89+
func (c CustomFields) CustomCreateTime(ctx context.Context, doc interface{}) {
6990
if c.createAt == "" {
7091
return
7192
}
7293
fieldName := c.createAt
73-
setTime(doc, fieldName, false)
94+
setTime(ctx, doc, fieldName, false)
95+
return
96+
}
97+
98+
// CustomCreateBy changes the custom create by
99+
func (c CustomFields) CustomCreateBy(ctx context.Context, doc interface{}) {
100+
if c.createBy == "" {
101+
return
102+
}
103+
fieldName := c.createBy
104+
ctxkey := c.createByKey
105+
setBy(ctx, doc, fieldName, ctxkey)
74106
return
75107
}
76108

77109
// CustomUpdateTime changes the custom update time
78-
func (c CustomFields) CustomUpdateTime(doc interface{}) {
110+
func (c CustomFields) CustomUpdateTime(ctx context.Context, doc interface{}) {
79111
if c.updateAt == "" {
80112
return
81113
}
82114
fieldName := c.updateAt
83-
setUpdatedTime(doc, fieldName)
115+
setUpdatedTime(ctx, doc, fieldName)
116+
return
117+
}
118+
119+
// CustomUpdateBy changes the custom update by
120+
func (c CustomFields) CustomUpdateBy(ctx context.Context, doc interface{}) {
121+
if c.createBy == "" {
122+
return
123+
}
124+
fieldName := c.updateBy
125+
ctxkey := c.updateByKey
126+
setBy(ctx, doc, fieldName, ctxkey)
84127
return
85128
}
86129

87130
// CustomUpdateTime changes the custom update time
88-
func (c CustomFields) CustomId(doc interface{}) {
131+
func (c CustomFields) CustomId(ctx context.Context, doc interface{}) {
89132
if c.id == "" {
90133
return
91134
}
92135
fieldName := c.id
93-
setId(doc, fieldName)
136+
setId(ctx, doc, fieldName)
94137
return
95138
}
96139

97140
// setTime changes the custom time fields
98141
// The overWrite defines if change value when the filed has valid value
99-
func setUpdatedTime(doc interface{}, fieldName string) {
142+
func setUpdatedTime(ctx context.Context, doc interface{}, fieldName string) {
100143
if reflect.Ptr != reflect.TypeOf(doc).Kind() {
101144
fmt.Println("not a point type")
102145
return
@@ -123,9 +166,27 @@ func setUpdatedTime(doc interface{}, fieldName string) {
123166
}
124167
}
125168

169+
func setBy(ctx context.Context, doc interface{}, fieldName string, ctxKey string) {
170+
if reflect.Ptr != reflect.TypeOf(doc).Kind() {
171+
fmt.Println("not a point type")
172+
return
173+
}
174+
e := reflect.ValueOf(doc).Elem()
175+
ca := e.FieldByName(fieldName)
176+
if ca.CanSet() {
177+
switch a := ca.Interface().(type) {
178+
case string:
179+
by := ctx.Value(ctxKey)
180+
ca.SetString(by.((string)))
181+
default:
182+
fmt.Println("unsupported type to SetBy", a)
183+
}
184+
}
185+
}
186+
126187
// setTime changes the custom time fields
127188
// The overWrite defines if change value when the filed has valid value
128-
func setTime(doc interface{}, fieldName string, overWrite bool) {
189+
func setTime(ctx context.Context, doc interface{}, fieldName string, overWrite bool) {
129190
if reflect.Ptr != reflect.TypeOf(doc).Kind() {
130191
fmt.Println("not a point type")
131192
return
@@ -166,7 +227,7 @@ func setTime(doc interface{}, fieldName string, overWrite bool) {
166227
}
167228

168229
// setId changes the custom Id fields
169-
func setId(doc interface{}, fieldName string) {
230+
func setId(ctx context.Context, doc interface{}, fieldName string) {
170231
if reflect.Ptr != reflect.TypeOf(doc).Kind() {
171232
fmt.Println("not a point type")
172233
return

field/custom_field_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
package field
1515

1616
import (
17-
"github.com/stretchr/testify/require"
18-
"go.mongodb.org/mongo-driver/bson/primitive"
17+
"context"
1918
"testing"
2019
"time"
20+
21+
"github.com/stretchr/testify/require"
22+
"go.mongodb.org/mongo-driver/bson/primitive"
2123
)
2224

2325
type CustomUser struct {
@@ -42,17 +44,17 @@ func TestCustomFields(t *testing.T) {
4244
ast := require.New(t)
4345
u := &CustomUser{}
4446
c := u.CustomFields()
45-
c.(*CustomFields).CustomCreateTime(u)
46-
c.(*CustomFields).CustomUpdateTime(u)
47-
c.(*CustomFields).CustomId(u)
47+
c.(*CustomFields).CustomCreateTime(context.TODO(), u)
48+
c.(*CustomFields).CustomUpdateTime(context.TODO(), u)
49+
c.(*CustomFields).CustomId(context.TODO(), u)
4850
ast.NotEqual(0, u.Update)
4951
ast.NotEqual(time.Time{}, u.Create)
5052
ast.NotEqual(primitive.NilObjectID, u.MyId)
5153

5254
// id string
5355
u1 := &CustomUser{}
5456
c1 := u.CustomFieldsIdString()
55-
c1.(*CustomFields).CustomId(u1)
57+
c1.(*CustomFields).CustomId(context.TODO(), u1)
5658
ast.NotEqual("", u1.MyIdString)
5759

5860
}
@@ -71,34 +73,34 @@ func (c *CustomUser) CustomFieldsInvalid3() CustomFieldsBuilder {
7173
func TestCustomFieldsInvalid(t *testing.T) {
7274
u := &CustomUser{}
7375
c := u.CustomFieldsInvalid()
74-
c.(*CustomFields).CustomCreateTime(u)
75-
c.(*CustomFields).CustomUpdateTime(u)
76+
c.(*CustomFields).CustomCreateTime(context.TODO(), u)
77+
c.(*CustomFields).CustomUpdateTime(context.TODO(), u)
7678
ast := require.New(t)
7779
ast.Equal(0, u.InvalidCreate)
7880
ast.Equal(float32(0), u.InvalidUpdate)
7981

8082
u1 := &CustomUser{}
8183
c = u1.CustomFieldsInvalid2()
82-
c.(*CustomFields).CustomCreateTime(u1)
83-
c.(*CustomFields).CustomUpdateTime(u1)
84+
c.(*CustomFields).CustomCreateTime(context.TODO(), u1)
85+
c.(*CustomFields).CustomUpdateTime(context.TODO(), u1)
8486
ast.Equal(0, u1.InvalidCreate)
8587
ast.Equal(float32(0), u1.InvalidUpdate)
8688

8789
u2 := CustomUser{}
8890
c = u2.CustomFieldsInvalid()
89-
c.(*CustomFields).CustomCreateTime(u2)
90-
c.(*CustomFields).CustomUpdateTime(u2)
91+
c.(*CustomFields).CustomCreateTime(context.TODO(), u2)
92+
c.(*CustomFields).CustomUpdateTime(context.TODO(), u2)
9193
ast.Equal(0, u2.InvalidCreate)
9294
ast.Equal(float32(0), u2.InvalidUpdate)
9395

9496
u3 := CustomUser{}
9597
c = u3.CustomFieldsInvalid3()
96-
c.(*CustomFields).CustomId(u3)
98+
c.(*CustomFields).CustomId(context.TODO(), u3)
9799
ast.Equal(0, u3.InvalidId)
98100

99101
u4 := &CustomUser{}
100102
c = u4.CustomFieldsInvalid3()
101-
c.(*CustomFields).CustomId(u4)
103+
c.(*CustomFields).CustomId(context.TODO(), u4)
102104
ast.Equal(0, u4.InvalidId)
103105

104106
}

field/default_field.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,37 @@ type DefaultFieldHook interface {
3131
type DefaultField struct {
3232
Id primitive.ObjectID `bson:"_id"`
3333
CreateAt time.Time `bson:"createAt"`
34+
CreateBy string `bson:"createBy"`
3435
UpdateAt time.Time `bson:"updateAt"`
36+
UpdateBy string `bson:"updateBy"`
3537
}
3638

3739
// DefaultUpdateAt changes the default updateAt field
3840
func (df *DefaultField) DefaultUpdateAt() {
3941
df.UpdateAt = time.Now().Local()
4042
}
4143

44+
// DefaultUpdateBy changes the default updateBy field
45+
func (df *DefaultField) DefaultUpdateBy() {
46+
if df.UpdateBy == "" {
47+
df.UpdateBy = "Unknown"
48+
}
49+
}
50+
4251
// DefaultCreateAt changes the default createAt field
4352
func (df *DefaultField) DefaultCreateAt() {
4453
if df.CreateAt.IsZero() {
4554
df.CreateAt = time.Now().Local()
4655
}
4756
}
4857

58+
// DefaultCreateBy changes the default createBy field
59+
func (df *DefaultField) DefaultCreateBy() {
60+
if df.CreateBy == "" {
61+
df.CreateBy = "Unknown"
62+
}
63+
}
64+
4965
// DefaultId changes the default _id field
5066
func (df *DefaultField) DefaultId() {
5167
if df.Id.IsZero() {

0 commit comments

Comments
 (0)