package models import ( "encoding/json" "errors" "time" ) // A Change is a change in the timeline. type Change struct { ID string `json:"id"` Model string `json:"model"` Op string `json:"op"` Author string `json:"author"` Date time.Time `json:"date"` Keys []ChangeKey `json:"keys"` Objects []ChangeObject `json:"objects"` } // A ChangeKey describes a model directly or indirectly affected by the change. type ChangeKey struct { Model string `json:"model"` ID string `json:"id"` } // A ChangeObject is an object affected by the change. type ChangeObject struct { Data json.RawMessage `json:"-"` TypeName string `json:"-"` } type changeObjectHeader struct { TypeName string `json:"__typename"` } // Channel parses the ChangeObject as a channel. func (co *ChangeObject) Channel() (Channel, error) { if co.TypeName != "Channel" { return Channel{}, errors.New("Incorrect Type Name") } channel := Channel{} err := json.Unmarshal(co.Data, &channel) return channel, err } // UnmarshalJSON implements json.Unmarshaller func (co *ChangeObject) UnmarshalJSON(b []byte) error { data := make([]byte, len(b)) copy(data, b) header := changeObjectHeader{} err := json.Unmarshal(data, &header) if err != nil { return err } co.Data = data co.TypeName = header.TypeName return nil }