11package dstu2
22
33import (
4+ "time"
5+
46 dstu2dt "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/datatypes_go_proto"
57 dstu2pb "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/resources_go_proto"
68)
79
8- type WithFunc func (sub * dstu2pb.AuditEvent ) error
10+ type OptionFunc func (sub * dstu2pb.AuditEvent ) error
911
1012// NewAuditEvent creates a new audit event. It takes
1113// productKey and tenant as arguments as these are required
1214// for publishing to to Host Auditing service
13- func NewAuditEvent (productKey , tenant string , options ... WithFunc ) (* dstu2pb.AuditEvent , error ) {
15+ func NewAuditEvent (productKey , tenant string , options ... OptionFunc ) (* dstu2pb.AuditEvent , error ) {
1416 event := & dstu2pb.AuditEvent {}
1517
16- if err := WithSourceExtensionUriValue ("productKey" , productKey )(event ); err != nil {
18+ if err := AddSourceExtensionUriValue ("productKey" , productKey )(event ); err != nil {
1719 return nil , err
1820 }
19- if err := WithSourceExtensionUriValue ("tenant" , tenant )(event ); err != nil {
21+ if err := AddSourceExtensionUriValue ("tenant" , tenant )(event ); err != nil {
2022 return nil , err
2123 }
2224 for _ , w := range options {
@@ -27,8 +29,35 @@ func NewAuditEvent(productKey, tenant string, options ...WithFunc) (*dstu2pb.Aud
2729 return event , nil
2830}
2931
30- // WithObject adds the passed object to the AuditEvent
31- func WithObject (object * dstu2pb.AuditEvent_Object ) WithFunc {
32+ // WithEvent sets the event
33+ func WithEvent (e * dstu2pb.AuditEvent_Event ) OptionFunc {
34+ return func (event * dstu2pb.AuditEvent ) error {
35+ event .Event = e
36+ return nil
37+ }
38+ }
39+
40+ // DateTime returns DateTime
41+ func DateTime (at time.Time ) * dstu2dt.Instant {
42+ return & dstu2dt.Instant {
43+ Precision : dstu2dt .Instant_MICROSECOND ,
44+ ValueUs : at .UnixNano () / 1000 ,
45+ }
46+ }
47+
48+ // AddParticipant adds the participant
49+ func AddParticipant (participant * dstu2pb.AuditEvent_Participant ) OptionFunc {
50+ return func (event * dstu2pb.AuditEvent ) error {
51+ if event .Participant == nil {
52+ event .Participant = []* dstu2pb.AuditEvent_Participant {}
53+ }
54+ event .Participant = append (event .Participant , participant )
55+ return nil
56+ }
57+ }
58+
59+ // AddObject adds the passed object to the AuditEvent
60+ func AddObject (object * dstu2pb.AuditEvent_Object ) OptionFunc {
3261 return func (event * dstu2pb.AuditEvent ) error {
3362 if event .Object == nil {
3463 event .Object = []* dstu2pb.AuditEvent_Object {}
@@ -38,9 +67,20 @@ func WithObject(object *dstu2pb.AuditEvent_Object) WithFunc {
3867 }
3968}
4069
41- // WithExtensionUriValue sets extension Uri/Value tuples, some of which are mandatory
70+ // WithSourceIdentifier sets the source identifier
71+ func WithSourceIdentifier (identifier * dstu2dt.Identifier ) OptionFunc {
72+ return func (event * dstu2pb.AuditEvent ) error {
73+ if event .Source == nil {
74+ event .Source = & dstu2pb.AuditEvent_Source {}
75+ }
76+ event .Source .Identifier = identifier
77+ return nil
78+ }
79+ }
80+
81+ // AddSourceExtensionUriValue sets extension Uri/Value tuples, some of which are mandatory
4282// for successfully posting to HSDP Audit
43- func WithSourceExtensionUriValue (extensionUri , extensionValue string ) WithFunc {
83+ func AddSourceExtensionUriValue (extensionUri , extensionValue string ) OptionFunc {
4484 return func (event * dstu2pb.AuditEvent ) error {
4585 if event .Source == nil {
4686 event .Source = & dstu2pb.AuditEvent_Source {}
@@ -58,7 +98,7 @@ func WithSourceExtensionUriValue(extensionUri, extensionValue string) WithFunc {
5898 }
5999 if ext == nil {
60100 ext = & dstu2dt.Extension {
61- Url : & dstu2dt.Uri {},
101+ Url : & dstu2dt.Uri {Value : "/fhir/device" },
62102 }
63103 event .Source .Extension = append (event .Source .Extension , ext )
64104 }
0 commit comments