1414package field
1515
1616import (
17+ "context"
1718 "fmt"
1819 "reflect"
1920 "time"
@@ -24,9 +25,13 @@ import (
2425
2526// CustomFields defines struct of supported custom fields
2627type 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
3843type 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
5670func (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
6283func (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
0 commit comments