@@ -15,8 +15,8 @@ import (
1515
1616// HistoryRecord represents history part of metadata record
1717type HistoryRecord struct {
18- User string
19- Timestamp int64
18+ User string `json:"user"`
19+ Timestamp int64 `json:"timestamp"`
2020}
2121
2222// String provives string representation of history record
@@ -198,20 +198,33 @@ func insertData(sname string, rec map[string]any, attrs, sep, div string, update
198198 }
199199 // add history part of the record
200200 if user , ok := rec ["user" ]; ok {
201- hrec := HistoryRecord {User : user .(string ), Timestamp : time .Now ().Unix ()}
202201 var hrecords []HistoryRecord
203202 if val , ok := rec ["history" ]; ok {
204- var existingRecords []HistoryRecord
205- if err := json .Unmarshal ([]byte (fmt .Sprintf ("%v" , val )), & existingRecords ); err == nil {
206- // perform mapping from rec["history"] which is []any data-type to history records
207- // if successful we use existing records
208- hrecords = existingRecords
203+ switch records := val .(type ) {
204+ case []any :
205+ for _ , r := range records {
206+ switch hr := r .(type ) {
207+ case HistoryRecord :
208+ hrecords = append (hrecords , hr )
209+ case map [string ]any :
210+ var hrec HistoryRecord
211+ if data , err := json .Marshal (hr ); err == nil {
212+ if err := json .Unmarshal (data , & hrec ); err == nil {
213+ hrecords = append (hrecords , hrec )
214+ } else {
215+ log .Println ("ERROR: unable to unmarshal mongodb record" , err )
216+ }
217+ } else {
218+ log .Println ("ERROR: unable to marshal mongodb record" , err )
219+ }
220+ }
221+ }
209222 }
210- hrecords = append (hrecords , hrec )
211- rec ["history" ] = hrecords
212- } else {
213- rec ["history" ] = []HistoryRecord {hrec }
214223 }
224+ // add new history record
225+ hrec := HistoryRecord {User : user .(string ), Timestamp : time .Now ().Unix ()}
226+ hrecords = append (hrecords , hrec )
227+ rec ["history" ] = hrecords
215228 } else {
216229 msg := fmt .Sprintf ("Metadata record does not contain user key" )
217230 return did , errors .New (msg )
0 commit comments