package sessions import ( "encoding/json" "slices" "strconv" ) // hours is a day's Active by the local hour it was put on, kept as only // the hours that have some: of the owner's 19,191 days, 13,406 had hours, // and 19,107 hours had any, so 24 int64s a day (192 bytes) were ~2.6 MB of // zeros held for good. Each entry is hour<> hourShift) } func msOf(e uint64) int64 { return int64(e & (1< h: *hs = slices.Insert(*hs, i, hourEntry(h, ms)) return } } *hs = append(*hs, hourEntry(h, ms)) } func (hs hours) MarshalJSON() ([]byte, error) { var all [24]int64 for _, e := range hs { all[hourOf(e)] = msOf(e) } b := make([]byte, 0, 2+24*2) b = append(b, '[') for i, ms := range all { if i > 0 { b = append(b, ',') } b = strconv.AppendInt(b, ms, 10) } return append(b, ']'), nil } func (hs *hours) UnmarshalJSON(b []byte) error { var all []int64 if err := json.Unmarshal(b, &all); err != nil { return err } if all == nil { *hs = nil return nil } n := 0 for _, ms := range all[:min(len(all), 24)] { if ms > 0 { n++ } } out := make(hours, 0, max(n, 1)) for h, ms := range all[:min(len(all), 24)] { if ms > 0 { out = append(out, hourEntry(h, ms)) } } if len(out) == 0 { out = append(out, hourEntry(0, 0)) } *hs = out return nil }