github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/audit/record.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package audit 5 6 // Meta represents metadata that can be added to a audit record as name/value pairs. 7 type Meta map[string]interface{} 8 9 // FuncMetaTypeConv defines a function that can convert meta data types into something 10 // that serializes well for audit records. 11 type FuncMetaTypeConv func(val interface{}) (newVal interface{}, converted bool) 12 13 // Record provides a consistent set of fields used for all audit logging. 14 type Record struct { 15 APIPath string 16 Event string 17 Status string 18 UserID string 19 SessionID string 20 Client string 21 IPAddress string 22 Meta Meta 23 metaConv []FuncMetaTypeConv 24 } 25 26 // Success marks the audit record status as successful. 27 func (rec *Record) Success() { 28 rec.Status = Success 29 } 30 31 // Success marks the audit record status as failed. 32 func (rec *Record) Fail() { 33 rec.Status = Fail 34 } 35 36 // AddMeta adds a single name/value pair to this audit record's metadata. 37 func (rec *Record) AddMeta(name string, val interface{}) { 38 if rec.Meta == nil { 39 rec.Meta = Meta{} 40 } 41 42 // possibly convert val to something better suited for serializing 43 // via zero or more conversion functions. 44 var converted bool 45 for _, conv := range rec.metaConv { 46 val, converted = conv(val) 47 if converted { 48 break 49 } 50 } 51 52 rec.Meta[name] = val 53 } 54 55 // AddMetaTypeConverter adds a function capable of converting meta field types 56 // into something more suitable for serialization. 57 func (rec *Record) AddMetaTypeConverter(f FuncMetaTypeConv) { 58 rec.metaConv = append(rec.metaConv, f) 59 }