package main import ( "bytes" "encoding/json" "fmt" "strconv" ) // Timestamp accepts the millisecond timestamps emitted by the Android client as // well as the older string/second-based representations accepted by the Node // server. It marshals back as a JSON number for the mobile client. type Timestamp int64 func (t *Timestamp) UnmarshalJSON(data []byte) error { value := bytes.TrimSpace(data) if len(value) == 0 || bytes.Equal(value, []byte("null")) { *t = 0 return nil } if value[0] == '"' { var stringValue string if err := json.Unmarshal(value, &stringValue); err != nil { return err } parsed, err := parseTimestamp(stringValue) if err != nil { return err } *t = Timestamp(parsed) return nil } parsed, err := strconv.ParseFloat(string(value), 64) if err != nil { return fmt.Errorf("invalid timestamp %q: %w", string(value), err) } *t = Timestamp(parsed) return nil } func parseTimestamp(value string) (int64, error) { if numeric, err := strconv.ParseFloat(value, 64); err == nil { return int64(numeric), nil } return 0, fmt.Errorf("invalid timestamp %q", value) } type List struct { ID string `json:"id"` Name string `json:"name"` CreatedAt Timestamp `json:"createdAt"` UpdatedAt Timestamp `json:"updatedAt"` IsDeleted bool `json:"isDeleted"` } type Field struct { ID string `json:"id"` Name string `json:"name"` FieldType string `json:"fieldType"` FieldOptions string `json:"fieldOptions"` Alignment string `json:"alignment"` ListID string `json:"listId"` Order int `json:"order"` CreatedAt Timestamp `json:"createdAt"` UpdatedAt Timestamp `json:"updatedAt"` IsDeleted bool `json:"isDeleted"` } type Item struct { ID string `json:"id"` ListID string `json:"listId"` CreatedAt Timestamp `json:"createdAt"` UpdatedAt Timestamp `json:"updatedAt"` IsDeleted bool `json:"isDeleted"` } type ItemValue struct { ID string `json:"id"` ItemID string `json:"itemId"` FieldID string `json:"fieldId"` Value string `json:"value"` UpdatedAt Timestamp `json:"updatedAt"` } type SyncRequest struct { LastSyncTimestamp Timestamp `json:"lastSyncTimestamp"` LastSyncToken *int64 `json:"lastSyncToken"` Lists []List `json:"lists"` Fields []Field `json:"fields"` Items []Item `json:"items"` ItemValues []ItemValue `json:"itemValues"` DeviceID string `json:"deviceId,omitempty"` } type SyncResponse struct { Lists []List `json:"lists"` Fields []Field `json:"fields"` Items []Item `json:"items"` ItemValues []ItemValue `json:"itemValues"` OrphanItemValues []OrphanValue `json:"orphanItemValues"` ServerTimestamp int64 `json:"serverTimestamp"` ServerChangeToken int64 `json:"serverChangeToken"` NextSyncToken int64 `json:"nextSyncToken"` HasMore bool `json:"hasMore"` } type PageResponse[T any] struct { Items []T `json:"items"` NextCursor string `json:"nextCursor"` HasMore bool `json:"hasMore"` } type OrphanValue struct { ID string `json:"id"` ItemID string `json:"itemId"` FieldID string `json:"fieldId"` Reason string `json:"reason"` } type NotificationEvent struct { ID string `json:"id"` DeviceIDOrigin string `json:"deviceIdOrigin,omitempty"` EventType string `json:"eventType"` EntityType string `json:"entityType"` EntityID string `json:"entityId,omitempty"` ListID string `json:"listId,omitempty"` CreatedAt int64 `json:"createdAt"` } type PollResponse struct { Notifications []NotificationEvent `json:"notifications"` ServerTimestamp int64 `json:"serverTimestamp"` }